예제 #1
1
  private void loop() {
    while (thread == Thread.currentThread()) {
      try {
        Request key = peekKey();
        if (key == null) throw new IOException("end of stream");
        synchronized (response_map) {
          Response response = (Response) response_map.get(key);
          if (response == null) {
            if (log.level >= 4) log.println("Invalid key, skipping message");
            doSkip();
          } else {
            doRecv(response);
            response.isReceived = true;
            response_map.notifyAll();
          }
        }
      } catch (Exception ex) {
        String msg = ex.getMessage();
        boolean timeout = msg != null && msg.equals("Read timed out");
        /* If just a timeout, try to disconnect gracefully
         */
        boolean hard = timeout == false;

        if (!timeout && log.level >= 3) ex.printStackTrace(log);

        try {
          disconnect(hard);
        } catch (IOException ioe) {
          ioe.printStackTrace(log);
        }
      }
    }
  }
예제 #2
0
 public void sendrecv(Request request, Response response, long timeout) throws IOException {
   synchronized (response_map) {
     makeKey(request);
     response.isReceived = false;
     try {
       response_map.put(request, response);
       doSend(request);
       response.expiration = System.currentTimeMillis() + timeout;
       while (!response.isReceived) {
         response_map.wait(timeout);
         timeout = response.expiration - System.currentTimeMillis();
         if (timeout <= 0) {
           throw new TransportException(name + " timedout waiting for response to " + request);
         }
       }
     } catch (IOException ioe) {
       if (log.level > 2) ioe.printStackTrace(log);
       try {
         disconnect(true);
       } catch (IOException ioe2) {
         ioe2.printStackTrace(log);
       }
       throw ioe;
     } catch (InterruptedException ie) {
       throw new TransportException(ie);
     } finally {
       response_map.remove(request);
     }
   }
 }