?? smitest.java
字號:
package examples.externalCom;// Examplesimport examples.simpleCom.*;// Grasshopper 1.2+import de.ikv.grasshopper.communication.GrasshopperAddress;import de.ikv.grasshopper.communication.ExternalCommService;import de.ikv.grasshopper.communication.ProxyGenerator;import de.ikv.grasshopper.agency.IAgentSystem;import de.ikv.grasshopper.agency.PlaceAlreadyExistsException;import de.ikv.grasshopper.agency.IRegionRegistration;import de.ikv.grasshopper.util.SearchFilter;import de.ikv.grasshopper.type.AgentSystemInfo;import de.ikv.grasshopper.type.AgentInfo;import mobilitools.smi.*;import org.omg.CfMAF.*;import java.io.*;public class SmiTest implements MobileObject, Cloneable{ String my_name; transient Agency my_agency; boolean running; public String Gpdm=""; public String Gpjg=""; public String Gpgs=""; public String Yssj=""; public SmiTest() { System.out.println("SmiTest default constructor"); } public SmiTest(String[] args) { if (args.length < 4){ System.out.println("Exit!"); throw new RuntimeException("missing arguments"); } /*Gpdm=args[0]; Gpjg=args[1]; Gpgs=args[2]; Yssj=args[3];*/ } public void afterBirth(AgentSystem agency, AgentEntry entry, Object [] args) throws ArgumentInvalid { my_agency = (Agency)agency; my_name = new String(my_agency.getAgentEntry(this).getName().identity); System.out.print("agent " + (new NameWrapper(my_agency.getAgentEntry(this).getNameWrapper().getStringRepresentation())).toString() + " is born with argument(s):"); for (int i=0 ; i<4 ; ++i) { System.out.print(" " + args[i]); } running = true; Gpdm=(String)args[0]; Gpjg=(String)args[1]; Gpgs=(String)args[2]; Yssj=(String)args[3]; System.out.println("\n" + my_name + ".afterBirth() is OK ([y]/n)?"); try { String reply = (new BufferedReader(new InputStreamReader(System.in))).readLine(); if (reply.equalsIgnoreCase("n")) { throw new ArgumentInvalid(); } } catch (IOException e) { e.printStackTrace(); }/*********************************************/ System.out.println("Gpdm = "+Gpdm); System.out.println("Gpjg = "+Gpjg); System.out.println("Gpgs = "+Gpgs); ExternalApplication ea=new ExternalApplication();try {ea.create("socket://202.115.30.169:7020/GHRegistry", "socket://202.115.30.165:8000/agent","file:/E:/lb/grasshopper/bin",Gpdm,Gpjg,Gpgs,Yssj);} catch (Exception e) { e.printStackTrace(); } my_agency.terminate();/*********************************************/ } public Object beforeClone(Object[] args) throws CloneNotSupportedException { System.out.println(my_name + ".clone() with arguments:"); for (int i=0 ; i<args.length ; ++i) { System.out.print(" " + args[i]); } System.out.println("\n" + my_name + ".beforeClone() is OK ([y]/n)?"); try { String reply = (new BufferedReader(new InputStreamReader(System.in))).readLine(); if (reply.equalsIgnoreCase("n")) { throw new CloneNotSupportedException(); } } catch (IOException e) { e.printStackTrace(); } return super.clone(); } public void afterClone(AgentEntry entry, Object[] args) throws ArgumentInvalid { my_name = new String(entry.getName().identity); System.out.print("clone " + entry.getNameWrapper() + " is appearing with argument(s):"); for (int i=0 ; i<args.length ; ++i) { System.out.print(" " + args[i]); } running = true; System.out.println("\n" + my_name + ".afterClone() is OK ([y]/n)?"); try { String reply = (new BufferedReader(new InputStreamReader(System.in))).readLine(); if (reply.equalsIgnoreCase("n")) { throw new ArgumentInvalid(); } } catch (IOException e) { e.printStackTrace(); } } public void afterMove(AgentSystem agency, SMILocation location, String place) throws BadMove { my_agency = (Agency)agency; System.out.println( my_name + ".afterMove(" + location.getRegion() + " " + location.getAgency() + ", " + place + ") is OK ([y]/n)?"); try { String reply = (new BufferedReader(new InputStreamReader(System.in))).readLine(); if (reply.equalsIgnoreCase("n")) { throw new BadMove(BadMove.REJECTED, "re-installation refused by user"); } } catch (IOException e) { e.printStackTrace(); } } public void afterMoveFailed( SMILocation location, String place, int reason, String message) { System.out.println( my_name + ".afterMoveFailed(" + location.getRegion() + " " + location.getAgency() + ", " + place + ")"); System.out.println("code " + String.valueOf(reason) + ": " + message); } public void beforeMove(SMILocation location, String place) throws BadMove { System.out.println( my_name + ".beforeMove(" + location.getRegion() + " " + location.getAgency() + ", " + place + ") is OK ([y]/n)?"); try { String reply = (new BufferedReader(new InputStreamReader(System.in))).readLine(); if (reply.equalsIgnoreCase("n")) { throw new BadMove(BadMove.REJECTED, "move refused by user"); } } catch (IOException e) { e.printStackTrace(); } } public void beforeDeath() { System.out.println(my_name + ".beforeDeath()"); } public void beforeShutdown() { System.out.println(my_name + ".beforeShutdown()"); } public void beforeResume() throws AgentIsRunning { if (running) { throw new AgentIsRunning(); } running = true; System.out.println(my_name + ".resume()"); } public void beforeSuspend() throws AgentIsSuspended { if (! running) { throw new AgentIsSuspended(); } running = false; System.out.println(my_name + ".suspend()"); }}/** * This class realizes a stand-alone application that acts as client as * well as a server for the agent 'ExternalAccessAgent'. */ class ExternalApplication { public static void create(String registryAddress,String args,String agentCodebase,String gd,String gj,String gg,String ys) throws Exception { /** * Creation arguments: * args[0] = Region registry address * args[1] = Communication service address * args[2] = Agent code base */ /* if (args.length < 3) { System.out.println("## ExternalApplication: Creation arguments needed: <registryAddress> <commServiceAddress> <agentCodebase>"); System.out.println("## Exiting."); System.exit(1); }*/ /* String registryAddress = args[0];*/ // GrasshopperAddress commServiceAddress = new GrasshopperAddress(args); /*String agentCodebase = args[2];*/ // Create server object System.out.println("## ExternalApplication: Creating server object"); // ServerObject serverObject = new ServerObject(); // Create communication service System.out.println("## ExternalApplication: Creating external communication service"); // ExternalCommService commService = new ExternalCommService(serverObject); // commService.start(); // Start communication receiver // System.out.println("## ExternalApplication: Starting new receiver at " + commServiceAddress); // commService.startReceiver(commServiceAddress); // Contact region registry System.out.println("## ExternalApplication: Contacting region registry '" + registryAddress + "'."); GrasshopperAddress regionAddr = new GrasshopperAddress(registryAddress); IRegionRegistration regionProxy = (IRegionRegistration) ProxyGenerator.newInstance(IRegionRegistration.class, regionAddr.generateRegionId(), regionAddr); // Request a list of all registered agencies from the region registry AgentSystemInfo[] agentSystemInfo = regionProxy.listAgencies(new SearchFilter()); System.out.println("## ExternalApplication: " + agentSystemInfo.length + " agencies found."); for (int i=0; i<agentSystemInfo.length; i++){ System.out.println("## " + (i+1) + ". " + agentSystemInfo[i].getLocation()); } // check array boundaries (bug in 2.2.0) if (agentSystemInfo.length > 0) { // Contact first agency of the list System.out.println("## ExternalApplication: Contacting agency '" + agentSystemInfo[0].getLocation().generateAgentSystemId() + "'."); GrasshopperAddress address = agentSystemInfo[0].getLocation(); String serverAddresses[] = regionProxy.lookupCommunicationServer(address.generateAgentSystemId()); System.out.println("## ExternalApplication: The agency has the following communication servers:"); for(int i = 0; i < serverAddresses.length; i++){ System.out.println("## " + (i+1) + ". " + serverAddresses[i]); } System.out.println("## ExternalApplication: Selecting server " + serverAddresses[0]); GrasshopperAddress agencyAddress = new GrasshopperAddress(serverAddresses[0]); IAgentSystem agencyProxy = (IAgentSystem) ProxyGenerator.newInstance(IAgentSystem.class, agencyAddress.generateAgentSystemId(), agencyAddress); // Create a new place inside the contacted agency. System.out.println("## ExternalApplication: Creating place 'NewPlace' in contacted agency."); /* try { agencyProxy.createPlace("NewPlace", ""); } catch(PlaceAlreadyExistsException e) { // ignore }*/ // Create an agent in the place 'InformationDesk' of the contacted agency. System.out.println("## ExternalApplication: Creating agent inside the place 'InformationDesk'."); Object agentCreationArgs[] = new Object[5]; agentCreationArgs[0] = "socket://202.115.30.165:7000/second"; agentCreationArgs[1] = (String)gd; agentCreationArgs[2] = (String)gj; agentCreationArgs[3] = (String)gg; agentCreationArgs[4] = (String)ys; System.out.println("Gpdm = "+gd); System.out.println("Gpjg = "+gj); System.out.println("Gpgs = "+gg); AgentInfo agentInfo = agencyProxy.createAgent("examples.externalCom.ExternalAccessAgent", agentCodebase, "", agentCreationArgs); // Contact the new agent. System.out.println("## ExternalApplication: Contacting the new agent."); IExternalAccessAgent agentProxy = (IExternalAccessAgent) ProxyGenerator.newInstance(IExternalAccessAgent.class, agentInfo.getIdentifier(), agencyAddress); // Move the agent to the new place via the agent's own 'go(...)' method. System.out.println("## ExternalApplication: Moving the agent to the place 'NewPlace'."); //GrasshopperAddress newLocation = agencyAddress; // newLocation.setPlace("InformationDesk"); // agentProxy.go(newLocation.toString()); } else { System.out.println("## ExternalApplication: Nothing to do. Please start at least one agency."); } // Sleeping some seconds (simulate server lifecycle) System.out.println("## ExternalApplication: Sleeping five seconds."); try { Thread.sleep(5000); } catch(InterruptedException e) { System.out.println("## ExternalApplication: Interrupted."); } // That's all System.out.println("## ExternalApplication: Stopping communication service."); //commService.stop(); System.out.println("## ExternalApplication: Ready. Game over"); //System.exit(0); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -