/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2013 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.BasicObjects;

import com.jaamsim.Graphics.DisplayEntity;
import com.jaamsim.Samples.SampleConstant;
import com.jaamsim.Samples.SampleInput;
import com.jaamsim.input.Keyword;
import com.jaamsim.units.TimeUnit;

/**
 * Server processes entities one by one from a queue.  When finished with an entity, it passes it to the next
 * LinkedComponent in the chain.
 */
public class Server extends LinkedService {

	@Keyword(description = "The service time required to process an entity.\n" +
			"A constant value, a distribution to be sampled, or a time series can be entered.",
	         exampleList = { "3.0 h", "NormalDistribution1", "'1[s] + 0.5*[TimeSeries1].PresentValue'" })
	private final SampleInput serviceTime;

	private DisplayEntity servedEntity;	// the DisplayEntity being server

	{
		serviceTime = new SampleInput("ServiceTime", "Key Inputs", new SampleConstant(TimeUnit.class, 0.0));
		serviceTime.setUnitType(TimeUnit.class);
		serviceTime.setEntity(this);
		serviceTime.setValidRange(0, Double.POSITIVE_INFINITY);
		this.addInput(serviceTime);
	}

	public Server() {}

	@Override
	public void earlyInit() {
		super.earlyInit();
		servedEntity = null;
	}

	@Override
	public void startAction() {

		// Stop if there is a forced downtime activity about to begin
		if (forcedDowntimePending) {
			forcedDowntimePending = false;
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Determine the match value
		Integer m = this.getNextMatchValue(getSimTime());

		// Stop if the queue is empty or a threshold is closed
		if (waitQueue.getValue().getMatchCount(m) == 0 || !this.isOpen()) {
			this.setBusy(false);
			this.setPresentState();
			return;
		}

		// Remove the first entity from the queue
		servedEntity = this.getNextEntityForMatch(m);
		this.moveToProcessPosition(servedEntity);

		// Schedule the completion of service
		startTime = this.getSimTime();
		duration = serviceTime.getValue().getNextSample(startTime);
		this.scheduleProcess(duration, 5, endActionTarget, endActionHandle);
	}

	@Override
	public void endAction() {

		// Send the entity to the next component in the chain
		this.sendToNextComponent(servedEntity);
		servedEntity = null;

		// Remove the next entity from the queue and start processing
		this.startAction();
	}

}

@@ -1897,31 +1897,55 @@
 e%0A%09p
-ublic void startAction(
+rotected boolean startProcessing(double simTime
 ) %7B%0A
@@ -2081,65 +2081,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2314,65 +2314,20 @@
 %0A%09%09%09
-this.setBusy(false);%0A%09%09%09this.setPresentState();%0A%09%09%09
 return
+ false
 ;%0A%09%09
@@ -2469,213 +2469,21 @@
 y);%0A
-%0A
 %09%09
-// Schedule the completion of service%0A%09%09startTime = this.getSimTime();%0A%09%09duration = serviceTime.getValue().getNextSample(startTime);%0A%09%09this.scheduleProcess(duration, 5, endActionTarget, endActionHandle)
+return true
 ;%0A%09%7D
@@ -2493,29 +2493,32 @@
 @Override%0A%09p
-ublic
+rotected
  void endAct
@@ -2514,23 +2514,41 @@
 void end
-Action(
+Processing(double simTime
 ) %7B%0A%0A%09%09/
@@ -2669,92 +2669,130 @@
 ll;%0A
-%0A%09%09// Remove the next entity from the queue and start processing%0A%09%09this.startAction(
+%09%7D%0A%0A%09@Override%0A%09protected double getProcessingTime(double simTime) %7B%0A%09%09return serviceTime.getValue().getNextSample(simTime
 );%0A%09

use the new LinkedService framework for Server