private PDBTEW2Listener(PDBTExecSingleCltWrkInitSrv srv, Socket s) throws IOException { _srv = srv; _s = s; _ois = new ObjectInputStream(_s.getInputStream()); _oos = new ObjectOutputStream(_s.getOutputStream()); _oos.flush(); }
/** * there is an issue with this method: if it is called often enough, the <CODE> * _s.sendUrgentData(0);</CODE> method that it invokes, will force the <CODE>_s</CODE> socket to * close on the other end, at least on Windows systems. This behavior is due to the fact that * OOB data handling is problematic since there are conflicting specifications in TCP. * Therefore, it is required that the method is not called with high frequency (see the <CODE> * PDBTExecSingleCltWrkInitSrv._CHECK_PERIOD_MSECS</CODE> flag in this file.) * * @return true iff the worker is available to accept work according to all evidence. */ private synchronized boolean getAvailability() { boolean res = _isAvail && _s != null && _s.isClosed() == false; if (res && _OK2SendOOB) { // work-around the OOB data issue // last test using OOB sending of data try { _OK2SendOOB = false; // indicates should not send OOB data until set to true _s.sendUrgentData(0); // unfortunately, if this method is called often enough, // it will cause the socket to close??? res = true; } catch (IOException e) { // e.printStackTrace(); utils.Messenger.getInstance() .msg("PDBTExecSingleCltWrkInitSrv.getAvailability(): Socket has been closed", 0); res = false; _isAvail = false; // declare availability to false as well // try graceful exit try { _s.shutdownOutput(); _s.close(); // Now we can close the Socket } catch (IOException e2) { // silently ignore } } } return res; }
private void processException(Exception e) { // e.printStackTrace(); try { _ois.close(); _oos.close(); _s.close(); } catch (Exception e2) { // e2.printStackTrace(); } finally { synchronized (_srv) { getWorkers().remove(_s); utils.Messenger.getInstance() .msg("PDBTExecSingleCltWrkInitSrv: Worker Network Connection Closed", 0); } _ois = null; _oos = null; _s = null; } }
private boolean isConnectionLost() { return _s == null || _s.isClosed(); }
/** * 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); } } }