Example Program 1

A Simple Client Application:

    Step

    Description

    Source Code

    0

    do any necessary imports for application

    import EDU.cmu.softagents.misc.KQMLParser.InternalKQMLmessage;

    import EDU.cmu.softagents.misc.KQMLParser.ExternalKQMLmessage;

    import EDU.cmu.softagents.retsina.Communicator.*

    1

    Start class and main program

    do any necessary initialization and processing

    public class Client {

      static Communicator comm = null;

      public static void main ( String argv[] ) throws CommunicatorException, IOException

      {

    2

    create a new instance of the Communicator.

    give it the Client's AgentName, its HostName, and the preferred port number for communication.

        comm = new Communicator( "TestClient",

                                 "angel.cimds.ri",

                                 0,

                                 new InternalKQMLmessage(),

                                 new ExternalKQMLmessage(),

                                 null,

                                 false);

    3

    Using the comm object, open a connection to a server agent by referencing its Agent's name.  In return you'll get a ConnectionDescriptor that will be used to refer to this client-to-server link.

        ConnectionDescriptor cd = comm.openConnection("TestServer");

    4

    Create an internal message.

        InternalKQMLmessage reqMsg = new InternalKQMLmessage();

    5

    Populate the message structure with relevant information; including the "CONTENT".

        reqMsg.setPerformative("ask-one");

        reqMsg.setField(reqMsg.SENDER, "TestClient");

        reqMsg.setField(reqMsg.RECEIVER, "TestServer");

        reqMsg.setField(reqMsg.REPLY_WITH, "");

        reqMsg.setField(reqMsg.IN_REPLY_TO, "");

        reqMsg.setField(reqMsg.CONTENT, "HelloWorld!");

     

    6

    Send the message.

    After sending the message, wait for a reply message.

    Create a new message object called replyMsg and fill it with the information that returns from the server.

        boolean sentOK = comm.sendMsg(cd, reqMsg);

        InternalKQMLmessage replyMsg = (InternalKQMLmessage) comm.waitOnGetInputMsg(cd);

    or

       InternalKQMLmessage replyMsg=(InternalKQMLmessage) comm.sendMsgAndGetReply(cd, reqMsg);

    7

    Extract the CONTENT information from the reply message.

        String result = replyMsg.getField(replyMsg.CONTENT).trim();

    8

    Do whatever processing is appropriate to the data received.

        System.out.println("**** reply message = " + result + " ****";

    9

    Close down services and connections and end.

        comm.refuseNewConnection();

        comm.flushMsgsAndStop();

        comm.unRegisterWithANS();

     

        comm.closeConnection( cd );

        system.exit(0);

      }


A Simple Server Application:


    Step

    Description

    Source Code

  1.  
  2. Do any necessary imports for application

    import EDU.cmu.softagents.misc.KQMLParser.InternalKQMLmessage;

    import EDU.cmu.softagents.misc.KQMLParser.ExternalKQMLmessage;

    import EDU.cmu.softagents.retsina.Communicator.*

  3.  
  4. Start class and main program

    do any necessary initialization and processing

    public class Server {

      static Communicator comm = null;

      public static void main ( String argv[] ) throws CommunicatorException, IOException

      {

  5.  
  6. Create a new instance of the Communicator.

    Give it the Client’s AgentName, its HostName, and the preferred port number for communication.

        comm = new Communicator( "TestServer",

                                 "angel.cimds.ri",

                                 0,

                                 new InternalKQMLmessage(),

                                 new ExternalKQMLmessage(),

                                 null,

                                 false);

  7.  
  8. We don't have a connection with another Agent yet, so, wait for a message and then discover the connection details from the incoming message object.

        InputMsgObject iMsgObj = comm.waitOnGetInputMsg();

  9.  
  10. Discover Connection Descriptor and details from incoming Message.

        ConnectionDescriptor cd = comm.lookupConnection(iMsgObj);

  11.  
  12. Extract the message from the message object;

    then do whatever processing is required.

        InternalKQMLmessage msg = (InternalKQMLmessage) iMsgObj.getMsg();

        System.out.println("*** message= "+ msg.getField(msg.Content)+" ***");

  13.  
  14. Build a reply message based on the incoming message.

        InternalKQMLmessage replyMsg =

             (InternalKQMLmessage) msg.buildReplyMsg("Reply", "ThisIsTheReply", "nothing");

  15.  
  16. Send the reply message back to the client

        boolean sentOK = comm.sendMsg( cd, replyMsg );

  17.  
  18. Do whatever follow-up processing may be required

    //…

  19.  
  20. Close down all services and connections and end.
     comm.refuseNewConnection();
     comm.flushMsgsAndStop();
     comm.unRegisterWithANS();
      
     system.exit(0);
     }