package socketservertest;
import java.net.*;
import java.lang.Runtime;

public class ServerThread extends Thread {

  public ServerThread(Socket aSocket) {
    socket = aSocket;
  }


private Socket socket;

  /**
   * When an object implementing interface <code>Runnable</code> is used to
   * create a thread, starting the thread causes the object's <code>run</code>
   * method to be called in that separately executing thread.
   *
   * @todo Implement this java.lang.Runnable method
   */
  public void run () {
  try {
    Runtime rt = Runtime.getRuntime();
    java.io.BufferedReader reader =
      new java.io.BufferedReader(
        new java.io.InputStreamReader(socket.getInputStream()));
    java.io.PrintWriter writer =
      new java.io.PrintWriter (socket.getOutputStream(),true);
    while (this.isAlive()) {
      String data = reader.readLine();

      // Exit the server...
      if (data.equals("quit") || data.equals("exit")) {
        System.out.println("MIDI-server stopping...");
        rt.exec("killall timidity");
      System.exit(0);
      break; }


    // Start a new MID...
    try{
           rt.exec("killall timidity");
           rt.exec("timidity " + data);
          }catch(Exception e){
            System.out.println(e);
          }


      //writer.println("<"+System.getProperty("user.name")+"> Hello, "+data);
    }
    } catch (Exception ex) {
    }
  }
}
