Subversion Repositories programming

Rev

Rev 223 | Go to most recent revision | Details | 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();
31
 
32
            if (!inputLine.equals("req"))
33
                System.err.println ("REQ was not the first message!");
34
 
35
            // Try to get HW Address
36
            String HWAddr = in.readLine();
37
            e = table.addEntry (HWAddr);
38
 
39
            // Print out DHCP Server info
40
            System.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());
41
 
42
            // Send out the client's IP Address
43
            out.println ("IPAddr: " + e.getIPAddr());
44
 
45
            expired = table.timeoutEntry (e.getIPAddr());
46
            while (expired == null)
47
            {
48
                expired = table.timeoutEntry (e.getIPAddr());
49
                // wait for timeout (accomplished above)
50
            }
51
 
52
            // Print DHCP Server info
53
            System.out.printf ("Client Expired: IP=%s -- HW=%s\n", 
54
                    expired.getIPAddr(), expired.getHWAddr());
55
 
56
            // Tell the client that it expired            
57
            out.println ("IP Lease Time Expired");
58
 
59
            out.close();
60
            in.close();
61
            socket.close();
62
        }
63
        catch (IOException e)
64
        {
65
            e.printStackTrace();
66
        }
67
    }
68
}
69