Package jade.core

Examples of jade.core.Agent


        if (!l.contains(introspectorName)) {
          l.add(introspectorName);
        }
      }
     
      Agent a = myContainer.acquireLocalAgent(targetName);
     
      // Activate generation of behaviour-related events on the
      // target agent
      a.setGenerateBehaviourEvents(true);
     
      // Retrieve the current agent state
      AgentState as = a.getAgentState();
     
      // Retrieve the list of pending ACL messages
      List messages = new LinkedList();
      myContainer.fillListFromMessageQueue(messages, a);
     
View Full Code Here


          }
        }
      }
     
      if (resetGenerateBehaviourEvents) {
        Agent a = myContainer.acquireLocalAgent(targetName);
        if (a != null) {
          a.setGenerateBehaviourEvents(false);
        }
        myContainer.releaseLocalAgent(targetName);
      }
    }
View Full Code Here

     
      clonedAgent(agentID, cid, creds);
    }
   
    private void moveAgent(AID agentID, Location where) throws IMTPException, NotFoundException {
      Agent a = myContainer.acquireLocalAgent(agentID);
     
      if(a == null) {
        throw new NotFoundException("Move-Agent failed to find " + agentID);
      }
      a.doMove(where);
     
      myContainer.releaseLocalAgent(agentID);
    }
View Full Code Here

     
      myContainer.releaseLocalAgent(agentID);
    }
   
    private void copyAgent(AID agentID, Location where, String newName) throws IMTPException, NotFoundException {
      Agent a = myContainer.acquireLocalAgent(agentID);
     
      if(a == null)
        throw new NotFoundException("Clone-Agent failed to find " + agentID);
      a.doClone(where, newName);
     
      myContainer.releaseLocalAgent(agentID);
    }
