forward.server
Class ForwardScheduler

java.lang.Object
  extended by forward.server.ForwardScheduler
All Implemented Interfaces:
java.lang.Runnable

public class ForwardScheduler
extends java.lang.Object
implements java.lang.Runnable

This class allocates the bandwidth to all forwarded connections. Also the server part is managed here.


Field Summary
private static long CYCLE_TIME
          This is the time between two allocation rounds in milliseconds.
private  java.util.Vector m_connectionHandler
          This stores all active connections (ForwardConnection).
private  int m_netBandwidth
          The maximum bandwidth for all connections together in bytes/second.
private  int m_nrOfConnections
          This stores the maximum number of simultaneous connections.
private  java.lang.Thread m_schedulerThread
          This stores the internal scheduling thread.
private  java.util.Hashtable m_serverManagers
          This stores the associated ServerManagers (ID as keys and the instances as values) managing the server sockets of this ForwardScheduler.
private  boolean m_shutdown
          This value is set to true, for signalizing the shutdown of this ForwardScheduler to the internal thread.
private  ForwardSchedulerStatistics m_statistics
          This stores the statistics for the ForwardScheduler.
 
Constructor Summary
ForwardScheduler()
          Creates a new ForwardScheduler.
 
Method Summary
 void addServerManager(IServerManager a_serverManager)
          Adds a ServerManager to the list of associated ServerManagers.
 int getCurrentlyForwardedConnections()
          Returns the number of currently forwarded connections.
 int getGuaranteedBandwidth()
          Returns the guaranteed bandwidth for one connection in bytes/sec.
 int getMaximumBandwidth()
          Returns the maximum bandwidth a connection can use in bytes/sec.
 ForwardSchedulerStatistics getStatistics()
          Returns the statistics instance for this ForwardScheduler.
 void handleNewConnection(IStreamConnection a_newConnection)
          The associated ServerManager signalize any new connection here.
 void removeAllServerManagers()
          Removes all ServerManagers from the list of associated ServerManagers of this ForwardScheduler.
 void removeConnection(ForwardConnection a_connectionToRemove)
          This method is called from a ForwardConnection to signalize, that the connection is closed and can be removed from the connection store of all forwarded connections.
 void removeServerManager(java.lang.Object a_serverManagerId)
          Removes one ServerManager from the list of associated ServerManagers of this ForwardScheduler.
 void run()
          This is the implementation of the internal thread.
 void setMaximumNumberOfConnections(int a_maximumNumberOfConnections)
          Changes the number of simultaneously forwarded client connections.
 void setNetBandwidth(int a_netBandwidth)
          Changes the maximum bandwidth (net bandwidth, without TCP/IP headers...) which can be used by all client connections together.
 void shutdown()
          This method must be called, if the ForwardScheduler shall come to an end.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CYCLE_TIME

private static final long CYCLE_TIME
This is the time between two allocation rounds in milliseconds.

See Also:
Constant Field Values

m_nrOfConnections

private int m_nrOfConnections
This stores the maximum number of simultaneous connections. If this number is reached, any new connection will be rejected (immediately closed after acception) until one of the active connections is closed.


m_netBandwidth

private int m_netBandwidth
The maximum bandwidth for all connections together in bytes/second. Because of the forwarding, this is the amount of upstream = amount of downstream data.


m_connectionHandler

private java.util.Vector m_connectionHandler
This stores all active connections (ForwardConnection).

See Also:
ForwardConnection

m_shutdown

private boolean m_shutdown
This value is set to true, for signalizing the shutdown of this ForwardScheduler to the internal thread.

See Also:
run()

m_schedulerThread

private java.lang.Thread m_schedulerThread
This stores the internal scheduling thread.


m_serverManagers

private java.util.Hashtable m_serverManagers
This stores the associated ServerManagers (ID as keys and the instances as values) managing the server sockets of this ForwardScheduler.

See Also:
ServerManager

m_statistics

private ForwardSchedulerStatistics m_statistics
This stores the statistics for the ForwardScheduler.

