Subversion Repositories programming

Rev

Rev 222 | Rev 224 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
222 ira 1
 
2
import java.net.*;
3
import java.io.*;
4
 
5
public class P3_ServerThread extends Thread
6
{
7
    private Socket socket = null;
8
    private DHCPTable table = null;
9
 
10
    public P3_ServerThread (Socket socket, DHCPTable table)
11
    {
12
        super ("P3_ServerThread");
13
        this.socket = socket;
14
        this.table = table;
15
    }
16
 
17
    public void run ()
18
    {
19
        try
20
        {
21
            PrintWriter out = new PrintWriter (socket.getOutputStream(), true);
22
            BufferedReader in = new BufferedReader (
23
                                    new InputStreamReader (
24
                                        socket.getInputStream()));
25
 
26
            String inputLine, outputLine;
27
            DHCPTableEntry e, expired;
28
 
29
            // Try to get REQ message
30
            inputLine = in.readLine();
223 ira 31
            String[] splitInput = inputLine.split(" ");
32
 
33
            if (splitInput.length != 3 && 
34
                !splitInput[0].equals("req") && 
35
                !splitInput[1].equals("IP"))
36
            {
222 ira 37
                System.err.println ("REQ was not the first message!");
223 ira 38
                out.println ("BAD REQUEST");
39
                return; // Leave uncleanly
40
            }
222 ira 41
 
223 ira 42
            e = table.addEntry (splitInput[2]);
222 ira 43
 
44
            // Print out DHCP Server info
45
            System.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());
46
 
47
            // Send out the client's IP Address
48
            out.println ("IPAddr: " + e.getIPAddr());
49
 
50
            expired = table.timeoutEntry (e.getIPAddr());
51
            while (expired == null)
52
            {
53
                expired = table.timeoutEntry (e.getIPAddr());
54
                // wait for timeout (accomplished above)
55
            }
56
 
57
            // Print DHCP Server info
58
            System.out.printf ("Client Expired: IP=%s -- HW=%s\n", 
59
                    expired.getIPAddr(), expired.getHWAddr());
60
 
61
            // Tell the client that it expired            
62
            out.println ("IP Lease Time Expired");
63
 
64
            out.close();
65
            in.close();
66
            socket.close();
67
        }
68
        catch (IOException e)
69
        {
70
            e.printStackTrace();
71
        }
72
    }
73
}
74