public void handleMessage(final Connection connection, final InputStream dataStream)
     throws IOException {
   final int cmd = readUnsignedByte(dataStream);
   if (cmd != Protocol.AUTH) {
     log.warnf(
         "Received unrecognized greeting code 0x%02x from %s",
         Integer.valueOf(cmd), connection.getPeerAddress());
     connection.close();
     return;
   }
   final int version = StreamUtils.readUnsignedByte(dataStream);
   if (version < 1) {
     log.warnf("Received connection with invalid version from %s", connection.getPeerAddress());
     connection.close();
     return;
   }
   final byte[] authCode = new byte[16];
   StreamUtils.readFully(dataStream, authCode);
   final ManagedProcess process = processController.getServerByAuthCode(authCode);
   if (process == null) {
     log.warnf(
         "Received connection with unknown credentials from %s", connection.getPeerAddress());
     StreamUtils.safeClose(connection);
     return;
   }
   log.tracef("Received authentic connection from %s", connection.getPeerAddress());
   connection.setMessageHandler(
       new ConnectedMessageHandler(processController, process.isInitial()));
   processController.addManagedConnection(connection);
   dataStream.close();
 }
      public void handleMessage(final Connection connection, final InputStream dataStream)
          throws IOException {
        try {

          final int cmd = StreamUtils.readUnsignedByte(dataStream);
          switch (cmd) {
            case Protocol.SEND_STDIN:
              {
                // HostController only
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  log.tracef("Received send_stdin for process %s", processName);
                  processController.sendStdin(processName, dataStream);
                } else {
                  log.tracef("Ignoring send_stdin message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.ADD_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  final byte[] authKey = new byte[16];
                  readFully(dataStream, authKey);
                  final int commandCount = readInt(dataStream);
                  final String[] command = new String[commandCount];
                  for (int i = 0; i < commandCount; i++) {
                    command[i] = readUTFZBytes(dataStream);
                  }
                  final int envCount = readInt(dataStream);
                  final Map<String, String> env = new HashMap<String, String>();
                  for (int i = 0; i < envCount; i++) {
                    env.put(readUTFZBytes(dataStream), readUTFZBytes(dataStream));
                  }
                  final String workingDirectory = readUTFZBytes(dataStream);
                  log.tracef("Received add_process for process %s", processName);
                  processController.addProcess(
                      processName, Arrays.asList(command), env, workingDirectory, false);
                } else {
                  log.tracef("Ignoring add_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.START_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  processController.startProcess(processName);
                  log.tracef("Received start_process for process %s", processName);
                } else {
                  log.tracef("Ignoring start_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.STOP_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  // HostController only
                  processController.stopProcess(processName);
                } else {
                  log.tracef("Ignoring stop_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.REMOVE_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  processController.removeProcess(processName);
                } else {
                  log.tracef("Ignoring remove_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.REQUEST_PROCESS_INVENTORY:
              {
                if (isHostController) {
                  processController.sendInventory();
                } else {
                  log.tracef("Ignoring request_process_inventory message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.RECONNECT_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  final String hostName = readUTFZBytes(dataStream);
                  final int port = readInt(dataStream);
                  processController.sendReconnectProcess(processName, hostName, port);
                } else {
                  log.tracef("Ignoring reconnect_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.SHUTDOWN:
              {
                if (isHostController) {
                  new Thread(
                          new Runnable() {
                            public void run() {
                              processController.shutdown();
                              System.exit(0);
                            }
                          })
                      .start();
                } else {
                  log.tracef("Ignoring shutdown message from untrusted source");
                }
                break;
              }
            default:
              {
                log.warnf("Received unknown message with code 0x%02x", Integer.valueOf(cmd));
                // unknown
                dataStream.close();
              }
          }
        } finally {
          safeClose(dataStream);
        }
      }