edu.uiuc.cs.net.DPRPManager
Class DPRPLease

java.lang.Object
  |
  +--edu.uiuc.cs.net.DPRPManager.DPRPLease

public class DPRPLease
extends java.lang.Object

This class represents all of the details that comprise a "lease". In the DPRP sense, this means that we have several private variables, comprising the external IP address and port on the gateway, as well as duration, protocol, flags, and the starting time for this lease. This class also contains methods to write out a lease into a machine-parsable string, and of course, to suck in a lease from such a string.

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

Field Summary
private  java.net.InetAddress clientIP
          The IP address of the client that registered this lease.
private  int clientPort
          The port of the registering client.
static boolean DEBUG
          In order to ease JAR file creation, this variable is now public, and set to be true whenever the server or client sets it's debug variables to true.
static int DEFAULT_DURATION
          Constant representing the default duration, in seconds.
static short DEFAULT_FLAGS
          Constant representing the default flags for a new lease.
private  int duration
          The length of time, in seconds, during which this lease is valid.
private  java.net.InetAddress externalIP
          The external IP address (on the gateway) for this lease.
private  int externalPort
          The external port (on the gateway) for this lease.
private  short flags
          The flags that govern this lease.
private  short nackError
          Extra field to pass server lease NACK errorcodes.
private  int numRenews
          Number of times this lease has been renewed.
private  byte protocol
          The protocol type that the gateway will forward.
private  java.net.InetAddress serverIP
          I've been forced to make the lease track the IP address of the server that generated it.
private  int serverPort
          The server's main listening port; where new connections go.
private  java.util.Calendar startTime
          The wall-clock time at which this lease was created.
 
Constructor Summary
DPRPLease()
          Simple constructor for DPRPLease.
DPRPLease(DPRPMessage msg, boolean dst)
          DPRPLease constructor from DPRPMessage.
 
Method Summary
 boolean compareToMsg(DPRPMessage msg)
          Compares the parameters in the given message to those in this lease.
 boolean equals(DPRPLease cmp)
          Localized method for determining the equality of two leases.
 boolean equals(java.lang.Object obj)
          Overloading the exact equals() method that appears in Object.
static DPRPLease fromString(java.lang.String str)
          Converts a string representation of a lease into a DPRPLease object.
 java.net.InetAddress getClientIP()
          Returns the client IP address that 'owns' this lease.
 int getClientPort()
          Returns the port of the client that established this lease.
 int getDuration()
          Returns the duration of this lease, in seconds.
 java.net.InetAddress getExternalIP()
          Returns the external IP address of this lease.
 int getExternalPort()
          Gets the external port of this lease.
 short getFlags()
          Returns the current value of the flags.
 short getNackError()
          Deprecated. This will be removed, when I refactor the NACK stuff out of this class.
 int getNumRenews()
          Gets the number of times this lease has been renewed.
 java.lang.String getPrimaryKey()
          Returns the "primary key" for this lease.
 byte getProtocol()
          Return the current protocol for this lease.
 java.lang.String getProtocolStr()
          Return the current protocol as a human-readable string.
 java.net.InetAddress getServerIP()
          Get the server's IP address.
 int getServerPort()
          Gets the server port.
 java.util.Date getStopTime()
          Determines when this lease expires.
 int hashCode()
          Returns an integer represenation of this lease, for use in hashing.
 void incNumRenews()
          Increments the number of times this lease has been renewed.
 boolean isExpired()
          This method checks to see if a lease has expired.
 DPRPMessage messageToClient(java.net.InetAddress sourceIP, int sourcePort)
          Spits out a fully-formed DPRPMessage, addressed to the client.
private static java.util.Vector parseLeaseString(java.lang.String str, char separator)
          An internal method that breaks a string into components around the separator.
 void prettyPrint()
          Prints a lease to stdout in a human-readable format.
