Rev 221 | Go to most recent revision | 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: //FIXME******************************************************************************/import java.util.Date;public class DHCPTable{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 defaultpublic DHCPTable (){int i;final String startStr = "134.71.24.";IPAddr ip = null;for (i=1; i<=table.length; i++){ip = new IPAddr (startStr + i);table[i-1] = new DHCPTableEntry("", ip, 0);}}private DHCPTableEntry getFirstFreeEntry (){int i = 0;for (i=0; i<table.length; i++)if (table[i].getLeaseStart() == 0)return table[i];throw new ArrayIndexOutOfBoundsException();}public DHCPTableEntry addEntry (String HWAddr){long curTime = new Date().getTime();DHCPTableEntry e = getFirstFreeEntry();e.setHWAddr (HWAddr);e.setLeaseStart (curTime);return e;}/*** Method: timeoutEntry()* Purpose: tries to find an expired entry. If it has found one, it expires it* and returns it, otherwise it returns null.*/public DHCPTableEntry timeoutEntry (){int i;long curTime = new Date().getTime();DHCPTableEntry e = null;for (i=0; i<table.length; i++)if (table[i].getLeaseStart() != 0 && curTime > (table[i].getLeaseStart() + timeoutMS)){e = new DHCPTableEntry (table[i].getHWAddr(),table[i].getIPAddr(),table[i].getLeaseStart());table[i].setHWAddr ("");table[i].setLeaseStart (0);return e;}return e; // will be null if an entry was not found}public DHCPTableEntry timeoutEntry (IPAddr ip){int i = ip.getField(3) - 1;long curTime = new Date().getTime();DHCPTableEntry e = null;if (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;}}