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

% Departure 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 depart

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 std_interarrival dep

    if(num_in_q == 0) 
    % queue is empty. so make the server idle and eliminate the departure event from the considerarion
      
        server_status = 0; 					%idle
        time_next_event(2) = 1.0*exp(30);

    else
    % queue is not empty, so decrease the no. of customers in queue
      
       num_in_q = num_in_q - 1;

       delay = time - time_arrival(1);
       total_of_delays = total_of_delays + delay;

       num_custs_delayed = num_custs_delayed + 1;
       time_next_event(2) = time + expon(mean_service);
	   dep (num_custs_delayed + 1) = expon(mean_service);
       
       % move each customer's arrival time in queue up one place
       for i = 1:num_in_q
          time_arrival(i)=time_arrival(i+1);
       end
    
    end         
  
   
