Subversion Repositories programming

Rev

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

Rev 223 Rev 224
Line -... Line 1...
-
 
1
/*******************************************************************************
-
 
2
 * File: P3_ServerThread.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
 ******************************************************************************/
1
 
12
 
2
import java.net.*;
13
import java.net.*;
3
import java.io.*;
14
import java.io.*;
4
 
15
 
5
public class P3_ServerThread extends Thread
16
public class P3_ServerThread extends Thread
6
{
17
{
-
 
18
    /* Instance Variables */
7
    private Socket socket = null;
19
    private Socket socket = null;
8
    private DHCPTable table = null;
20
    private DHCPTable table = null;
9
 
21
 
-
 
22
    /**
-
 
23
     * Method: P3_ServerThread constructor
-
 
24
     * Purpose: Construct a new thread that will handle dealing with a
-
 
25
     * DHCP client.
-
 
26
     */
10
    public P3_ServerThread (Socket socket, DHCPTable table)
27
    public P3_ServerThread (Socket socket, DHCPTable table)
11
    {
28
    {
12
        super ("P3_ServerThread");
29
        super ("P3_ServerThread");
13
        this.socket = socket;
30
        this.socket = socket;
14
        this.table = table;
31
        this.table = table;
15
    }
32
    }
16
 
33
 
-
 
34
    /**
-
 
35
     * Method: run
-
 
36
     * Purpose: Communicate with the client
-
 
37
     */
17
    public void run ()
38
    public void run ()
18
    {
39
    {
-
 
40
        BufferedReader in = null;
-
 
41
        PrintWriter out = null;
-
 
42
 
-
 
43
        // Set up communication to the server
19
        try
44
        try
20
        {
45
        {
21
            PrintWriter out = new PrintWriter (socket.getOutputStream(), true);
46
            out = new PrintWriter (socket.getOutputStream(), true);
22
            BufferedReader in = new BufferedReader (
47
            in = new BufferedReader (
23
                                    new InputStreamReader (
48
                     new InputStreamReader (
24
                                        socket.getInputStream()));
49
                         socket.getInputStream()));
-
 
50
        }
-
 
51
        catch (IOException ex)
-
 
52
        {
-
 
53
            ex.printStackTrace();
-
 
54
        }
25
 
55
        
26
            String inputLine, outputLine;
56
        String inputLine, outputLine;
27
            DHCPTableEntry e, expired;
57
        DHCPTableEntry e, expired;
-
 
58
        String[] splitInput = new String[1];
28
 
59
 
29
            // Try to get REQ message
60
        // Try to get "req IP <HWAddr>" message
-
 
61
        try
-
 
62
        {
30
            inputLine = in.readLine();
63
            inputLine = in.readLine();
31
            String[] splitInput = inputLine.split(" ");
64
            splitInput = inputLine.split(" ");
-
 
65
        }
-
 
66
        catch (IOException ex)
-
 
67
        {
-
 
68
            ex.printStackTrace();
32
            
69
        }
-
 
70
 
-
 
71
        // Make sure the request was ok
33
            if (splitInput.length != 3 && 
72
        if (splitInput.length != 3 &&
34
                !splitInput[0].equals("req") && 
73
            !splitInput[0].equals("req") &&
35
                !splitInput[1].equals("IP"))
74
            !splitInput[1].equals("IP"))
36
            {
75
        {
37
                System.err.println ("REQ was not the first message!");
76
            System.err.println ("REQ was not the first message!");
38
                out.println ("BAD REQUEST");
77
            out.println ("BAD REQUEST");
39
                return; // Leave uncleanly
78
            return; // Leave uncleanly
40
            }
79
        }
41
 
80
 
-
 
81
        // Get the next entry in the DHCPTable
42
            e = table.addEntry (splitInput[2]);
82
        e = table.addEntry (splitInput[2]);
43
 
83
 
44
            // Print out DHCP Server info
84
        // Print out DHCP Server info
45
            System.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());
85
        System.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());
46
 
86
 
47
            // Send out the client's IP Address
87
        // Send out the client's IP Address
48
            out.println ("IPAddr: " + e.getIPAddr());
88
        out.println ("IPAddr: " + e.getIPAddr());
-
 
89
 
-
 
90
        // wait for timeout of this entry
-
 
91
        expired = table.timeoutEntry (e.getIPAddr());
-
 
92
        while (expired == null)
49
            
93
        {
50
            expired = table.timeoutEntry (e.getIPAddr());
94
            expired = table.timeoutEntry (e.getIPAddr());
51
            while (expired == null)
-
 
52
            {
-
 
53
                expired = table.timeoutEntry (e.getIPAddr());
-
 
54
                // wait for timeout (accomplished above)
-
 
55
            }
95
        }
56
 
96
 
57
            // Print DHCP Server info
97
        // Print DHCP Server info
58
            System.out.printf ("Client Expired: IP=%s -- HW=%s\n", 
98
        System.out.printf ("Client Expired: IP=%s -- HW=%s\n",
59
                    expired.getIPAddr(), expired.getHWAddr());
99
                expired.getIPAddr(), expired.getHWAddr());
60
 
100
 
61
            // Tell the client that it expired            
101
        // Tell the client that it expired
62
            out.println ("IP Lease Time Expired");
102
        out.println ("IP Lease Time Expired");
63
 
103
 
-
 
104
        // Try to cleanup sockets
-
 
105
        try
-
 
106
        {
64
            out.close();
107
            out.close();
65
            in.close();
108
            in.close();
66
            socket.close();
109
            socket.close();
67
        }
110
        }
68
        catch (IOException e)
111
        catch (IOException ex)
69
        {
112
        {
70
            e.printStackTrace();
113
            ex.printStackTrace();
71
        }
114
        }
72
    }
115
    }
73
}
116
}
74
 
117