public T receive() {
   // in case one wants to synchronize this method,
   // bear in mind that it is blocking so it cannot share the same lock as others
   T result = delegate.receive();
   LOGGER.debug(
       "thread {}: received {}",
       Thread.currentThread().getId(),
       result == null ? "null" : result.getClass());
   return result;
 }
  public DefaultDaemonConnection(
      final Connection<Object> connection, ExecutorFactory executorFactory) {
    this.connection = connection;
    stdinQueue = new StdinQueue(executorFactory);
    disconnectQueue = new DisconnectQueue();
    receiveQueue = new ReceiveQueue();
    executor = executorFactory.create("Handler for " + connection.toString());
    executor.execute(
        new Runnable() {
          public void run() {
            Throwable failure = null;
            try {
              while (true) {
                Object message;
                try {
                  message = connection.receive();
                } catch (Exception e) {
                  LOGGER.debug("Could not receive message from client.", e);
                  failure = e;
                  return;
                }
                if (message == null) {
                  LOGGER.debug("Received end-of-input from client.");
                  return;
                }

                if (!(message instanceof IoCommand)) {
                  LOGGER.debug("Received non-IO message from client: {}", message);
                  receiveQueue.add(message);
                } else {
                  LOGGER.debug("Received IO message from client: {}", message);
                  stdinQueue.add((IoCommand) message);
                }
              }
            } finally {
              stdinQueue.disconnect();
              disconnectQueue.disconnect();
              receiveQueue.disconnect(failure);
            }
          }
        });
  }
 public String toString() {
   return delegate.toString();
 }
 public void stop() {
   LOGGER.debug("thread {}: stopping connection", Thread.currentThread().getId());
   delegate.stop();
 }
 public void requestStop() {
   LOGGER.debug("thread {}: requesting stop for connection", Thread.currentThread().getId());
   delegate.requestStop();
 }
 public void completed(Result result) {
   connection.dispatch(result);
 }
 public void logEvent(OutputEvent logEvent) {
   connection.dispatch(logEvent);
 }
 public void buildStarted(BuildStarted buildStarted) {
   connection.dispatch(buildStarted);
 }
 public void daemonUnavailable(DaemonUnavailable unavailable) {
   connection.dispatch(unavailable);
 }