Constructor Detail

ForwardScheduler

public ForwardScheduler()
Creates a new ForwardScheduler. Also the internal bandwidth allocation thread is started. The initial number of simultaneously forwarded client connections is set to 0 and the bandwidth is also set to 0 bytes/sec.

Method Detail

handleNewConnection

public void handleNewConnection(IStreamConnection a_newConnection)
The associated ServerManager signalize any new connection here. If there are empty slots, we accept this new connection. If we already holds the maximum number of connections, the new connection is immediately closed.

Parameters:
a_newConnection - The new connection.

removeConnection

public void removeConnection(ForwardConnection a_connectionToRemove)
This method is called from a ForwardConnection to signalize, that the connection is closed and can be removed from the connection store of all forwarded connections.

Parameters:
a_connectionToRemove - The closed connection which can be removed.

shutdown

public void shutdown()
This method must be called, if the ForwardScheduler shall come to an end. All associated ServerSockets and all forward connections are closed and the internal scheduling thread is stopped. This method will block until all ServerSockets and forwarded connections are closed and the internal scheduling thread has come to the end.


getMaximumBandwidth

public int getMaximumBandwidth()
Returns the maximum bandwidth a connection can use in bytes/sec. That is the total bandwidth, because if there are no other connections or they have no bytes to transmit, one connection gan get all the bandwidth we have.

Returns:
The maximum bandwidth for one connection in bytes/sec.

getGuaranteedBandwidth

public int getGuaranteedBandwidth()
Returns the guaranteed bandwidth for one connection in bytes/sec. If there is no other bottleneck, every connection gets [total bandwidth / maximum number of connections] as guaranteed minimal bandwidth.

Returns:
The guaranteed bandwidth for every connection.

setMaximumNumberOfConnections

public void setMaximumNumberOfConnections(int a_maximumNumberOfConnections)
Changes the number of simultaneously forwarded client connections. If the new number is less than the old one and there are more forwarded connections at the moment, we closes some randomly choosed connections.


setNetBandwidth

public void setNetBandwidth(int a_netBandwidth)
Changes the maximum bandwidth (net bandwidth, without TCP/IP headers...) which can be used by all client connections together.

Parameters:
a_netBandwidth - The maximum bandwidth for all client connections together (= average upstream = average downstream) in bytes/sec.

addServerManager

public void addServerManager(IServerManager a_serverManager)
                      throws java.lang.Exception
Adds a ServerManager to the list of associated ServerManagers. Also the ServerManager is started (startServerManager() is called). This call throws an exception, if there was an error while starting the ServerManager.

Parameters:
a_serverManager - The ServerManager to add.
Throws:
java.lang.Exception

removeServerManager

public void removeServerManager(java.lang.Object a_serverManagerId)
Removes one ServerManager from the list of associated ServerManagers of this ForwardScheduler. The shutdown() method is called on that ServerManager and it is removed from the internal list. Active forwarded connections are not affected by this call. If this ForwardScheduler doesn't know any ServerManager with the specified ID, nothing is done.

Parameters:
a_serverManagerId - The ID of the ServerManager to close, see IServerManager.getId().

removeAllServerManagers

public void removeAllServerManagers()
Removes all ServerManagers from the list of associated ServerManagers of this ForwardScheduler. The shutdown() method is called on every ServerManager and all ServerManagers are removed from the internal list. There can't be opened new connections until at least one server manager is added to the list. Active forwarded connections are not affected by this call.


getStatistics

public ForwardSchedulerStatistics getStatistics()
Returns the statistics instance for this ForwardScheduler.

Returns:
The statistics for this ForwardScheduler.

getCurrentlyForwardedConnections

public int getCurrentlyForwardedConnections()
Returns the number of currently forwarded connections.

Returns:
The number of currently forwarded connections.

run

public void run()
This is the implementation of the internal thread. It allocates the bandwidth for all forwarded connections periodically every CYCLE_TIME milliseconds.

Specified by:
run in interface java.lang.Runnable