View Full Code Here

      if(myLogger.isLoggable(Logger.CONFIG))
        myLogger.log(Logger.CONFIG,"Outgoing Filer accepting command INFORM_KILLED. Name is "+target.getName());
     
      // Remove CodeLocator entry.
      String containerName = myContainer.getID().getName();
      Agent agent = myContainer.acquireLocalAgent(target);
      String codeContainerName = getClassSite(agent);
      myContainer.releaseLocalAgent(target);

      //Check if the agent have migrated or not.
      if (codeContainerName != null) {
View Full Code Here

       
       
        // Reconstruct the serialized agent
        //#DOTNET_EXCLUDE_BEGIN
        ObjectInputStream in = new Deserializer(new ByteArrayInputStream(serializedInstance), agentID.getName(), classSiteName, myContainer.getServiceFinder());
        Agent instance = (Agent)in.readObject();
        //#DOTNET_EXCLUDE_END
        /*#DOTNET_INCLUDE_BEGIN
         ubyte[] ubyteSerializedInstance = new ubyte[serializedInstance.length];
         System.Buffer.BlockCopy(serializedInstance, 0, ubyteSerializedInstance, 0, serializedInstance.length);
         ByteArrayInputStream in = new ByteArrayInputStream(serializedInstance);
         ObjectInputStream decoder = new ObjectInputStream(in);
         Object obj = decoder.readObject();
        
         Agent instance = (Agent) obj;
         #DOTNET_INCLUDE_END*/
       
        //log("Agent " + agentID + " reconstructed", 2);
        if(myLogger.isLoggable(Logger.FINE))
          myLogger.log(Logger.FINE,"Agent " + agentID + " reconstructed");
       
       
       
        /* --- This code should go into the Security Service ---
        
         // agent is about to be created on the destination Container,
          // let's check for permissions before
          
           // does the agent come from a MOVE or a CLONE ?
            switch (instance.getState()) {
            case Agent.AP_TRANSIT:  // MOVED
            // checking CONTAINER_MOVE_TO...
             myContainer.getAuthority().checkAction(
             Authority.CONTAINER_MOVE_TO,
             myContainer.getContainerPrincipal(),
             instance.getCertificateFolder()  );
             break;
             case Agent.AP_COPY:  // CLONED
             // checking CONTAINER_CLONE_TO...
              myContainer.getAuthority().checkAction(
              Authority.CONTAINER_CLONE_TO,
              myContainer.getContainerPrincipal(),
              instance.getCertificateFolder()  );
              break;
              } // end switch
             
              log("Permissions for agent " + agentID + " OK", 2);
             
              // --- End of code that should go into the Security Service ---
               */
       
        Credentials agentCerts = null;
        //#MIDP_EXCLUDE_BEGIN
        //CertificateFolder agentCerts = instance.getCertificateFolder();
        //#MIDP_EXCLUDE_END
       
        /*# MIDP_INCLUDE_BEGIN
         CertificateFolder agentCerts = new CertificateFolder();
         # MIDP_INCLUDE_END*/
       
        if(isCloned) {
          // Notify the main slice that a new agent is born
          AgentMobilitySlice mainSlice = (AgentMobilitySlice)getSlice(MAIN_SLICE);
         
          try {
            mainSlice.clonedAgent(agentID, myContainer.getID(), agentCerts);
          }
          catch(IMTPException imtpe) {
            // Try to get a newer slice and repeat...
            mainSlice = (AgentMobilitySlice)getFreshSlice(MAIN_SLICE);
            mainSlice.clonedAgent(agentID, myContainer.getID(), agentCerts);
          }
        }
       
        // Store the container where the classes for this agent can be
        // retrieved
        sites.put(instance, classSiteName);
       
        // Connect the new instance to the local container
        Agent old = myContainer.addLocalAgent(agentID, instance);
        if(myLogger.isLoggable(Logger.FINE))
          myLogger.log(Logger.FINE,"Agent " + agentID.getName() + " inserted into LADT");
       
        if(startIt) {
          // Actually start the agent thread
View Full Code Here

    private void handleTransferResult(AID agentID, boolean result, List messages) throws IMTPException, NotFoundException {
      if(myLogger.isLoggable(Logger.FINER))
        myLogger.log(Logger.FINER,"Activating incoming agent "+agentID);
     
      try {
        Agent agent = myContainer.acquireLocalAgent(agentID);
       
        if ((agent == null) || (agent.getState() != AP_TRANSIT)) {
          throw new NotFoundException("handleTransferResult() unable to find a suitable agent.");
        }
       
        if (result == TRANSFER_ABORT) {
          myContainer.removeLocalAgent(agentID);
        }
        else {
          // Insert received messages at the start of the queue
          for (int i = messages.size(); i > 0; i--) {
            agent.putBack((ACLMessage)messages.get(i - 1));
          }
         
          myContainer.powerUpLocalAgent(agentID);
          if(myLogger.isLoggable(Logger.CONFIG))
            myLogger.log(Logger.CONFIG,"Incoming agent " + agentID.getName() + " activated");
View Full Code Here

  private void handleReattached() {
    myLogger.log(Logger.INFO, "Re-adding all local agents to recovered main container...");
    AID[] ids = myContainer.agentNames();
    for (int i = 0; i < ids.length; ++i) {
      AID id = ids[i];
      Agent agent = myContainer.acquireLocalAgent(id);
      if (agent != null) {
        if(myLogger.isLoggable(Logger.CONFIG)) {
          myLogger.log(Logger.CONFIG, "Re-adding agent "+id.getName());
        }
        try {
          // Note that we pass null owner principal and null initial credentials.
          // The Security service (if active) will insert the existing ones.
          // Note also that we use agent.getAID() instead of id since the latter may have addresses not up to date.
          myContainer.initAgent(agent.getAID(), agent, null, null);
        }
        catch (Exception e) {
          myLogger.log(Logger.SEVERE, "Error reattaching agent "+id.getName()+". ", e);
        }
      }
View Full Code Here

  protected Service.Slice getFreshSlice(String name) throws ServiceException {
    return super.getFreshSlice(name);
  }
 
  private void initCredentials(Command cmd, AID id) {
    Agent agent = myContainer.acquireLocalAgent(id);
    if (agent != null) {
      try {
        CredentialsHelper ch = (CredentialsHelper) agent.getHelper("jade.core.security.Security");
        cmd.setPrincipal(ch.getPrincipal());
        cmd.setCredentials(ch.getCredentials());
      }
      catch (ServiceException se) {
        // The security plug-in is not there. Just ignore it
View Full Code Here

      Location where = (Location)params[1];
     
      if(myLogger.isLoggable(Logger.CONFIG))
        myLogger.log(Logger.CONFIG,"Moving agent " + agentID.getName() + " on container " + where.getName());
     
      Agent a = myContainer.acquireLocalAgent(agentID);
      if (a == null) {
        myLogger.log(Logger.SEVERE,"Internal error: handleMove() called with a wrong name (" + agentID.getName() + ") !!!");
        return;
      }
     
      int transferState = 0;
      List messages = new ArrayList();
      AgentMobilitySlice dest = null;
      try {
        // If the destination container is the same as this one, there is nothing to do
        if (CaseInsensitiveString.equalsIgnoreCase(where.getName(), myContainer.here().getName())) {
          return;
        }
       
        dest = (AgentMobilitySlice) getSlice(where.getName());
        if (dest == null) {
          myLogger.log(Logger.SEVERE,"Destination "+where.getName()+" does not exist or does not support mobility");
          return;
        }
        if(myLogger.isLoggable(Logger.FINE)) {
          myLogger.log(Logger.FINE,"Destination container for agent " + agentID + " found");
        }
       
        transferState = 1;
       
        // Serialize the agent
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream encoder = new ObjectOutputStream(out);
        encoder.writeObject(a);
        byte[] bytes = out.toByteArray();
        if(myLogger.isLoggable(Logger.FINE)) {
          myLogger.log(Logger.FINE,"Agent " + agentID.getName() + " correctly serialized");
        }
       
        // Gets the container where the agent classes can be retrieved (possibly the agent arrived in this container from another container)
        String classSiteName = (String)sites.get(a);     
        if (classSiteName == null) {
          // The agent was born on this container
          classSiteName = getLocalNode().getName();
        }
       
        // Create the agent on the destination container
        try {
          dest.createAgent(agentID, bytes, classSiteName, MIGRATION, CREATE_ONLY);
        }
        catch(IMTPException imtpe) {
          // Try to get a newer slice and repeat...
          dest = (AgentMobilitySlice)getFreshSlice(where.getName());
          dest.createAgent(agentID, bytes, classSiteName, MIGRATION, CREATE_ONLY);
        }
       
        transferState = 2;
        if(myLogger.isLoggable(Logger.FINE)) {
          myLogger.log(Logger.FINE,"Agent " + agentID.getName() + " correctly created on destination container");
        }
       
        AgentMobilitySlice mainSlice = (AgentMobilitySlice)getSlice(MAIN_SLICE);
       
        // Perform an atomic transaction for agent identity transfer
        // From now on, messages for the moving agent will be routed to the
        // destination container
        boolean transferResult = false;
        try {
          transferResult = mainSlice.transferIdentity(agentID, (ContainerID) myContainer.here(), (ContainerID) where);
        }
        catch(IMTPException imtpe) {
          // Try to get a newer slice and repeat...
          mainSlice = (AgentMobilitySlice)getFreshSlice(MAIN_SLICE);
          transferResult = mainSlice.transferIdentity(agentID, (ContainerID) myContainer.here(), (ContainerID) where);
        }
       
        transferState = 3;
       
        if (transferResult == TRANSFER_COMMIT) {
          if(myLogger.isLoggable(Logger.FINE)) {
            myLogger.log(Logger.FINE,"Identity of agent " + agentID.getName() + " correctly transferred");
          }
         
          // Send received messages to the destination container. Note that
          // there is no synchronization problem as the agent is locked in the LADT
          myContainer.fillListFromMessageQueue(messages, a);
         
          dest.handleTransferResult(agentID, transferResult, messages);
         
          try {
            // Cause the termination of the agent thread
            a.changeStateTo(new LifeCycle(AP_GONE) {
              public boolean alive() {
                return false;
              }
            });
           
            // Remove the gone agent from the LADT
            myContainer.removeLocalAgent(a.getAID());
          }
          catch (Exception e) {
            // Should never happen
            e.printStackTrace();
          }
          sites.remove(a);
          if(myLogger.isLoggable(Logger.FINE)) {
            myLogger.log(Logger.FINE,"Agent " + agentID.getName() + " correctly gone");
          }
        }
        else {
          myLogger.log(Logger.WARNING,"Error transferring identity of agent " + agentID.getName());
         
          a.restoreBufferedState();
          dest.handleTransferResult(agentID, transferResult, messages);
          myLogger.log(Logger.WARNING,"Migration of agent " + agentID.getName() + "aborted");
        }
      }
      //#DOTNET_EXCLUDE_BEGIN
      catch (IOException ioe) {
        // Error in agent serialization
        myLogger.log(Logger.SEVERE,"Error in agent serialization. Abort transfer. " + ioe);
      }
      catch (JADESecurityException ae) {
        // Permission to move not owned
        myLogger.log(Logger.SEVERE,"Permission to move not owned. Abort transfer. " + ae.getMessage());
      }
      catch(NotFoundException nfe) {
        if(transferState == 0) {
          myLogger.log(Logger.SEVERE,"Destination container does not exist. Abort transfer. " + nfe.getMessage());
        }
        else if(transferState == 2) {
          myLogger.log(Logger.SEVERE,"Transferring agent does not seem to be part of the platform. Abort transfer. " + nfe.getMessage());
        }
        else if(transferState == 3) {
          // PANIC !!!
          myLogger.log(Logger.SEVERE,"Transferred agent not found on destination container. Can't roll back. " + nfe.getMessage());
        }
      }
      catch(NameClashException nce) {
        // This should not happen, because the agent is not changing its name but just its location...
        nce.printStackTrace();
      }
      catch(IMTPException imtpe) {
        // Unexpected remote error
        if (transferState == 0) {
          myLogger.log(Logger.SEVERE,"Can't retrieve destination container. Abort transfer. " + imtpe.getMessage());
        }
        else if (transferState == 1) {
          myLogger.log(Logger.SEVERE,"Error creating agent on destination container. Abort transfer. " + imtpe.getMessage());
        }
        else if (transferState == 2) {
          myLogger.log(Logger.SEVERE,"Error transferring agent identity. Abort transfer. " + imtpe.getMessage());
         
          try {
            dest.handleTransferResult(agentID, TRANSFER_ABORT, messages);
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
        else if (transferState == 3) {
          // PANIC !!!
          myLogger.log(Logger.SEVERE,"Error activating transferred agent. Can't roll back!!!. " + imtpe.getMessage());
        }
      }
      //#DOTNET_EXCLUDE_END
      /*#DOTNET_INCLUDE_BEGIN
       catch(System.Exception exc)
       {
       if(myLogger.isLoggable(Logger.SEVERE))
       myLogger.log(Logger.SEVERE,"Error in agent serialization. Abort transfer. " + exc.get_Message());
       }
       #DOTNET_INCLUDE_END*/
      finally {
        if (transferState <= 2) {
          // Something went wrong --> Roll back.
          a.restoreBufferedState();
        }
        myContainer.releaseLocalAgent(agentID);
      }
    }
View Full Code Here

TOP

Related Classes of jade.core.Agent

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.