protected void onAgentCleanup() {
    agent.agent_cleanup();

    network.clearSendBuffer();
    network.putInt(Network.kAgentCleanup);
    network.putInt(0); // No data in this packet
  }
  protected void onAgentMessage() throws UnsupportedEncodingException {
    String message = network.getString();
    String reply = agent.agent_message(message);

    network.clearSendBuffer();
    network.putInt(Network.kAgentMessage);
    network.putInt(Network.sizeOf(reply));
    network.putString(reply);
  }
  protected void onAgentEnd() {
    // int size = network.getInt();
    double reward = network.getDouble();

    agent.agent_end(reward);

    network.clearSendBuffer();
    network.putInt(Network.kAgentEnd);
    network.putInt(0); // No data in this packet
  }
  protected void onAgentStep() {
    double reward = network.getDouble();
    Observation observation = network.getObservation();
    Action action = agent.agent_step(reward, observation);

    int size = Network.sizeOf(action);
    network.clearSendBuffer();
    network.putInt(Network.kAgentStep);
    network.putInt(size);
    network.putAction(action);
  }
  protected void onAgentInit() throws UnsupportedEncodingException {
    String taskSpec = network.getString();

    agent.agent_init(taskSpec);

    network.clearSendBuffer();
    network.putInt(Network.kAgentInit);
    // BTANNER: Sept 16 2008.. why are we putting this here?
    // OH, it's the size!  Ok.  I think.
    network.putInt(0); // No data following this header
  }
  protected void onAgentStart() {
    if (debug) {
      System.out.println("\tonAgentStart()");
    }
    Observation observation = network.getObservation();
    if (debug) {
      System.out.println("\t\tgot observation");
    }
    Action action = agent.agent_start(observation);
    if (debug) {
      System.out.println("\t\tgot action");
    }

    int size = Network.sizeOf(action);

    network.clearSendBuffer();
    network.putInt(Network.kAgentStart);
    network.putInt(size);
    network.putAction(action);
  }