Subversion Repositories programming

Rev

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

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