edu.uiuc.cs.net.DPRPManager
Class DPRPServerSession

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--edu.uiuc.cs.net.DPRPManager.DPRPServerSession
All Implemented Interfaces:
java.lang.Runnable

public class DPRPServerSession
extends java.lang.Thread

This class, when instantiated, will spawn in its own thread, and handle a single client request (be it to request a new lease, or renew an old one). It contains a reference to the global Server Port Manager, which was instantiated in the SimpleDPRPServer class. Using this reference, this server session is able to check the priviliges of the client, regarding its request. Furthermore, this session allocates a local UDP socket, for use in it's replies to the client. This separates this transaction from other transactions that are going on concurrently in the server.

Version:
0.9c - October 27th, 2001
Author:
Andy Reitz (areitz@cs.uiuc.edu)

Field Summary
private  int curXid
          The current XID of this session, for sanity checking.
private  java.net.InetAddress externalIP
          The external IP address of the gateway.
private  java.net.InetAddress internalIP
          The internal IP address of the gateway.
private  DPRPSocket localSock
          Local socket, allocated to keep this session separate from the rest.
private static int NUM_RETRIES
          Constant directing the number of times to retry sending a message.
 DPRPMessage passedMessage
          This message reference is set by the server singleton, whenever it receives an errant message from the client, that is really destined for this session.
private static long RETRY_TIMEOUT
          The amount of time to wait for a message from the client, where we know that they received our final message of the transaction.
private  ServerPortMgr SPM
          Reference to the Server Port Manager singleton.
private  DPRPMessage startMessage
          The message that started this session, to be removed from the server singleton's map.
 
Fields inherited from class java.lang.Thread
contextClassLoader, daemon, eetop, group, inheritableThreadLocals, inheritedAccessControlContext, MAX_PRIORITY, MIN_PRIORITY, name, NORM_PRIORITY, priority, single_step, stillborn, stopThreadPermission, target, threadInitNumber, threadLocals, threadQ
 
Constructor Summary
DPRPServerSession(DPRPMessage inMessage, java.lang.String threadName, java.net.InetAddress extIP)
          New session constructor.
 
Method Summary
private  void handleNewRequest()
          Handles a client request for a new lease.
private  void handleRenewRequest()
          Takes care of a client renewal request.
 void run()
          Run method for DPRPServerSession.
private  DPRPMessage sendAndMaybeRecv(DPRPMessage outMessage, byte msgShouldntRecv)
          The new way to call the send and maybe receiver, which will eventually take over.
private  DPRPMessage sendAndMaybeRecv(DPRPMessage outMessage, byte msgShouldntRecv, boolean expectingMsg)
          Takes in a fully-formed DPRPMessage, and spits out a response message form the client.
private  void sendRenewNack(DPRPLease lea, short reason)
          Constructs and transmits a valid DPRP_SRV_RENEW_NACK packet.
 boolean sendRenewResp(DPRPMessage msg)
          This method sends a "renew" response to the client.
 
Methods inherited from class java.lang.Thread
, activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, exit, getContextClassLoader, getName, getPriority, getThreadGroup, init, interrupt, interrupt0, interrupted, isAlive, isDaemon, isInterrupted, isInterrupted, join, join, join, nextThreadNum, registerNatives, resume, resume0, setContextClassLoader, setDaemon, setName, setPriority, setPriority0, sleep, sleep, start, stop, stop, stop0, suspend, suspend0, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

passedMessage

public DPRPMessage passedMessage
This message reference is set by the server singleton, whenever it receives an errant message from the client, that is really destined for this session. This occurs in error situations -- either the client is re-transmitting it's first request to main server port, or it is sending some sort of error to the main server port. Either way, the server will set this variable, and then call Thread.interrupt(). Thus, at certain points in the code (usually in the exception handler for Thread.sleep()), this code will check this variable, and if it is non-null, it will consume the message, and take the appropriate action.

startMessage

private DPRPMessage startMessage
The message that started this session, to be removed from the server singleton's map.

curXid

private int curXid
The current XID of this session, for sanity checking.

externalIP

