% Simulation model of a single server queue
% Adapted from Law and Kelton (1991)
% T.Trani and H. Baik (March 99)

% Arrival function

% Q_LIMIT				= queue limit to terminate the simulation
% mean_interarrival		= interarrival time in units of time
% mean_service			= service time in time units
% mean_delays_required 	= numbers of entities simulated

% Function calls: 	expon.m (negative exponential distribution)

% Called from:		sim_queue.m (main function)

function arrive

global Q_LIMIT  mean_interarrival  mean_service mean_delays_required 
global next_event_type num_custs_delayed num_delays_required...
       num_events num_in_q server_status
global area_num_in_q area_server_status mean_interarrival...
       mean_service time time_arrival...
       time_last_event time_next_event total_of_delays arr

 
  time_next_event(1) = time + expon(mean_interarrival); % next arrival time
  %time
  %disp(['arrival']);
  %pause;

  %check to see whether server is busy
 
  if(server_status == 1) % server is busy
  
     num_in_q = num_in_q + 1 ; % increase the no. of customers in queue
     
     if(num_in_q > Q_LIMIT)
        disp(['num_in_q = ', num2str(num_in_q)]);
        disp(['Overflow of the array of time_arrival at ',num2str(time)]);
        pause
     end

     time_arrival(num_in_q) = time;

  else 				%server is idle
    
     % delay = 0.0;
     % total_of_delays = total_of_delays + delay;

     num_custs_delayed = num_custs_delayed + 1;
     server_status = 1;

     time_next_event(2) = time + expon(mean_service);
	 arr(num_custs_delayed + 1) = expon(mean_service);
  end


 

  
  

