Subversion Repositories programming

Rev

Rev 221 | Rev 224 | 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
{
222 ira 17
    private DHCPTableEntry[] table = new DHCPTableEntry[100];
18
    private IPAddr lowestIP = new IPAddr("134.71.24.1");
19
    private IPAddr highestIP = new IPAddr("134.71.24.100");
20
    private long timeoutMS = 8000; // 8000 millisec by default
221 ira 21
 
222 ira 22
    public DHCPTable ()
23
    {
24
        int i;
25
        final String startStr = "134.71.24.";
26
        IPAddr ip = null;
221 ira 27
 
222 ira 28
        for (i=1; i<=table.length; i++)
29
        {
30
            ip = new IPAddr (startStr + i);
31
            table[i-1] = new DHCPTableEntry("", ip, 0);
32
        }
33
    }
34
 
35
    private DHCPTableEntry getFirstFreeEntry ()
221 ira 36
    {
37
        int i = 0;
38
 
222 ira 39
        for (i=0; i<table.length; i++)
40
            if (table[i].getLeaseStart() == 0)
41
                return table[i];
221 ira 42
 
222 ira 43
        throw new ArrayIndexOutOfBoundsException();
221 ira 44
    }
45
 
222 ira 46
    public DHCPTableEntry addEntry (String HWAddr)
47
    {
48
        long curTime = new Date().getTime();
49
        DHCPTableEntry e = getFirstFreeEntry();
50
 
51
        e.setHWAddr (HWAddr);
52
        e.setLeaseStart (curTime);
53
 
54
        return e;
55
    }
56
 
221 ira 57
    /**
222 ira 58
     * Method: timeoutEntry()
59
     * Purpose: tries to find an expired entry. If it has found one, it expires it
60
     * and returns it, otherwise it returns null.
221 ira 61
     */
222 ira 62
    public DHCPTableEntry timeoutEntry ()
221 ira 63
    {
222 ira 64
        int i;
65
        long curTime = new Date().getTime();
66
        DHCPTableEntry e = null;
67
 
68
        for (i=0; i<table.length; i++)
69
            if (table[i].getLeaseStart() != 0 && curTime > (table[i].getLeaseStart() + timeoutMS))
70
            {
71
                e = new DHCPTableEntry (table[i].getHWAddr(),
72
                                        table[i].getIPAddr(),
73
                                        table[i].getLeaseStart());
74
 
75
                table[i].setHWAddr ("");
76
                table[i].setLeaseStart (0);
77
                return e;
78
            }
79
 
80
        return e; // will be null if an entry was not found
81
    }
82
 
83
    public DHCPTableEntry timeoutEntry (IPAddr ip)
221 ira 84
    {
222 ira 85
        int i = ip.getField(3) - 1;
86
        long curTime = new Date().getTime();
87
        DHCPTableEntry e = null;
88
 
89
        if (curTime > (table[i].getLeaseStart() + timeoutMS))
221 ira 90
        {
222 ira 91
            // Make a copy of the object
92
            e = new DHCPTableEntry (table[i].getHWAddr(),
93
                                    table[i].getIPAddr(),
94
                                    table[i].getLeaseStart());
221 ira 95
 
222 ira 96
            table[i].setHWAddr ("");
97
            table[i].setLeaseStart (0);
98
            return e;
221 ira 99
        }
100
 
222 ira 101
        return null;
221 ira 102
    }
103
}
104