private java.net.InetAddress externalIP
The external IP address of the gateway.

internalIP

private java.net.InetAddress internalIP
The internal IP address of the gateway.

localSock

private DPRPSocket localSock
Local socket, allocated to keep this session separate from the rest.

SPM

private ServerPortMgr SPM
Reference to the Server Port Manager singleton.

NUM_RETRIES

private static final int NUM_RETRIES
Constant directing the number of times to retry sending a message.

RETRY_TIMEOUT

private static final long RETRY_TIMEOUT
The amount of time to wait for a message from the client, where we know that they received our final message of the transaction.
Constructor Detail

DPRPServerSession

public DPRPServerSession(DPRPMessage inMessage,
                         java.lang.String threadName,
                         java.net.InetAddress extIP)
New session constructor.
Parameters:
inMessage - The message that caused the creation of a new session.
threadName - The name for this thread, as generated by the server singleton.
extIP - Our external IP address, as specified in a parameter to the server.
Method Detail

run

public void run()
Run method for DPRPServerSession. This method parses the packet that started this session, and either initiates a new request, or a renew request, depending.
Overrides:
run in class java.lang.Thread

handleNewRequest

private void handleNewRequest()
                       throws DPRPOfferException
Handles a client request for a new lease. This entails checking the client's specified parameters (if any) with the Server Port Manager. Furthermore, if the client specified an external port, we must attempt to acquire this port. If it cannot be acquired, than any free port will be offered instead. The offer, once constructed, is sent, and is only commited after the client has properly ACK'd it.
Throws:
DPRPOfferException - If a problem occurs after a port has been allocated. The handler will attempt to cleanup the invalid state in a sane manner.

handleRenewRequest

private void handleRenewRequest()
Takes care of a client renewal request. This entails checking the parameters specified against the Server Port Manager, and either issuing a DPRP_SRV_RENEW_ACK or DPRP_SRV_RENEW_NACK, as appropriate.

sendRenewNack

private void sendRenewNack(DPRPLease lea,
                           short reason)
Constructs and transmits a valid DPRP_SRV_RENEW_NACK packet. Also, this method cleans up any server state regarding this deleted port, and then sends the NACK to the client.
Parameters:
lea - The DPRPLease to be terminated.
reason - The reason (according to the DPRPErrors database) that the lease is being terminated.
See Also:
DPRPErrors

sendRenewResp

public boolean sendRenewResp(DPRPMessage msg)
This method sends a "renew" response to the client. This response can be either an ACK or a NACK, as decided by the callee. All this method really does is to fire out the message, and then block the thread. If we get interrupted, that means that the main server has gotten another message from this client session. In that case, our response must have been lost, so we wake up, consume the client's messsage, and if all is well, retransmit.
Parameters:
msg - The DPRPMessage to send to the client.
Returns:
True if the send came off without a hitch, false if we never were able to ram it through.

sendAndMaybeRecv

private DPRPMessage sendAndMaybeRecv(DPRPMessage outMessage,
                                     byte msgShouldntRecv)
The new way to call the send and maybe receiver, which will eventually take over.
Parameters:
outMessage - A message to send.
msgShouldntRecv - The message type that we don't want to get back. If this is zero, then getting back no message is what we want.
Returns:
A message that was received, if we want one.

sendAndMaybeRecv

private DPRPMessage sendAndMaybeRecv(DPRPMessage outMessage,
                                     byte msgShouldntRecv,
                                     boolean expectingMsg)
Takes in a fully-formed DPRPMessage, and spits out a response message form the client. The 'maybe' flag controls whether a response from the client is necessary or not. To be pedantic, I can fold the last two parameters into one. Scrap the boolean, and if msgShouldntReceive is 0, then expectingMsg is (symbolically) true. Otherwise, it's false, and we're in we-get-a-message-is-bad mode. Confused yet? This method is _entirely_ too complicated.
Parameters:
outMessage - The message to send out to the client.
msgShouldntRecv - The type of message that is expected in response. If 0, don't expect anything.
expectingMsg - Controls wether a message should be received or not.
Returns:
A DPRPMessage if we got a valid one, null otherwise.