static java.util.HashSet readLeaseFile(java.io.File fname)
          Reads ASCII lease representations from disk, and returns them in a collection.
 void setClientIP(java.net.InetAddress a)
          Sets the client IP address for this lease.
 void setClientPort(int extPortInt)
          Sets the client port for this lease.
 void setClientPort(java.lang.String extPortStr)
          Sets the client port for this lease.
 void setDuration(int dur)
          Sets the duration of this lease.
 void setDuration(java.lang.String sDur)
          Sets the duration of this lease.
 void setExternalIP(java.net.InetAddress a)
          Sets the external IP address of this lease.
 void setExternalPort(int extP)
          Sets the external port for this lease.
 void setExternalPort(java.lang.String extS)
          Sets the external port for this lease.
 void setFlags(short f)
          Sets the flags for this lease.
 void setFlags(java.lang.String fStr)
          Set the flags for this lease, given a string representation of a short.
 void setNackError(short r)
          Sets the nack reason.
 void setNumRenews(int i)
          Sets the number of times this lease has been renewed.
 void setNumRenews(java.lang.String s)
          Sets the number of times this lease has been renewed.
 void setProtocol(byte prot)
          Sets the raw protocol.
 void setProtocol(java.lang.String sP)
          Sets the protocol, given a human-readable string.
 void setServerIP(java.net.InetAddress sIP)
          Sets the server's IP.
 void setServerPort(int xp)
          Sets the port on the server, from an int.
 void setServerPort(java.lang.String ps)
          Sets the port on the server, from a string.
 void setStartTime(java.util.Date t)
          Sets the startTime to the Date object given.
 java.lang.String toString()
          Writes out a lease in a computer-parseable format.
 void touchStartTime()
          Updates the startTime for this lease with the current wall-clock time.
static java.util.HashSet writeLeaseFile(java.io.File fname, java.util.HashSet leasesToWrite)
          Writes a given collection of leases to disk, and returns the leases that were actually written.
private static void writeLeaseFileComment(java.io.File fname)
          Writes out the standard comment block at the top of the on-disk lease database.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

DEFAULT_DURATION

public static final int DEFAULT_DURATION
Constant representing the default duration, in seconds.

DEFAULT_FLAGS

public static final short DEFAULT_FLAGS
Constant representing the default flags for a new lease.

duration

private int duration
The length of time, in seconds, during which this lease is valid.

startTime

private java.util.Calendar startTime
The wall-clock time at which this lease was created.

externalIP

private java.net.InetAddress externalIP
The external IP address (on the gateway) for this lease.

externalPort

private int externalPort
The external port (on the gateway) for this lease.

clientIP

private java.net.InetAddress clientIP
The IP address of the client that registered this lease.

clientPort

private int clientPort
The port of the registering client.

serverIP

private java.net.InetAddress serverIP
I've been forced to make the lease track the IP address of the server that generated it. This makes the lease valid after it has been saved to disk, and reloaded by the client.

serverPort

private int serverPort
The server's main listening port; where new connections go.

protocol

private byte protocol
The protocol type that the gateway will forward.

flags

private short flags
The flags that govern this lease.

nackError

private short nackError
Extra field to pass server lease NACK errorcodes.

numRenews

private int numRenews
Number of times this lease has been renewed.

DEBUG

public static boolean DEBUG
In order to ease JAR file creation, this variable is now public, and set to be true whenever the server or client sets it's debug variables to true.
Constructor Detail

DPRPLease

public DPRPLease(DPRPMessage msg,
                 boolean dst)
DPRPLease constructor from DPRPMessage.
Parameters:
msg - A DPRPMessage, containing the parameters to make a lease out of.
dst - Wether the proposed lease's 'ClientIP' refers to the destination or the source of the message.

DPRPLease

public DPRPLease()
Simple constructor for DPRPLease.
Method Detail

getExternalIP

public java.net.InetAddress getExternalIP()
Returns the external IP address of this lease.

setExternalIP

public void setExternalIP(java.net.InetAddress a)
Sets the external IP address of this lease.

getExternalPort

public int getExternalPort()
Gets the external port of this lease.

setExternalPort

public void setExternalPort(int extP)
Sets the external port for this lease.
Parameters:
extP - An integer value, representing the port.

