Пример #1
0
  private void handleClientRequest(StringMessage request) {
    if (!checkAgentAddress(request.getSender())) // Is the client still there ?
    return;
    if (hasGUI()) { // starting the contract net
      blinkPanel.setBackground(Color.YELLOW);
    }
    if (logger != null)
      logger.info(
          "I received a request for a " + request.getContent() + " \nfrom " + request.getSender());
    List<Message> bids =
        broadcastMessageWithRoleAndWaitForReplies( // wait all answers
            MarketOrganization.COMMUNITY, // community
            MarketOrganization.PROVIDERS_GROUP, // group
            request.getContent() + "-" + MarketOrganization.PROVIDER_ROLE, // role
            new StringMessage("make-bid-please"), // ask for a bid
            MarketOrganization.BROKER_ROLE, // I am a broker
            900); // I cannot wait the end of the universe

    if (bids == null) { // no reply
      if (logger != null)
        logger.info(
            "No bids at all : No one is selling "
                + request.getContent().toUpperCase()
                + " !!\nPlease launch other providers !a");
      if (hasGUI()) {
        blinkPanel.setBackground(Color.LIGHT_GRAY);
      }
      return;
    }

    // select the best offer
    final List<IntegerMessage> offers =
        Arrays.asList(bids.toArray(new IntegerMessage[0])); // casting
    IntegerMessage best = ObjectMessage.min(offers);
    if (logger != null)
      logger.info("The best offer is from " + best.getSender() + " " + best.getContent());

    // creating a contract group
    String contractGroupId = "" + System.nanoTime();
    // sending the location to the provider
    Message ack =
        sendMessageWithRoleAndWaitForReply(
            best.getSender(), // the address of the provider
            new StringMessage(contractGroupId), // send group's info
            MarketOrganization.BROKER_ROLE, // I am a broker
            1000); // I cannot wait the end of the universe

    if (ack != null) { // The provider has entered the contract group
      if (logger != null) logger.info("Provider is ready !\nSending the contract number to client");
      sendReply(request, new StringMessage(contractGroupId)); // send group's info
      pause((int) (Math.random() * 2000 + 1000)); // let us celebrate !!
    } else { // no answer from the provider...
      if (logger != null) logger.info("Provider disappears !!!!");
    }
    if (hasGUI()) {
      blinkPanel.setBackground(Color.LIGHT_GRAY);
    }
  }