コード例 #1
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 final synchronized void deconnection(String id) {
   for (Iterator i = distantKernels.iterator(); i.hasNext(); ) {
     AgentAddress distantK = (AgentAddress) i.next();
     if (distantK.getKernel().getID().equals(id)) {
       debug("disconnected from " + id);
       for (Iterator j = organizations.entrySet().iterator(); j.hasNext(); ) {
         Map.Entry e = (Map.Entry) j.next();
         Organization org = (Organization) e.getValue();
         if (org.removeAgentsFromKernel(distantK.getKernel())) {
           leaveGroup("communities", (String) e.getKey());
           sendAll(
               new SynchroMessage(
                   Kernel.LEAVE_GROUP,
                   getAddress(),
                   COMMUNITIES,
                   (String) e.getKey(),
                   null,
                   null));
           sendAll(new SynchroMessage(Kernel.DELETE_COMMUNITY, (String) e.getKey()));
           j.remove();
         }
       }
       kernelAgent.callHooks(Kernel.DISCONNECTED_FROM, distantK.getKernel());
       kernelAgent.callHooks(Kernel.REMOVE_MEMBER_ROLE, distantK, COMMUNITIES, PUBLIC, SITE);
       i.remove();
       System.gc();
       System.runFinalization();
       break;
     }
   }
   if (gui != null) gui.refreshCommunities();
 }
コード例 #2
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 final void handleConnectionMessage(ConnectionMessage m) {
   if (m.isTheFirstMessage()) {
     getCurrentKernel().synchronizeKernel(((ConnectionMessage) m).getOrgs(), true);
     establishConnectionWith(m.getSender(), false);
   } else getCurrentKernel().synchronizeKernel(((ConnectionMessage) m).getOrgs(), false);
   if (distantKernels.add(m.getSender())) {
     // System.out.println("Connecting to "+m.getSender());
     kernelAgent.callHooks(Kernel.CONNECTED_TO, m.getSender().getKernel());
     kernelAgent.callHooks(Kernel.ADD_MEMBER_ROLE, m.getSender(), COMMUNITIES, PUBLIC, SITE);
   }
   System.gc();
   System.runFinalization();
 }
コード例 #3
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 synchronized void removeCommunity(String communityName) {
   System.err.println("removing community : " + communityName);
   organizations.remove(communityName);
   kernelAgent.callHooks(Kernel.DELETE_COMMUNITY, communityName);
   leaveGroup(COMMUNITIES, communityName);
   sendAll(
       new SynchroMessage(
           Kernel.LEAVE_GROUP, getAddress(), COMMUNITIES, communityName, null, null));
   sendAll(new SynchroMessage(Kernel.DELETE_COMMUNITY, communityName));
   if (gui != null) gui.refreshCommunities();
 }
コード例 #4
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
  public final void activate() {

    createCommunity(PUBLIC);
    /*createGroup(true,"communities","public",null,null);
    requestRole("communities","public","site",null);*/
    createGroup(false, "communications", null, null);
    // createGroup(false,"local",null,null);
    createGroup(false, "system", null, null);
    requestRole("system", SITE, null);
    requestRole("communications", SITE, null);
    kernelAgent.localOrg = (Organization) organizations.get(PUBLIC);
    getCurrentKernel().launchAgent(kernelAgent, "KernelAgent", getCurrentKernel(), false);
    this.setName("SITE:" + this.getAddress().getKernel().getHost());
  }
コード例 #5
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 synchronized Organization createCommunity(String communityName) {
   if (!organizations.containsKey(communityName)) {
     Organization org = new Organization();
     organizations.put(communityName, org);
     createGroup(true, COMMUNITIES, communityName, null, null);
     requestRole(COMMUNITIES, communityName, SITE, null);
     sendAll(
         new SynchroMessage(
             getAddress(), COMMUNITIES, communities.getGroup(communityName), communityName));
     sendAll(
         new SynchroMessage(
             Kernel.ADD_MEMBER_ROLE, getAddress(), COMMUNITIES, communityName, SITE, null));
     sendAll(new SynchroMessage(Kernel.NEW_COMMUNITY, communityName));
     kernelAgent.callHooks(Kernel.NEW_COMMUNITY, communityName);
     if (hasGUI()) gui.refreshCommunities();
     return org;
   }
   return null;
 }
