// MultiServerThread.java
// when a new connection is established, this thread is started

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

public class MultiServerThread extends Thread {
  private Socket socket = null;
  Space space;
  
  public MultiServerThread (Socket socket, Space space) {
    super ("MultiServerThread");
    this.socket = socket;
    this.space = space;
  }
  
  synchronized public void run() {
    System.err.println ("somebody connected...");
    try {
      PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
      BufferedReader in = new BufferedReader(
        new InputStreamReader(
          socket.getInputStream()));
      
      String inputLine, outputLine;
      Protocol protocol = new Protocol();

      // ask client what type of service they want
      while ((inputLine = in.readLine()) == null);

      outputLine = protocol.processInput (inputLine, space);
      out.println (outputLine);
      System.err.println (outputLine);

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