/**
  * invoke as: <CODE>
  * java -cp &lt;classpath&gt; parallel.distributed.PDBTExecSingleCltWrkInitSrv [workers_port(7890)] [client_port(7891)]
  * </CODE>
  *
  * @param args String[]
  */
 public static void main(String[] args) {
   int wport = 7890; // default port
   int cport = 7891;
   if (args.length > 0) {
     try {
       wport = Integer.parseInt(args[0]);
     } catch (Exception e) {
       e.printStackTrace();
       usage();
       System.exit(-1);
     }
     if (args.length > 1) {
       try {
         cport = Integer.parseInt(args[1]);
       } catch (Exception e) {
         e.printStackTrace();
         usage();
         System.exit(-1);
       }
     }
   }
   PDBTExecSingleCltWrkInitSrv server = new PDBTExecSingleCltWrkInitSrv(wport, cport);
   try {
     server.run();
   } catch (Exception e) {
     e.printStackTrace();
     System.err.println("Server exits due to exception.");
   }
 }
 /**
  * creates a server socket listening on the port specified in the parameter of the constructor,
  * and waits for a single incoming client connection which it handles by invoking the <CODE>
  * addNewClientConnection(s)</CODE> method of the enclosing server, and then the thread exits.
  */
 public void run() {
   try {
     ServerSocket ss = new ServerSocket(_port);
     System.out.println("Srv: Now Accepting Single Client Connection");
     // while (true) {
     try {
       Socket s = ss.accept();
       System.out.println("Srv: Client Added to the Network");
       addNewClientConnection(s);
       System.out.println("Srv: finished adding client connection");
     } catch (Exception e) {
       // e.printStackTrace();
       System.err.println("Client Connection failed, exiting...");
       System.exit(-1);
     }
     // }
   } catch (IOException e) {
     // e.printStackTrace();
     utils.Messenger.getInstance()
         .msg(
             "PDBTExecSingleCltWrkInitSrv.C2Thread.run(): "
                 + "Failed to create Server Socket, Server exiting.",
             0);
     System.exit(-1);
   }
 }
 /**
  * creates a server socket listening on port specified in the object constructor, and then
  * enters an infinite loop waiting for incoming socket connection requests representing a worker
  * process attempting to connect to this server, which it handles via the enclosing server's
  * <CODE>addNewWorkerConnection(s)</CODE> method.
  */
 public void run() {
   try {
     ServerSocket ss = new ServerSocket(_port);
     System.out.println("Srv: Now Accepting Worker Connections");
     while (true) {
       try {
         Socket s = ss.accept();
         System.out.println("Srv: Incoming New Worker Connection to the Network");
         System.out.println(
             "Srv: Thread may have to wait if an init_cmd has not yet arrived from the client");
         addNewWorkerConnection(s);
         System.out.println("Srv: finished adding new worker connection to the _workers");
       } catch (Exception e) {
         utils.Messenger.getInstance()
             .msg(
                 "PDBTExecSingleCltWrkInitSrv.W2Thread.run(): "
                     + "An error occured while adding new worker connection",
                 2);
         // e.printStackTrace();
       }
     }
   } catch (IOException e) {
     // e.printStackTrace();
     utils.Messenger.getInstance()
         .msg(
             "PDBTExecSingleCltWrkInitSrv.W2Thread.run(): "
                 + "Failed to create Server Socket, Server exiting.",
             0);
     System.exit(-1);
   }
 }
 /**
  * reads from the input stream the initialization command sent to it, sends back to the client
  * an <CODE>OKReply</CODE> "ACK" msg, and then enters an infinite loop waiting to read from the
  * input stream an <CODE>RRObject</CODE> obj that should really be of type <CODE>
  * TaskObjectsExecutionRequest</CODE>, on which it executes its method <CODE>
  * obj.runProtocol(_srv,_ois, _oos)</CODE>.
  */
 public void run() {
   // first, read from socket the worker-initialization object that will
   // be broadcast for execution to every worker connecting to this server.
   utils.Messenger mger = utils.Messenger.getInstance();
   try {
     mger.msg("PDBTEC2ListenerThread: waiting to read the init_cmd from client", 1);
     RRObject initCmd = (RRObject) _ois.readObject();
     mger.msg("PDBTEC2ListenerThread: done reading the init_cmd from client", 1);
     setInitCmd(initCmd);
     mger.msg("PDBTEC2ListenerThread: done setting the init_cmd", 2);
     // send back to the clt an "ACK" message
     _oos.writeObject(new OKReply());
     _oos.flush();
     mger.msg("PDBTEC2ListenerThread: done sending OKReply through the socket", MIN_PRIORITY);
   } catch (Exception e) { // client closed connection
     // e.printStackTrace();
     try {
       _ois.close();
       _oos.close();
       _s.close();
     } catch (Exception e2) {
       // e2.printStackTrace();
     }
     mger.msg("PDBTEC2ListenerThread: Client Network Connection Closed", 0);
     System.exit(-1);
   }
   while (true) {
     try {
       mger.msg("PDBTEC2ListenerThread.run(): waiting to read an RRObject...", 2);
       // 1. read from socket input
       RRObject obj = (RRObject) _ois.readObject(); // obj is an TaskObjectsExecutionRequest
       mger.msg("PDBTEC2ListenerThread.run(): RRObject read", 2);
       // 2. take appropriate action
       try {
         obj.runProtocol(_srv, _ois, _oos);
       } catch (PDBatchTaskExecutorException e) {
         mger.msg(
             "PDBTEC2ListenerThread.run(): calling obj.runProtocol() "
                 + "issued PDBatchTaskExecutorException, will try one more time.",
             1);
         secondChance(obj);
       } catch (
           IOException e) { // worker somehow failed, give srv one more shot, then notify client
         mger.msg(
             "PDBTEC2ListenerThread.run(): calling obj.runProtocol() "
                 + "issued IOException, will try one more time.",
             1);
         secondChance(obj);
       }
     } catch (Exception e) { // client closed connection
       // e.printStackTrace();
       try {
         _ois.close();
         _oos.close();
         _s.close();
       } catch (Exception e2) {
         // e2.printStackTrace();
       }
       mger.msg(
           "PDBTExecSingleCltWrkInitSrv: Client Network Connection Closed "
               + "(exception '"
               + e
               + "' caught)",
           0);
       System.exit(-1);
     }
   }
 }