package nl.tudelft.simulation.examples.dsol.terminal; import java.io.Serializable; 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 org.djutils.event.EventProducer; import org.djutils.event.TimedEventType; import org.djutils.metadata.MetaData; import org.djutils.metadata.ObjectDescriptor; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.simtime.SimTime; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; /** * A resource defines a shared and limited amount.. *
* Copyright (c) 2002-2022 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 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 int getQueueLength()
{
return this.requests.size();
}
/**
* Method alterClaimedCapacity.
* @param amount long; 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 long; the new maximal capacity
*/
public void setCapacity(final long capacity)
{
this.capacity = capacity;
try
{
this.releaseCapacity(0);
}
catch (RemoteException remoteException)
{
// This exception cannot occur.
this.simulator.getLogger().always().error(remoteException, "setCapacity");
}
}
/**
* requests an amount of capacity from the resource.
* @param amount long; the requested amount
* @param requestor IntResourceRequestorInterface<A,R,T>; the RequestorInterface requesting the amount
* @throws RemoteException on network failure
* @throws SimRuntimeException on other failures
*/
public 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 long; the requested amount
* @param requestor IntResourceRequestorInterface<A,R,T>; the RequestorInterface requesting the amount
* @param priority int; the priority of the request
* @throws RemoteException on network failure
* @throws SimRuntimeException on other failures
*/
public 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[] {Long.valueOf(amount), this});
}
else
{
synchronized (this.requests)
{
this.requests.add(new Request(requestor, amount, priority));
}
this.fireTimedEvent(IntResource.RESOURCE_REQUESTED_QUEUE_LENGTH, this.requests.size(),
this.simulator.getSimulatorTime());
}
}
/**
* releases an amount of capacity from the resource.
* @param amount long; the amount to release
* @throws RemoteException on network failure
*/
public 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