Subversion Repositories programming

Rev

Rev 222 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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