package nl.tudelft.simulation.examples.dsol.terminal; import java.rmi.RemoteException; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicLong; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.logger.SimLogger; import nl.tudelft.simulation.dsol.simtime.SimTime; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; import nl.tudelft.simulation.event.EventProducer; import nl.tudelft.simulation.event.EventType; /** * A resource defines a shared and limited amount.. *
* Copyright (c) 2002-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights * reserved. See for project information * https://simulation.tudelft.nl. The DSOL project is distributed under a three-clause BSD-style license, which can * be found at * https://simulation.tudelft.nl/dsol/3.0/license.html. *
* @author Alexander Verbraeck * @param the absolute time type to use in timed events * @paramreturn this.getCapacity()-this.getClaimedCapacity()
* @return the currently available capacity on this resource.
*/
public final long getAvailableCapacity()
{
return this.capacity - this.claimedCapacity;
}
/**
* returns the number of instances currently waiting for this resource.
* @return the number of instances currently waiting for this resource
*/
public final int getQueueLength()
{
return this.requests.size();
}
/**
* Method alterClaimedCapacity.
* @param amount refers the amount which is added to the claimed capacity
* @throws RemoteException on network failure
*/
private synchronized void alterClaimedCapacity(final long amount) throws RemoteException
{
this.claimedCapacity += amount;
this.fireTimedEvent(IntResource.UTILIZATION_EVENT, this.claimedCapacity, this.simulator.getSimulatorTime());
}
/**
* sets the capacity of the resource.
* @param capacity the new maximal capacity
*/
public final void setCapacity(final long capacity)
{
this.capacity = capacity;
try
{
this.releaseCapacity(0);
}
catch (RemoteException remoteException)
{
// This exception cannot occur.
SimLogger.always().error(remoteException, "setCapacity");
}
}
/**
* requests an amount of capacity from the resource.
* @param amount the requested amount
* @param requestor the RequestorInterface requesting the amount
* @throws RemoteException on network failure
* @throws SimRuntimeException on other failures
*/
public final synchronized void requestCapacity(final long amount,
final IntResourceRequestorInterface requestor) throws RemoteException, SimRuntimeException
{
this.requestCapacity(amount, requestor, IntResource.DEFAULT_REQUEST_PRIORITY);
}
/**
* requests an amount of capacity from the resource.
* @param amount the requested amount
* @param requestor the RequestorInterface requesting the amount
* @param priority the priority of the request
* @throws RemoteException on network failure
* @throws SimRuntimeException on other failures
*/
public final synchronized void requestCapacity(final long amount,
final IntResourceRequestorInterface requestor, final int priority)
throws RemoteException, SimRuntimeException
{
if (amount < 0)
{
throw new SimRuntimeException("requested capacity on resource cannot < 0.0");
}
if ((this.claimedCapacity + amount) <= this.capacity)
{
this.alterClaimedCapacity(amount);
this.simulator.scheduleEventNow(this, requestor, "receiveRequestedResource",
new Object[]{new Long(amount), this});
}
else
{
synchronized (this.requests)
{
this.requests.add(new Request(requestor, amount, priority));
}
this.fireTimedEvent(IntResource.RESOURCE_REQUESTED_QUEUE_LENGTH, (double) this.requests.size(),
this.simulator.getSimulatorTime());
}
}
/**
* releases an amount of capacity from the resource.
* @param amount the amount to release
* @throws RemoteException on network failure
*/
public final void releaseCapacity(final long amount) throws RemoteException
{
if (amount < 0)
{
throw new IllegalArgumentException("released capacity on resource cannot < 0.0");
}
if (amount > 0)
{
this.alterClaimedCapacity(-Math.min(this.capacity, amount));
}
synchronized (this.requests)
{
for (Iterator