Subversion Repositories programming

Rev

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


import java.net.*;
import java.io.*;

public class P3_ServerThread extends Thread
{
    private Socket socket = null;
    private DHCPTable table = null;

    public P3_ServerThread (Socket socket, DHCPTable table)
    {
        super ("P3_ServerThread");
        this.socket = socket;
        this.table = table;
    }

    public void run ()
    {
        try
        {
            PrintWriter out = new PrintWriter (socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader (
                                    new InputStreamReader (
                                        socket.getInputStream()));

            String inputLine, outputLine;
            DHCPTableEntry e, expired;

            // Try to get REQ message
            inputLine = in.readLine();
            String[] splitInput = inputLine.split(" ");
            
            if (splitInput.length != 3 && 
                !splitInput[0].equals("req") && 
                !splitInput[1].equals("IP"))
            {
                System.err.println ("REQ was not the first message!");
                out.println ("BAD REQUEST");
                return; // Leave uncleanly
            }

            e = table.addEntry (splitInput[2]);

            // Print out DHCP Server info
            System.out.printf ("Client Added: IP=%s -- HW=%s\n", e.getIPAddr(), e.getHWAddr());

            // Send out the client's IP Address
            out.println ("IPAddr: " + e.getIPAddr());
            
            expired = table.timeoutEntry (e.getIPAddr());
            while (expired == null)
            {
                expired = table.timeoutEntry (e.getIPAddr());
                // wait for timeout (accomplished above)
            }

            // Print DHCP Server info
            System.out.printf ("Client Expired: IP=%s -- HW=%s\n", 
                    expired.getIPAddr(), expired.getHWAddr());

            // Tell the client that it expired            
            out.println ("IP Lease Time Expired");

            out.close();
            in.close();
            socket.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}