コード例 #6
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
  synchronized void leaveCommunity(String communityName) {
    Organization organization = (Organization) organizations.get(communityName);
    organization.removeDistantAgents();
    if (organization.isEmpty()) {

      leaveGroup("communities", communityName);
      sendAll(
          new SynchroMessage(
              Kernel.LEAVE_GROUP, getAddress(), COMMUNITIES, communityName, null, null));
      organizations.remove(communityName);
      kernelAgent.callHooks(Kernel.DELETE_COMMUNITY, communityName);
    } else {
      leaveRole(COMMUNITIES, communityName, SITE);
      sendAll(
          new SynchroMessage(
              Kernel.REMOVE_MEMBER_ROLE, getAddress(), COMMUNITIES, communityName, "site", null));
      sendAll(new SynchroMessage(Kernel.DELETE_COMMUNITY, communityName));
    }
    if (gui != null) gui.refreshCommunities();
  }
コード例 #7
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 final synchronized void handleNetworkRequest(NetworkRequest m) {
   switch (m.getRequestCode()) {
     case NetworkRequest.INJECT_MESSAGE:
       try {
         Message m2 = (Message) m.getArgument();
         getCurrentKernel().sendLocalMessage(m2);
         if (!(m2 instanceof PrivateMessage))
           kernelAgent.callHooks(Kernel.SEND_MESSAGE, m2.clone());
       } catch (MessageException me) {
         if (getDebug()) System.err.println(me);
       }
       break;
     case NetworkRequest.CONNECTION_REQUEST:
       if (m.getSender().equals(myCommunicator)) {
         establishConnectionWith((AgentAddress) m.getArgument(), true);
         // distantKernels.add(m.getArgument());
       }
       break;
     case NetworkRequest.DECONNECTED_FROM:
       distantKernels.remove(m.getArgument());
       deconnection((String) m.getArgument());
       break;
     case NetworkRequest.BE_COMMUNICATOR:
       if (myCommunicator == null) {
         redisplayMyGUI();
         myCommunicator = m.getSender();
       }
       break;
     case NetworkRequest.STOP_COMMUNICATOR:
       if (myCommunicator != null && myCommunicator.equals(m.getSender())) {
         disposeMyGUI();
         myCommunicator = null;
         for (Iterator i = organizations.entrySet().iterator(); i.hasNext(); ) {
           Map.Entry e = (Map.Entry) i.next();
           Organization org = (Organization) e.getValue();
           if (org.removeDistantAgents()) {
             // communities.leaveGroup(getAddress(), (String) e.getKey());
             leaveGroup("communities", (String) e.getKey());
             i.remove();
           }
         }
         distantKernels.clear();
         System.gc();
         System.runFinalization();
       }
       break;
     case NetworkRequest.GET_AVAILABLE_DESTINATIONS:
       KernelAddress[] destinations = new KernelAddress[distantKernels.size()];
       int j = 0;
       for (Iterator i = distantKernels.iterator(); i.hasNext(); j++)
         destinations[j] = ((AgentAddress) i.next()).getKernel();
       sendMessage(
           m.getSender(),
           new NetworkRequest(NetworkRequest.GET_AVAILABLE_DESTINATIONS, destinations));
       break;
     case NetworkRequest.REQUEST_MIGRATION:
       if (getDebug()) println("receiving a migration request of " + m.getSender());
       // kernelAgent.callsHook(Kernel.MIGRATION...	à rajouter
       tryMigration((KernelAddress) m.getArgument(), m.getSender());
       break;
   }
 }