setExternalPort

public void setExternalPort(java.lang.String extS)
                     throws java.lang.NumberFormatException
Sets the external port for this lease.
Parameters:
extS - A string value, to be converted to an integral port value.
Throws:
java.lang.NumberFormatException - If the string doesn't contain a valid integer.

getClientIP

public java.net.InetAddress getClientIP()
Returns the client IP address that 'owns' this lease.

setClientIP

public void setClientIP(java.net.InetAddress a)
Sets the client IP address for this lease.

getClientPort

public int getClientPort()
Returns the port of the client that established this lease.

setClientPort

public void setClientPort(int extPortInt)
Sets the client port for this lease.
Parameters:
extP - An integral port value.

setClientPort

public void setClientPort(java.lang.String extPortStr)
                   throws java.lang.NumberFormatException
Sets the client port for this lease.
Parameters:
extPortStr - A string, hopefully containing an integer port value.
Throws:
java.lang.NumberFormatException - If the string doesn't contain a valid integer value.

getServerIP

public java.net.InetAddress getServerIP()
Get the server's IP address.

setServerIP

public void setServerIP(java.net.InetAddress sIP)
Sets the server's IP.

getServerPort

public int getServerPort()
Gets the server port.

setServerPort

public void setServerPort(int xp)
Sets the port on the server, from an int.

setServerPort

public void setServerPort(java.lang.String ps)
                   throws java.lang.NumberFormatException
Sets the port on the server, from a string.

getDuration

public int getDuration()
Returns the duration of this lease, in seconds.

setDuration

public void setDuration(int dur)
Sets the duration of this lease.
Parameters:
dur - An integer number of seconds, to comprise the new duration.

setDuration

public void setDuration(java.lang.String sDur)
                 throws java.lang.NumberFormatException
Sets the duration of this lease.
Parameters:
sDur - An string, representing an integer number of seconds.
Throws:
java.lang.NumberFormatException - If the string doesn't contain a valid integer value.

getProtocol

public byte getProtocol()
Return the current protocol for this lease.

getProtocolStr

public java.lang.String getProtocolStr()
Return the current protocol as a human-readable string.

setProtocol

public void setProtocol(byte prot)
Sets the raw protocol.

setProtocol

public void setProtocol(java.lang.String sP)
                 throws DPRPInvalidProtocolException
Sets the protocol, given a human-readable string.
Throws:
DPRPInvalidProtocolException - If the given string can't be matched to a known numeric representation.

getFlags

public short getFlags()
Returns the current value of the flags.

setFlags

public void setFlags(short f)
Sets the flags for this lease.

setFlags

public void setFlags(java.lang.String fStr)
              throws java.lang.NumberFormatException
Set the flags for this lease, given a string representation of a short.
Throws:
java.lang.NumberFormatException - If the string given can't be convereted to a numeric value.

getNackError

public short getNackError()
Deprecated. This will be removed, when I refactor the NACK stuff out of this class.

Returns the nack reason.

setNackError

public void setNackError(short r)
Sets the nack reason.

touchStartTime

public void touchStartTime()
Updates the startTime for this lease with the current wall-clock time.

setStartTime

public void setStartTime(java.util.Date t)
Sets the startTime to the Date object given.

getPrimaryKey

public java.lang.String getPrimaryKey()
Returns the "primary key" for this lease. This is a database term, which means the tuple that uniquely identifies an object. In this case, it is the conjunction of the external IP address, the external port, and the protocol, separated by colons.
Returns:
A string representing the primary key.

getNumRenews

public int getNumRenews()
Gets the number of times this lease has been renewed.

incNumRenews

public void incNumRenews()
Increments the number of times this lease has been renewed.

setNumRenews

public void setNumRenews(int i)
Sets the number of times this lease has been renewed.
Parameters:
i - The number of renews are set equal to this value.

setNumRenews

public void setNumRenews(java.lang.String s)
                  throws java.lang.NumberFormatException
Sets the number of times this lease has been renewed.
Parameters:
s - A string, representing an integer number.
Throws:
java.lang.NumberFormatException - If the string doesn't accurately represent an integer.

