示例#1
0
 protected void live() {
   while (true) {
     Message m = purgeMailbox(); // to always treat the last request
     if (m == null) {
       m = waitNextMessage(); // waiting a request
     }
     String role = m.getSender().getRole();
     if (role.equals(MarketOrganization.CLIENT_ROLE)) {
       handleClientRequest((StringMessage) m);
     }
   }
 }
  /* (non-Javadoc)
   * @see madkit.kernel.Agent#live()
   */
  public void live() {
    while (alive) {
      Message m = waitNextMessage();

      exitImmediatlyOnKill();
      if (!alive) break;
      if (m instanceof NetworkMessage) {
        handleNetworkMessage((NetworkMessage) m);
      } else if (m instanceof NetConfigMessage) {
        handleConfigMessage((NetConfigMessage) m);
      } else {
        debug(m.toString());
      }
    }
  }
  /**
   * Sends a message to a Distant Kernel.
   *
   * @param message message to send.
   */
  private void sendDistantMessage(KernelMessage message) {
    Message orig;
    if (message instanceof KernelMessage) {
      KernelMessage temp = (KernelMessage) message;
      orig = (Message) (temp.getArgument());
    } else orig = message;

    if (shouldUseUDP(orig)) {
      println("WARN:  No udp message routing yet!!!");
    } else {
      DistantKernelInformation info = getP2PAddress(orig);
      if (info != null) {
        AgentAddress p2p = info.getP2PAgent();
        sendMessage(p2p, new NetworkMessage(NetworkMessage.DISTANT_MESSAGE, orig));
      } else {
        println("WARN : unknown destination kernel" + orig.getReceiver().getKernel());
      }
    }
  }
 private DistantKernelInformation getP2PAddress(Message orig) {
   DistantKernelInformation info =
       (DistantKernelInformation) routeTable.get(orig.getReceiver().getKernel().getID());
   return info;
 }