コード例 #8
0
ファイル: SiteAgent.java プロジェクト: Ooya/Robot-Sapiens
 final void handleSynchroMessage(SynchroMessage m) {
   if (connectedWith(m.community)) {
     if (!organizations.containsKey(m.community)) {
       kernelAgent.callHooks(Kernel.NEW_COMMUNITY, m.community);
       organizations.put(m.community, new Organization());
     }
     Organization localOrg = (Organization) organizations.get(m.community);
     switch (m.code) {
       case Kernel.CREATE_GROUP:
         localOrg.createGroup(
             m.initiator,
             true,
             m.groupName,
             m.newGroup.getDescription(),
             m.newGroup.getGroupIdentifier());
         kernelAgent.callHooks(
             Kernel.CREATE_GROUP, new AGRTrio(m.initiator, m.community, m.groupName, null));
         break;
       case Kernel.ADD_MEMBER_ROLE:
         localOrg.requestRole(m.initiator, m.groupName, m.roleName, m.memberCard);
         kernelAgent.callHooks(
             Kernel.ADD_MEMBER_ROLE,
             new AGRTrio(m.initiator, m.community, m.groupName, m.roleName));
         break;
       case Kernel.LEAVE_GROUP:
         if (localOrg.leaveGroup(m.initiator, m.groupName)) removeCommunity(m.community);
         kernelAgent.callHooks(
             Kernel.LEAVE_GROUP, new AGRTrio(m.initiator, m.community, m.groupName, null));
         break;
       case Kernel.NEW_COMMUNITY:
         establishConnectionWith(m.getSender(), true);
         break;
       case Kernel.DELETE_COMMUNITY:
         localOrg.removeAgentsFromKernel(m.getSender().getKernel());
         if (localOrg.isEmpty()) {
           removeCommunity(m.community);
           kernelAgent.callHooks(Kernel.DELETE_COMMUNITY, m.community);
         }
         break;
       case Kernel.REMOVE_MEMBER_ROLE:
         if (localOrg.leaveRole(m.initiator, m.groupName, m.roleName)) {
           removeCommunity(m.community);
         }
         kernelAgent.callHooks(
             Kernel.REMOVE_MEMBER_ROLE,
             new AGRTrio(m.initiator, m.community, m.groupName, m.roleName));
         break;
       case Kernel.MIGRATION:
         if (getDebug()) System.err.println("receiving a migration " + m.ref.toString());
         getCurrentKernel().receiveAgent(m.ref);
         System.gc();
         System.runFinalization();
         break;
     }
   } else
     switch (m.code) {
       case Kernel.NEW_COMMUNITY:
         kernelAgent.callHooks(Kernel.NEW_COMMUNITY, m.community);
         System.err.println("new community detected");
         if (gui != null)
           gui.refreshCommunities(); // establishConnectionWith(m.getSender(), true);
         break;
       case Kernel.DELETE_COMMUNITY:
         kernelAgent.callHooks(Kernel.DELETE_COMMUNITY, m.community);
         System.err.println("a community has been deleted "); // +organizations);
         if (gui != null)
           gui.refreshCommunities(); // establishConnectionWith(m.getSender(), true);
         break;
     }
 }
コード例 #9
0
 public void resetAgent(KernelAgent agent) {
   alive.set(true);
   agent.stop();
   agentLatch.countDown();
 }
コード例 #10
0
  public void launchKernelAgent() {
    log.info("Starting Kernel Agent");

    String name = "KERNEL-AGENT";

    KernelAgent agent = (KernelAgent) applicationContext.getBean("kernelAgent");

    KernelAgentWorker kernelAgentWorker =
        (KernelAgentWorker) applicationContext.getBean("kernelAgentWorker");
    agent.setNodeContext(Coordination.emptyContext(NodeId.kernelAgentNode(name)));
    agent.init();

    log.info("Agent {} initialized", agent.getNodeContext().getId());

    try {
      do {
        agent.stop();
        agentLatch = new CountDownLatch(1);
        alive.set(false);
        do {
          try {
            RegistrationPack registrationPack =
                RegistrationPack.create(
                    agent.getNodeContext().getId(), kernelAgentWorker.getQualifiers());
            Ack ack = agent.getExchangeClient().registerNode(registrationPack);
            if (Ack.SUCCESS.equals(ack)) break;
          } catch (IOException e) {
            log.info("Agent {} can't do registration {}", agent.getNodeContext().getId(), e);
            log.info("Wait 10 seconds and try again");
            Thread.sleep(AGENT_REGISTRATION_PERIOD);
          }
        } while (true);
        log.info("Registration agent {} has been done", agent.getNodeContext().getId());

        agent.start();
        agentLatch.await();
      } while (alive.get());
    } catch (InterruptedException e) {
      // do nothing;
    }

    agent.stop();
    log.info("Kernel Agent finish work");
  }