Exemplo n.º 1
0
  private BECodec getBECodec(String serviceName) {
    if (serviceBECodecs == null) {
      serviceBECodecs = new HashMap(2);
    }
    BECodec codec = (BECodec) serviceBECodecs.get(serviceName);
    if (codec == null) {
      try {
        codec = (BECodec) Class.forName(serviceName + "BECodec").newInstance();
      } catch (Exception e) {
        // The service does not have a BECodec --> Use a dummy one
        codec =
            new BECodec() {
              public Object[] decodeParams(String methodName, Object[] methodParams) {
                return methodParams;
              }

              public Object encodeResult(String methodName, Object result) {
                return result;
              }
            };
      }
      serviceBECodecs.put(serviceName, codec);
    }
    return codec;
  }
Exemplo n.º 2
0
 public Agent acquireLocalAgent(AID id) {
   Agent ag = super.acquireLocalAgent(id);
   if (ag == null) {
     // The agent is not in the LADT. Likely it is in the FE --> Acquire its image
     // FIXME: The image is not "acquired". Likely we have to use a LADT
     // also for agent images
     ag = (Agent) agentImages.get(id);
   }
   return ag;
 }
Exemplo n.º 3
0
  /**
   * Dispatch a message to an agent in the FrontEnd. If this method is called by a thread that is
   * serving a message sent by an agent in the FrontEnd too, nothing is done as the dispatch has
   * already taken place in the FrontEnd (see messageOut()).
   */
  public boolean postMessageToLocalAgent(ACLMessage msg, AID receiverID) {

    // Try first in the LADT
    boolean found = super.postMessageToLocalAgent(msg, receiverID);
    if (found) {
      return found;
    } else {
      // The receiver must be in the FrontEnd
      AgentImage image = (AgentImage) agentImages.get(receiverID);
      if (image != null) {
        if (agentImages.containsKey(msg.getSender()) && isExplicitReceiver(msg, receiverID)) {
          // The message was sent by an agent living in the FrontEnd. The
          // receiverID (living in the FrontEnd too) has already received
          // the message.
          // The second part of the condition ensures that, if the
          // message was not directly sent to the receiver (e.g. it was sent to a topic
          // or an alias), message delivery occurs normally.
          // FIXME: This does not take into account that an agent not living
          // in the FrontEnd may send a message on behalf of an agent living
          // in the FrontEnd.
          return true;
        }

        try {
          // Forward the message to the FrontEnd
          int size;
          if (msg.hasByteSequenceContent()) {
            size = msg.getByteSequenceContent().length;
          } else {
            size = msg.getContent() != null ? msg.getContent().length() : 0;
          }
          myLogger.log(
              Logger.INFO,
              getID()
                  + " - Delivering IN message "
                  + ACLMessage.getPerformative(msg.getPerformative())
                  + ", size="
                  + size);
          myFrontEnd.messageIn(msg, receiverID.getLocalName());
          handlePosted(receiverID, msg);
          return true;
        } catch (NotFoundException nfe) {
          System.out.println("WARNING: Missing agent in FrontEnd");
          return false;
        } catch (IMTPException imtpe) {
          System.out.println("WARNING: Can't deliver message to FrontEnd");
          return false;
        }
      } else {
        // Agent not found
        System.out.println("WARNING: Agent " + receiverID + " not found on BackEnd container");
        return false;
      }
    }
  }
Exemplo n.º 4
0
  Object processInformation(Information info) {
    // -------------------------------------------

    Account acc = (Account) accounts.get(info.getAccountId());
    if (acc == null) return newProblem(ACCOUNT_NOT_FOUND);

    java.util.Date date = new java.util.Date();
    Operation op = new Operation(); // <-- Apply admin charge
    op.setType(ADMIN);
    op.setAmount(info.getType() == BALANCE ? BAL_CHARGE : OPER_CHARGE);
    acc.setBalance(acc.getBalance() - op.getAmount());
    op.setBalance(acc.getBalance());
    op.setAccountId(acc.getId());
    op.setDate(date);
    List l = (List) operations.get(acc.getId());
    l.add(op);
    operations.put(acc.getId(), l);

    if (info.getType() == BALANCE) return acc;
    if (info.getType() == OPERATIONS) return l;
    return null;
  }
Exemplo n.º 5
0
  Object processOperation(MakeOperation mo) {
    // -------------------------------------------

    Account acc = (Account) accounts.get(mo.getAccountId());
    if (acc == null) return newProblem(ACCOUNT_NOT_FOUND);
    if (mo.getAmount() <= 0) return newProblem(ILLEGAL_OPERATION);

    if (mo.getType() != DEPOSIT && mo.getType() != WITHDRAWAL) return null;
    if (mo.getType() == DEPOSIT) acc.setBalance(acc.getBalance() + mo.getAmount());
    else if (mo.getType() == WITHDRAWAL) {
      if (mo.getAmount() > acc.getBalance()) return newProblem(NOT_ENOUGH_MONEY);
      acc.setBalance(acc.getBalance() - mo.getAmount());
    }
    Operation op = new Operation();
    op.setType(mo.getType());
    op.setAmount(mo.getAmount());
    op.setAccountId(acc.getId());
    op.setDate(new java.util.Date());
    List l = (List) operations.get(acc.getId());
    l.add(op);
    operations.put(acc.getId(), l);
    return acc;
  }
Exemplo n.º 6
0
 public AgentImage getAgentImage(AID id) {
   return (AgentImage) agentImages.get(id);
 }