public void run() {
      synchronized (pluginStartGroup) {
        waitForDependencies();
      }

      if (Log.loggingDebug)
        Log.debug(
            "Dependency satisfied for type="
                + await.getPluginType()
                + " name="
                + await.getPluginName());

      MVByteBuffer buffer = new MVByteBuffer(1024);
      ResponseMessage response = new ResponseMessage(await);
      Message.toBytes(response, buffer);
      buffer.flip();

      try {
        if (!ChannelUtil.writeBuffer(buffer, agentSocket)) {
          Log.error("could not write await dependencies response");
        }
      } catch (java.io.IOException e) {
        Log.exception("could not write await dependencies response", e);
      }
    }
    public boolean handleMessage() throws java.io.IOException {
      ByteBuffer buf = ByteBuffer.allocate(4);
      int nBytes = ChannelUtil.fillBuffer(buf, agentSocket);
      if (nBytes == 0) {
        Log.info("DomainServer: agent closed connection " + agentSocket);
        return false;
      }
      if (nBytes < 4) {
        Log.error("DomainServer: invalid message " + nBytes);
        return false;
      }

      int msgLen = buf.getInt();
      if (msgLen < 0) {
        return false;
      }

      MVByteBuffer buffer = new MVByteBuffer(msgLen);
      nBytes = ChannelUtil.fillBuffer(buffer.getNioBuf(), agentSocket);
      if (nBytes == 0) {
        Log.info("DomainServer: agent closed connection " + agentSocket);
        return false;
      }
      if (nBytes < msgLen) {
        Log.error(
            "DomainServer: invalid message, expecting "
                + msgLen
                + " got "
                + nBytes
                + " from "
                + agentSocket);
        return false;
      }

      Message message = (Message) MarshallingRuntime.unmarshalObject(buffer);

      if (message instanceof AgentHelloMessage) {
        // Successfully added agent, clear our socket var
        // so we don't close it.
        if (handleAgentHello((AgentHelloMessage) message)) agentSocket = null;
        return false;
      } else if (message instanceof AllocNameMessage)
        handleAllocName((AllocNameMessage) message, agentSocket);

      return true;
    }
  void handleAllocName(AllocNameMessage allocName, SocketChannel agentSocket)
      throws java.io.IOException {
    if (allocName.getMsgType() != MessageTypes.MSG_TYPE_ALLOC_NAME) {
      Log.error("DomainServer: invalid alloc name message");
      return;
    }

    String agentName = allocName(allocName.getType(), allocName.getAgentName());

    MVByteBuffer buffer = new MVByteBuffer(1024);
    AllocNameResponseMessage allocNameResponse = new AllocNameResponseMessage(allocName, agentName);
    Message.toBytes(allocNameResponse, buffer);
    buffer.flip();

    if (!ChannelUtil.writeBuffer(buffer, agentSocket)) {
      throw new RuntimeException("could not write alloc name response");
    }
  }
    boolean handleAgentHello(AgentHelloMessage agentHello) throws java.io.IOException {
      if (agentHello.getMsgType() != MessageTypes.MSG_TYPE_AGENT_HELLO) {
        Log.error(
            "DomainServer: invalid agent hello, got message type "
                + agentHello.getMsgType()
                + " from "
                + agentSocket);
        return false;
      }

      int agentId;
      synchronized (domainServer) {
        agentId = getNextAgentId();
        if (!agentNames.contains(agentHello.getAgentName()))
          agentNames.add(agentHello.getAgentName());
      }

      MVByteBuffer buffer = new MVByteBuffer(1024);
      HelloResponseMessage helloResponse =
          new HelloResponseMessage(agentId, domainStartTime, agentNames, encodedDomainKey);
      Message.toBytes(helloResponse, buffer);
      buffer.flip();

      if (!ChannelUtil.writeBuffer(buffer, agentSocket)) {
        Log.error("could not write to new agent, " + agentSocket);
        return false;
      }

      addNewAgent(
          agentId,
          agentSocket,
          agentHello.getAgentName(),
          agentHello.getAgentIP(),
          agentHello.getAgentPort(),
          agentHello.getFlags());

      return true;
    }