edu.uiuc.cs.net.DPRPManager
Class DPRPClientSession

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

public class DPRPClientSession
extends java.lang.Thread
implements DPRPClientConstants

This class represents an individual DPRP Client session. Essentially, this means that this object is instantiated into a new thread. Then, it starts by negotiating a lease from the server. If this is successful, it sleeps until such a time until it must renew the lease. If the renewal process is successful, it will sleep again, repeating this cycle for as long as it can.

Version:
1.2 - October 30th, 2001
Author:
Andy Reitz (areitz@cs.uiuc.edu)

Field Summary
private  DPRPClient client
          A reference to the client object that spawned us.
private  DPRPLease curLease
          A reference to the current lease that we are holding.
private  java.net.InetAddress myIP
          Our local IP address, as specified when the client was launched.
private  int myPort
          Local port, associated with the socket.
private  DPRPSocket mySock
          Local socket, refreshed for each new request.
private  int reqDuration
          The lease duration to request for our lease.
private  short reqFlags
          The flags to guide the lease request.
private  byte reqProtocol
          The protocol to request for our lease.
private  int requestedPort
          A specific port that we'd like to request.
private  java.net.InetAddress serverIP
          The IP address of the server that will give us the goods.
private  int serverPort
          The port on the server to connect to.
 
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
 
Fields inherited from interface edu.uiuc.cs.net.DPRPManager.DPRPClientConstants
CLI_COMMAND, CLI_FORCE_LEASE_TERMINATE, CLI_FORCE_LEASE_UPDATE, CLI_TERMINATE_THREAD, cliIPFieldString, HALT, HALT_ACQUIRE_LEASE, HALT_INTERRUPTED, HALT_INVALID_SLEEP_TIMER, HALT_NO_RESPONSE, HALT_SERVER_NACK, HALT_T2_EXPIRED, NUM_RETRIES, okButtonString, reqDurationFieldString, reqPortFieldString, reqProtocolFieldString, resetButtonString, srvIPFieldString, srvPortFieldString, terminateButtonString, updateButtonString
 
Constructor Summary
DPRPClientSession(DPRPClient pcp)
          Constructs a new DPRPClientSession out of a DPRPClient object instance.
 
Method Summary
private  void allocNewMySock()
          Allocates a new socket.
private  DPRPMessage createNewMessage()
          Simply creates a new DPRPMessage, and fills it in with the parameters that we know.
 java.lang.Thread getThreadID()
          Used by the parent DPRPClient in order to interrupt us when (and if) needed.
private  int maintainCurrentLease()
          Looks after the upkeep of the current lease, by renewing it before it expires.
private  DPRPLease renewLease(DPRPLease lea)
          Attempts to renew an outstanding lease.
private  DPRPLease requestLease()
          Uses lower-level methods to request a lease from the server.
 void run()
          The method invoked at thread creation, to start things off.
private  DPRPMessage sendAndRecvMessage(DPRPMessage outMessage)
          Keep sending the given message until an appropriate response is received from the server.
private  DPRPMessage sendCliRequest()
          Sends a DPRP_CLI_REQUEST message to the server.
private  DPRPMessage sendPortAck(DPRPMessage sentMessage)
          Accepts an offered port from the server.
private  DPRPMessage sendPortNack(DPRPMessage msg)
          Sends 'CLI_OFFER_NACK' in response to offer contained in parameter 'msg'.
private  DPRPLease terminateLease(DPRPLease lea)
          Sends a CLI_TERMINATE_LEASE message, immediately revoking the given lease.
 
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

serverIP

private java.net.InetAddress serverIP
The IP address of the server that will give us the goods.

serverPort

private int serverPort
The port on the server to connect to.

requestedPort

private int requestedPort
A specific port that we'd like to request.

myIP

private java.net.InetAddress myIP
Our local IP address, as specified when the client was launched.

myPort

private int myPort
Local port, associated with the socket.

mySock

private DPRPSocket mySock
Local socket, refreshed for each new request.

client

private DPRPClient client
A reference to the client object that spawned us.

reqDuration

private int reqDuration
The lease duration to request for our lease.

reqFlags

private short reqFlags
The flags to guide the lease request.

reqProtocol

private byte reqProtocol
The protocol to request for our lease.

curLease

private DPRPLease curLease
A reference to the current lease that we are holding.
Constructor Detail

DPRPClientSession

public DPRPClientSession(DPRPClient pcp)
Constructs a new DPRPClientSession out of a DPRPClient object instance.
Parameters:
pcp - A DPRPClient instance, to base ourselves off of.
Method Detail

