info.jedi.net
Class ResourceHandler

java.lang.Object
  |
  +--info.jedi.net.ResourceHandler

public class ResourceHandler
extends java.lang.Object

Class ResourceHandler keeps track of allocating resources properly for the jedi.net infrastructure. It provides methods to supply datagram packets, packet handling threads, and sockets. It adds efficiency to JEDI by caching each of these items so they don't have to be created for each incoming JEDI call.

See Also:
MailDaemon

Field Summary
static int MAX_LENGTH
          The maximum length of a UDP packet.
 
Method Summary
 java.net.DatagramPacket getPacket()
          Returns a DatagramPacket suitable for receiving any size of UDP packet up to the maximum size (65535 bytes).
 java.net.DatagramSocket getSocket()
          Returns a socket from this ResourceHandler's cache, or creates a new one if the cache is empty.
 void returnPacket(java.net.DatagramPacket packet)
          Returns a datagram packet to the cache.
 void returnSocket(java.net.DatagramSocket socket)
          Returns a socket allocated with getSocket() to this ResourceHandler's socket cache, so it can be reused by the next getSocket() call.
 void threadBusy()
          Warns the ResourceHandler that the current thread will be busy handling some request and thus unable to handle additional requests for a while.
 void threadIdle()
          Tells the ResourceHandler that a thread (which called threadBusy() previously) is now once more available to take incoming packets
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_LENGTH

public static final int MAX_LENGTH
The maximum length of a UDP packet. This is used to allocate memory for incoming packets
Method Detail

getPacket

public java.net.DatagramPacket getPacket()
Returns a DatagramPacket suitable for receiving any size of UDP packet up to the maximum size (65535 bytes). It will create a new packet if necessary, otherwise use a packet from it's cache. Packets allocated with this method should be returned with returnPacket() when they are no longer needed so that they can be reused with the next call to getPacket().

returnPacket

public void returnPacket(java.net.DatagramPacket packet)
Returns a datagram packet to the cache. This packet should have been acquired by calling getPacket().

threadBusy

public void threadBusy()
Warns the ResourceHandler that the current thread will be busy handling some request and thus unable to handle additional requests for a while. If there is no thread idle (able to accept incoming requests) this method will create one for this ResourceHandler's MailDaemon

threadIdle

public void threadIdle()
Tells the ResourceHandler that a thread (which called threadBusy() previously) is now once more available to take incoming packets

getSocket

public java.net.DatagramSocket getSocket()
                                  throws java.net.SocketException
Returns a socket from this ResourceHandler's cache, or creates a new one if the cache is empty. For efficiency, the caller should return the socket later with returnSocket() when it is finished with the socket.
Throws:
java.net.SocketException - if a socket cannot be created

returnSocket

public void returnSocket(java.net.DatagramSocket socket)
Returns a socket allocated with getSocket() to this ResourceHandler's socket cache, so it can be reused by the next getSocket() call.