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

/**
   This program implements a simple server that listens to
   port 8189 and echoes back all client input.
*/
public class LightServer
{  
    static short datum;
    static short Addr=0x378;
    static pPort lpt;
    
   public static void main(String[] args )
   {  
      try
      {  
         // establish server socket
         ServerSocket s = new ServerSocket(8189);

         // wait for client connection
         Socket incoming = s.accept( );
         BufferedReader in = new BufferedReader
            (new InputStreamReader(incoming.getInputStream()));
         PrintWriter out = new PrintWriter
            (incoming.getOutputStream(), true /* autoFlush */);

         out.println( "Hello! Enter BYE to exit." );
         lpt = new pPort();

         // echo client input
         boolean done = false;
         while (!done)
         {  
            String line = in.readLine();
            if (line == null) done = true;
            else
            {  
               System.out.println("Echo: " + line);
               
               if (line.trim().equals("ON")){
                  datum=0xFF;
                  do_write();
               }
               
               if (line.trim().equals("OFF")){
                  datum=0x00;
                  do_write();
               }
               
               if (line.trim().equals("BYE"))
                  done = true;
            }
         }
         incoming.close();
      }
      catch (Exception e)
      {  
         e.printStackTrace();
      }
   }
   static void do_write()
     {
          // Notify the console
          System.out.println("Write to Port: " + Integer.toHexString(Addr) +
                              " with data = " +  Integer.toHexString(datum));
          //Write to the port
          lpt.output(Addr,datum);
     }
}


