/**
  * simply calls the method <CODE>t.runObject(req)</CODE> wrapped in some debug messages, and
  * returns the result of the call.
  *
  * @param req TaskObjectsExecutionRequest
  * @param t PDBTEW2Listener
  * @return TaskObjectsExecutionResults
  * @throws IOException
  * @throws PDBatchTaskExecutorException
  */
 private TaskObjectsExecutionResults submitWork(TaskObjectsExecutionRequest req, PDBTEW2Listener t)
     throws IOException, PDBatchTaskExecutorException {
   utils.Messenger.getInstance()
       .msg("PDBTExecSingleCltWrkInitSrv.submitWork(req,t): sending request", 1);
   TaskObjectsExecutionResults res = t.runObject(req);
   utils.Messenger.getInstance()
       .msg("PDBTExecSingleCltWrkInitSrv.submitWork(req,t): response received", 1);
   return res;
 }
 /**
  * adds a new worker to the network.
  *
  * @param s Socket
  * @throws IOException
  */
 private synchronized void addNewWorkerConnection(Socket s) throws IOException {
   PDBTEW2Listener lt = new PDBTEW2Listener(this, s);
   lt.init();
   getWorkers().put(s, lt);
 }
 TaskObjectsExecutionResults submitWork(Vector originating_clients, TaskObject[] tasks)
     throws IOException, ClassNotFoundException, PDBatchTaskExecutorException {
   Set workers2rm = new HashSet(); // Set<PDBTEW2Listener>
   PDBTEW2Listener t = null;
   utils.Messenger.getInstance()
       .msg(
           "PDBTExecSingleCltWrkInitSrv.submitWork(clts,tasks): "
               + "finding an available worker connection among "
               + getNumWorkers()
               + " known workers",
           1);
   // 1. find a worker (via Round-Robin)
   synchronized (this) {
     boolean cont = true;
     while (cont) {
       workers2rm.clear();
       Iterator sit = getWorkers().keySet().iterator();
       while (sit.hasNext()) {
         Socket s = (Socket) sit.next();
         t = (PDBTEW2Listener) getWorkers().get(s);
         boolean is_avail = t.getAvailability() && !getWorking().contains(t);
         // the _working set is needed so that submitWork() is guaranteed not
         // to choose twice the same worker before the worker is done with the
         // first request.
         if (is_avail) {
           cont = false; // break out of the top-level while-loop too
           getWorking().add(t);
           break;
         } else {
           if (t.isConnectionLost()) workers2rm.add(t);
           t = null; // reset to null
         }
       }
       // remove any "lost connections" worker listeners
       Iterator it = workers2rm.iterator();
       while (it.hasNext()) getWorkers().remove(it.next());
       if (t == null) {
         // before trying again to find an available worker, wait a while
         try {
           wait(_CHECK_PERIOD_MSECS); // wait out the workers for some time
         } catch (InterruptedException e) {
           // e.printStackTrace();
           Thread.currentThread().interrupt(); // recommended
         }
       }
     }
   } // synchronized (this)
   if (t == null) { // failed to find an available thread
     utils.Messenger.getInstance()
         .msg("PDBTExecSingleCltWrkInitSrv.submitWork(clt,tasks): no available threads...", 1);
     throw new PDBatchTaskExecutorException("no available worker or known srv to undertake work");
   }
   utils.Messenger.getInstance()
       .msg("PDBTExecSingleCltWrkInitSrv.submitWork(clt,tasks): found an available worker", 1);
   // 2. submit tasks and get back results
   TaskObjectsExecutionRequest req = new TaskObjectsExecutionRequest(originating_clients, tasks);
   utils.Messenger.getInstance()
       .msg(
           "PDBTExecSingleCltWrkInitSrv.submitWork(clt,tasks): created the TaskObjectsExecutionRequest to send",
           1);
   RRObject res = submitWork(req, t);
   utils.Messenger.getInstance()
       .msg(
           "PDBTExecSingleCltWrkInitSrv.submitWork(tasks): finished running submitWork(req,ois,oos)",
           1);
   synchronized (this) {
     getWorking().remove(t); // declare worker's availability again
     notifyAll(); // declare I'm done. With only a single client connected, not of much use.
   }
   if (res instanceof TaskObjectsExecutionResults) return (TaskObjectsExecutionResults) res;
   else {
     throw new PDBatchTaskExecutorException(
         "PDBTExecSingleCltWrkInitSrv.submitWork(tasks): worker failed to process tasks.");
   }
 }