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 MVByteBuffer toBytes() {
    int msgId = Engine.getEventServer().getEventID(this.getClass());

    MVByteBuffer buf = new MVByteBuffer(20);
    buf.putLong(playerId);
    buf.putInt(msgId);
    buf.putLong(questId);
    buf.flip();
    return buf;
  }
    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;
    }
  /** make sure the packet as the remote address and remote port set */
  static void sendPacket(DatagramChannel dc, RDPPacket packet) {

    sendMeter.add();

    // allocate a buffer
    int bufSize = 100 + (packet.numEacks() * 4);
    if (packet.getData() != null) {
      bufSize += packet.getData().length;
      sendDataMeter.add();
    }
    MVByteBuffer buf = new MVByteBuffer(bufSize);
    packet.toByteBuffer(buf); // function flips the buffer

    int remotePort = packet.getPort();
    InetAddress remoteAddr = packet.getInetAddress();

    if ((remotePort < 0) || (remoteAddr == null)) {
      throw new MVRuntimeException("RDPServer.sendPacket: remotePort or addr is null");
    }

    try {
      int bytes = dc.send(buf.getNioBuf(), new InetSocketAddress(remoteAddr, remotePort));
      if (bytes == 0) {
        Log.error("RDPServer.sendPacket: could not send packet, size=" + bufSize);
      }

      if (Log.loggingNet)
        Log.net(
            "RDPServer.sendPacket: remoteAddr="
                + remoteAddr
                + ", remotePort="
                + remotePort
                + ", numbytes sent="
                + bytes);
    } catch (java.io.IOException e) {
      Log.exception(
          "RDPServer.sendPacket: remoteAddr="
              + remoteAddr
              + ", remotePort="
              + remotePort
              + ", got exception",
          e);
      throw new MVRuntimeException("RDPServer.sendPacket", e);
    }
  }
  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;
    }
 static void callbackProcessPacket(
     ClientConnection.MessageCallback pcb, ClientConnection clientCon, RDPPacket packet) {
   if (packet.isNul()) {
     return;
   }
   byte[] data = packet.getData();
   MVByteBuffer buf = new MVByteBuffer(data);
   RDPConnection con = (RDPConnection) clientCon;
   // If this is a multiple-message message . . .
   if (buf.getLong() == -1 && buf.getInt() == RDPConnection.aggregatedMsgId) {
     con.aggregatedReceives++;
     PacketAggregator.allAggregatedReceives++;
     // Get the count of sub buffers
     int size = buf.getInt();
     con.receivedMessagesAggregated += size;
     PacketAggregator.allReceivedMessagesAggregated += size;
     if (Log.loggingNet)
       Log.net(
           "RDPServer.callbackProcessPacket: processing aggregated message with "
               + size
               + " submessages");
     MVByteBuffer subBuf = null;
     for (int i = 0; i < size; i++) {
       try {
         subBuf = buf.getByteBuffer();
       } catch (Exception e) {
         Log.error("In CallbackThread, error getting aggregated subbuffer: " + e.getMessage());
       }
       if (subBuf != null) pcb.processPacket(con, subBuf);
     }
   } else {
     con.unaggregatedReceives++;
     PacketAggregator.allUnaggregatedReceives++;
     buf.rewind();
     pcb.processPacket(con, buf);
   }
 }
 protected void parseBytes(MVByteBuffer buf) {
   buf.rewind();
   setPlayerOid(buf.getLong());
   /* int msgId = */ buf.getInt();
   setQuestId(buf.getLong());
 }