Subversion Repositories programming

Rev

Rev 222 | Go to most recent revision | 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
 *
10
 * Purpose: //FIXME
11
 ******************************************************************************/
12
 
222 ira 13
import java.util.Date;
221 ira 14
 
15
public class DHCPTable
16
{
224 ira 17
    /* Instance Variables */
222 ira 18
    private DHCPTableEntry[] table = new DHCPTableEntry[100];
19
    private IPAddr lowestIP = new IPAddr("134.71.24.1");
20
    private IPAddr highestIP = new IPAddr("134.71.24.100");
21
    private long timeoutMS = 8000; // 8000 millisec by default
221 ira 22
 
224 ira 23
    /**
24
     * Method: DHCPTable constructor
25
     * Purpose: Construct a DHCPTable entry
26
     */
222 ira 27
    public DHCPTable ()
28
    {
29
        int i;
30
        final String startStr = "134.71.24.";
31
        IPAddr ip = null;
221 ira 32
 
224 ira 33
        // Add every possible IP Address to the table
222 ira 34
        for (i=1; i<=table.length; i++)
35
        {
36
            ip = new IPAddr (startStr + i);
37
            table[i-1] = new DHCPTableEntry("", ip, 0);
38
        }
39
    }
40
 
224 ira 41
    /**
42
     * Method: getFirstFreeEntry
43
     * Purpose: Get the first free entry in the table
44
     */
222 ira 45
    private DHCPTableEntry getFirstFreeEntry ()
221 ira 46
    {
47
        int i = 0;
48
 
224 ira 49
        // Simple sequential search for an empty entry
222 ira 50
        for (i=0; i<table.length; i++)
51
            if (table[i].getLeaseStart() == 0)
52
                return table[i];
221 ira 53
 
222 ira 54
        throw new ArrayIndexOutOfBoundsException();
221 ira 55
    }
56
 
224 ira 57
    /**
58
     * Method: addEntry
59
     * Purpose: Add an entry to the table for a given Hardware Address.
60
     * This returns the table entry, so that all of it's information can be used.
61
     */
222 ira 62
    public DHCPTableEntry addEntry (String HWAddr)
63
    {
64
        long curTime = new Date().getTime();
65
        DHCPTableEntry e = getFirstFreeEntry();
66
 
67
        e.setHWAddr (HWAddr);
68
        e.setLeaseStart (curTime);
69
 
70
        return e;
71
    }
72
 
221 ira 73
    /**
224 ira 74
     * Method: timeoutEntry
75
     * Purpose: See if the entry for the given IP Address has timed out,
76
     * return a copy of the entry if it has expired, or return null if we
77
     * have not timed out yet.
221 ira 78
     */
222 ira 79
    public DHCPTableEntry timeoutEntry (IPAddr ip)
221 ira 80
    {
222 ira 81
        int i = ip.getField(3) - 1;
82
        long curTime = new Date().getTime();
83
        DHCPTableEntry e = null;
84
 
224 ira 85
        // Check if it has expired
222 ira 86
        if (curTime > (table[i].getLeaseStart() + timeoutMS))
221 ira 87
        {
222 ira 88
            // Make a copy of the object
89
            e = new DHCPTableEntry (table[i].getHWAddr(),
90
                                    table[i].getIPAddr(),
91
                                    table[i].getLeaseStart());
221 ira 92
 
222 ira 93
            table[i].setHWAddr ("");
94
            table[i].setLeaseStart (0);
95
            return e;
221 ira 96
        }
97
 
222 ira 98
        return null;
221 ira 99
    }
100
}
101