Subversion Repositories programming

Rev

Rev 224 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
221 ira 1
/*******************************************************************************
2
 * File: DHCPTable.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * License: GNU General Public License v2
5
 * Class: CS380 - Computer Networking
6
 *
7
 * Assignment: Project #3
8
 * Date Last Modified: 2006-02-15
9
 *
260 ira 10
 * Purpose: Implement a DHCPTable, which will give out addresses in the range
11
 * 134.71.24.1 - 134.71.24.100. It always gives out the lowest available
12
 * address in the range. Addresses time out after 8 seconds.
221 ira 13
 ******************************************************************************/
14
 
222 ira 15
import java.util.Date;
221 ira 16
 
17
public class DHCPTable
18
{
224 ira 19
    /* Instance Variables */
222 ira 20
    private DHCPTableEntry[] table = new DHCPTableEntry[100];
21
    private IPAddr lowestIP = new IPAddr("134.71.24.1");
22
    private IPAddr highestIP = new IPAddr("134.71.24.100");
23
    private long timeoutMS = 8000; // 8000 millisec by default
221 ira 24
 
224 ira 25
    /**
26
     * Method: DHCPTable constructor
27
     * Purpose: Construct a DHCPTable entry
28
     */
222 ira 29
    public DHCPTable ()
30
    {
31
        int i;
32
        final String startStr = "134.71.24.";
33
        IPAddr ip = null;
221 ira 34
 
224 ira 35
        // Add every possible IP Address to the table
222 ira 36
        for (i=1; i<=table.length; i++)
37
        {
38
            ip = new IPAddr (startStr + i);
39
            table[i-1] = new DHCPTableEntry("", ip, 0);
40
        }
41
    }
42
 
224 ira 43
    /**
44
     * Method: getFirstFreeEntry
45
     * Purpose: Get the first free entry in the table
46
     */
222 ira 47
    private DHCPTableEntry getFirstFreeEntry ()
221 ira 48
    {
49
        int i = 0;
50
 
224 ira 51
        // Simple sequential search for an empty entry
222 ira 52
        for (i=0; i<table.length; i++)
53
            if (table[i].getLeaseStart() == 0)
54
                return table[i];
221 ira 55
 
222 ira 56
        throw new ArrayIndexOutOfBoundsException();
221 ira 57
    }
58
 
224 ira 59
    /**
60
     * Method: addEntry
61
     * Purpose: Add an entry to the table for a given Hardware Address.
62
     * This returns the table entry, so that all of it's information can be used.
63
     */
222 ira 64
    public DHCPTableEntry addEntry (String HWAddr)
65
    {
66
        long curTime = new Date().getTime();
67
        DHCPTableEntry e = getFirstFreeEntry();
68
 
69
        e.setHWAddr (HWAddr);
70
        e.setLeaseStart (curTime);
71
 
72
        return e;
73
    }
74
 
221 ira 75
    /**
224 ira 76
     * Method: timeoutEntry
77
     * Purpose: See if the entry for the given IP Address has timed out,
78
     * return a copy of the entry if it has expired, or return null if we
79
     * have not timed out yet.
221 ira 80
     */
222 ira 81
    public DHCPTableEntry timeoutEntry (IPAddr ip)
221 ira 82
    {
222 ira 83
        int i = ip.getField(3) - 1;
84
        long curTime = new Date().getTime();
85
        DHCPTableEntry e = null;
86
 
224 ira 87
        // Check if it has expired
222 ira 88
        if (curTime > (table[i].getLeaseStart() + timeoutMS))
221 ira 89
        {
222 ira 90
            // Make a copy of the object
91
            e = new DHCPTableEntry (table[i].getHWAddr(),
92
                                    table[i].getIPAddr(),
93
                                    table[i].getLeaseStart());
221 ira 94
 
222 ira 95
            table[i].setHWAddr ("");
96
            table[i].setLeaseStart (0);
97
            return e;
221 ira 98
        }
99
 
222 ira 100
        return null;
221 ira 101
    }
102
}
103