externalaccessagent.java

来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 120 行

JAVA
120
字号
package examples.externalCom;import FIPA.*;import FIPA.ACL.*;import FIPA.ACL.Content.*;import javax.swing.JOptionPane;import de.ikv.grasshopper.communication.GrasshopperAddress;import de.ikv.grasshopper.communication.ProxyGenerator;import de.ikv.grasshopper.type.Identifier;import de.ikv.grasshopper.agency.AgentCreationFailedException;/** *  This class represents an agent initiating a conversation. * The user has to specify the agent at whom this message is directed. If both receiver and sender agent are on the * same grasshopper agency, the name of the agent is enough for the message to come through to the other side.  * If the agent runs on another agency, the name should include the address of the other agent. */public class ExternalAccessAgent extends FIPAAgent{        public static     String          Gpdm="";        public static      String          Gpjg="";        public static      String          Gpgs="";        public static      String          Yssj="";             public void init(Object args[]){    	    	    	 if (args.length < 5){            log("Creation arguments needed: <ComReceiverAddress>");            log("Exiting.");                    }       String serverObjectAddr = (String)args[0];         Gpdm = (String)args[1];         Gpjg = (String)args[2];         Gpgs = (String)args[3];         Yssj = (String)args[4];        // do nothing here.    }        public String getName(){        return "SenderAgent";    }    public void live(){        this.registerWithLocalDF();        this.action();    }                private ACLMessage createMessage(String receiver){        try{           return new ACLMessage("<mesg reply-with=\"greeting\" type=\"inform\">" +                                            "<receiver>" +                                                "<agent>" +                                                     receiver +                                                 "</agent>" +                                             "</receiver>" +                                            "<sender>" +                                                  "<agent>" +                                                     this.getAgentName() +                                                "</agent>" +                                            "</sender>" +                                             "<content>" +                                                 "<sl>" +                                                     "<cblock>" +                                                     ""+Gpdm+","+Gpjg+","+Gpgs+","+Yssj+"" +                                                    "</cblock>" +                                                "</sl>" +                                            "</content>" +                                            "</mesg>" );                                     }catch(Exception e){            System.out.println("SenderAgent: Exception " + e.toString());        }        return null;   }     /**     * The onRemove() method of Agent is overwritten so that the agent can    * deregister itself from the DF.    */    public void onRemove(){        this.deRegisterWithLocalDF();    }        public void action(){        //String receiver = JOptionPane.showInputDialog("Enter receiver: <receiverName>[@address]");         String receiver ="ReceiverAgent";        if (receiver == null || receiver.equals("")){            // do nothing        }else{            System.out.println("Creating message");            ACLMessage message = this.createMessage(receiver);            if (message != null){                System.out.println("SenderAgent: Sending message to " + receiver);                String s = message.toString();                System.out.println(s);                this.send(s);            }        }        try{        remove();}        catch(Exception e){        System.out.println("Remove Fail!");        	};    }        public void message(String msg){        System.out.println("#############Sender Agent: received message \n" + msg);    }             }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?