isExpired

public boolean isExpired()
This method checks to see if a lease has expired. This check is performed by adding the duration to the startTime, and checking this value against the current "wall clock" time.
Returns:
True if the lease has expired, false otherwise.

getStopTime

public java.util.Date getStopTime()
Determines when this lease expires.
Returns:
A Date instance, containing the exact time that this lease expires.

messageToClient

public DPRPMessage messageToClient(java.net.InetAddress sourceIP,
                                   int sourcePort)
Spits out a fully-formed DPRPMessage, addressed to the client.
Parameters:
sourceIP - The source IP address (server).
sourcePort - The source IP port (server).
Returns:
A fully-formed DPRPMessage based on this lease.

compareToMsg

public boolean compareToMsg(DPRPMessage msg)
Compares the parameters in the given message to those in this lease.
Parameters:
msg - A DPRPMessage, of which values certain fields will be compared to those in this lease.
Returns:
True if all values are identical, false otherwise.

equals

public boolean equals(java.lang.Object obj)
Overloading the exact equals() method that appears in Object.
Overrides:
equals in class java.lang.Object
Parameters:
obj - An object, to compare our values against, if it is in fact a DPRPLease instance.
Returns:
Returns the result of the localized equals() method.

equals

public boolean equals(DPRPLease cmp)
Localized method for determining the equality of two leases. I am being fairly loose about this -- if the two "primary keys" are the same, then the leases are said to be equivalent. They refer to the same external IP, port, and protocol, at least. They can have different start times, duration, flags, etc., however.
Parameters:
cmp - A DPRPLease, to compare our values against.
Returns:
True if the leases are equal, false otherwise.

hashCode

public int hashCode()
Returns an integer represenation of this lease, for use in hashing. Since I overloaded equals(), I also have to overload this method. Essentially, I just take the bits of the primary key, and smash them all together into a 32-bit quantity.
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Writes out a lease in a computer-parseable format. This format is suitable for an on-disk database of leases. Here is an example of the format:
   extIP:extPort:protocol|cliIP:cliPort|srvIP:srvPort|startTime|duration|flags|numRenews
 
Where extIP:extPort:protocol is the primary key.
Overrides:
toString in class java.lang.Object
Returns:
A string, uniquely representing the lease.

fromString

public static DPRPLease fromString(java.lang.String str)
Converts a string representation of a lease into a DPRPLease object.
Parameters:
str - A String representing a lease, to be converted.
Returns:
A DPRPLease object, with the fields matching those of the string.

parseLeaseString

private static java.util.Vector parseLeaseString(java.lang.String str,
                                                 char separator)
An internal method that breaks a string into components around the separator. I wrote this before I knew about java.util.StringTokenizer.
Parameters:
str - A String object to parse.
separator - A character that separates out the fields.
Returns:
A vector of each substring in between the separators.

prettyPrint

public void prettyPrint()
Prints a lease to stdout in a human-readable format.

readLeaseFile

public static java.util.HashSet readLeaseFile(java.io.File fname)
                                       throws java.io.IOException
Reads ASCII lease representations from disk, and returns them in a collection.
Parameters:
fname - The name of the file to be read.
Returns:
A collection of leases, as found on in the file.

writeLeaseFile

public static java.util.HashSet writeLeaseFile(java.io.File fname,
                                               java.util.HashSet leasesToWrite)
Writes a given collection of leases to disk, and returns the leases that were actually written. Because there is no explicit "lease garbage collector", leases are expired at file-write time. Thus, no guarantees are made that all of the leases given in the collection are actually written to disk.
Parameters:
fname - A File instance, containing the name and location of the file to be written.
leasesToWrite - A collection of leases to be written.
Returns:
A collection of the leases that actually were written.

writeLeaseFileComment

private static void writeLeaseFileComment(java.io.File fname)
                                   throws java.io.IOException
Writes out the standard comment block at the top of the on-disk lease database.
Throws:
java.io.IOException - If there was a problem writing to the file.