Rev 224 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/******************************************************************************** File: DHCPTable.java* Author: Ira W. Snyder (devel@irasnyder.com)* License: GNU General Public License v2* Class: CS380 - Computer Networking** Assignment: Project #3* Date Last Modified: 2006-02-15** Purpose: Implement a DHCPTable, which will give out addresses in the range* 134.71.24.1 - 134.71.24.100. It always gives out the lowest available* address in the range. Addresses time out after 8 seconds.******************************************************************************/import java.util.Date;public class DHCPTable{/* Instance Variables */private DHCPTableEntry[] table = new DHCPTableEntry[100];private IPAddr lowestIP = new IPAddr("134.71.24.1");private IPAddr highestIP = new IPAddr("134.71.24.100");private long timeoutMS = 8000; // 8000 millisec by default/*** Method: DHCPTable constructor* Purpose: Construct a DHCPTable entry*/public DHCPTable (){int i;final String startStr = "134.71.24.";IPAddr ip = null;// Add every possible IP Address to the tablefor (i=1; i<=table.length; i++){ip = new IPAddr (startStr + i);table[i-1] = new DHCPTableEntry("", ip, 0);}}/*** Method: getFirstFreeEntry* Purpose: Get the first free entry in the table*/private DHCPTableEntry getFirstFreeEntry (){int i = 0;// Simple sequential search for an empty entryfor (i=0; i<table.length; i++)if (table[i].getLeaseStart() == 0)return table[i];throw new ArrayIndexOutOfBoundsException();}/*** Method: addEntry* Purpose: Add an entry to the table for a given Hardware Address.* This returns the table entry, so that all of it's information can be used.*/public DHCPTableEntry addEntry (String HWAddr){long curTime = new Date().getTime();DHCPTableEntry e = getFirstFreeEntry();e.setHWAddr (HWAddr);e.setLeaseStart (curTime);return e;}/*** Method: timeoutEntry* Purpose: See if the entry for the given IP Address has timed out,* return a copy of the entry if it has expired, or return null if we* have not timed out yet.*/public DHCPTableEntry timeoutEntry (IPAddr ip){int i = ip.getField(3) - 1;long curTime = new Date().getTime();DHCPTableEntry e = null;// Check if it has expiredif (curTime > (table[i].getLeaseStart() + timeoutMS)){// Make a copy of the objecte = new DHCPTableEntry (table[i].getHWAddr(),table[i].getIPAddr(),table[i].getLeaseStart());table[i].setHWAddr ("");table[i].setLeaseStart (0);return e;}return null;}}