getThreadID

public java.lang.Thread getThreadID()
Used by the parent DPRPClient in order to interrupt us when (and if) needed.

run

public void run()
The method invoked at thread creation, to start things off. I think that maybe this method should fire error events at the GUI, so that nice error notification can occur.
Overrides:
run in class java.lang.Thread

maintainCurrentLease

private int maintainCurrentLease()
                          throws DPRPErrorMessageException
Looks after the upkeep of the current lease, by renewing it before it expires. This is accomplished by sleeping for t1 seconds, and then establishing a renew conversation with the server. If this is unsuccessful (but not via an explicit NACK), then we sleep for half of the difference between t2 and t1, and try again. This process continues, until either t2 is expired, the lease is expired, or a new lease is successfully negotiated.
Returns:
An control code, specifying the reason for loop termination.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

requestLease

private DPRPLease requestLease()
                        throws DPRPErrorMessageException
Uses lower-level methods to request a lease from the server. This method makes use of the sendCliRequest() method, and then error-checks the response, and generates a lease if all is well.
Returns:
A brand-spankin'-new lease, or null if the server wasn't feelin' it.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

renewLease

private DPRPLease renewLease(DPRPLease lea)
                      throws DPRPErrorMessageException
Attempts to renew an outstanding lease. This method will transmit a DPRP_CLI_RENEW_REQUEST message, and deal with the server's reply.
Parameters:
lea - The current outstanding lease to be renewed.
Returns:
A fresh lease, or null if one could not be obtained.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

sendCliRequest

private DPRPMessage sendCliRequest()
                            throws DPRPErrorMessageException
Sends a DPRP_CLI_REQUEST message to the server. It gains the necessary variables to include in the packet from class-wide instance variables.
Returns:
A valid offer (in the form of a DPRPMessage), or null if there was a problem.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

sendPortAck

private DPRPMessage sendPortAck(DPRPMessage sentMessage)
                         throws DPRPErrorMessageException
Accepts an offered port from the server. This is accomplished by sending a CLI_OFFER_ACK message, and then waiting for the accompanying SRV_COMMIT_OFFER message.
Parameters:
sentMessage - A skeleton of the message to send (type and addresses are not correct).
Returns:
The server offer message, or null if such a message could not be obtained.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

sendPortNack

private DPRPMessage sendPortNack(DPRPMessage msg)
                          throws DPRPErrorMessageException
Sends 'CLI_OFFER_NACK' in response to offer contained in parameter 'msg'. Expects to receive a 'SRV_OFFER_NACK_ACK' in response.
Parameters:
msg - An offer message that we don't like. Basis for the Nack.
Returns:
A 'SRV_OFFER_NACK_ACK' message.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

terminateLease

private DPRPLease terminateLease(DPRPLease lea)
                          throws DPRPErrorMessageException
Sends a CLI_TERMINATE_LEASE message, immediately revoking the given lease.
Parameters:
lea - A lease for which we want to immediately revoke our handle.
Returns:
A lease based upon the return message, or null.
Throws:
DPRPErrorMessageException - This is passed on from the call to sendAndRecvMessage().

sendAndRecvMessage

private DPRPMessage sendAndRecvMessage(DPRPMessage outMessage)
                                throws DPRPErrorMessageException
Keep sending the given message until an appropriate response is received from the server. Should only return null in the event that the server cannot be contacted, or if there is a socket error. Of course, I chould just make up new DPRPErrors for these situations, which is an interesting thought. I could throw UNKNOWN_ERRORS for the time being.

The problem with this idea is that in the catch, I'd have to check to see if this is a 'soft error' (i.e., I should retry), or if it is a 'hard error' (i.e., we got an error message). So, I'm going to leave the status quo for now.

Parameters:
outMessage - The outgoing DPRPMessage to be sent.
Returns:
A valid DPRPMessage that was received from the server.
Throws:
DPRPErrorMessageException - If an actual, honest-to-goodness, error message is received from the server.

createNewMessage

private DPRPMessage createNewMessage()
Simply creates a new DPRPMessage, and fills it in with the parameters that we know.
Returns:
A fully-formed DPRPMessage.

allocNewMySock

private void allocNewMySock()
Allocates a new socket. This code was broken out, because a new socket is now allocated for every renew request. The problem with this code, is that it not only returns a new socket, but it also damages the local myPort. That's frugli. The way to do this would be to return void, and just go ahead and damage both local variables.