Subversion Repositories programming

Rev

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