next up previous contents
Next: Source code for MessageSender Up: Communication between mobile agents Previous: Test it out   Contents

Source code for MessageReceiver

 package org.kaariboga.agents;

 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import javax.swing.*;

 import org.kaariboga.core.Kaariboga;
 import org.kaariboga.core.KaaribogaAddress;
 import org.kaariboga.core.KaaribogaEvent;
 import org.kaariboga.core.KaaribogaMessage;

 /**
  * The MessageReceiver agent waits for a message and then prints it out.
  */
 public class MessageReceiver extends Kaariboga
 {
     /**
      * Just initialize the super class.
      *
      * @param name The name of the agent. This name has to be
      * unique. Normally the KaaribogaBase class provides some
      * method to generate a unique name.
      */
     public MessageReceiver (String name){
         super("MessageReceiver_" + name);
     }

     /**
      * Prints kind and content of the received message to the screen.
      *
      * @param msg The message that this agent receives.
      */ 
     public void handleMessage (KaaribogaMessage msg){
         System.out.println ("MessageReceiver: Received Message.");      
         System.out.println ("           Kind: " + msg.kind);    
         System.out.println ("        Content: " + msg.content); 
     }   
      
 }
The only new thing is the handleMessage method.
    public void handleMessage (KaaribogaMessage msg){
        System.out.println ("MessageReceiver: Received Message.");      
        System.out.println ("           Kind: " + msg.kind);    
        System.out.println ("        Content: " + msg.content); 
    }
This method is used to handle all kinds of messages. By default the Kaariboga.handleMessage method just ignores all incoming messages. You are not restricted to the types of messages that are defined in the message class. Instead you may use arbitrary strings for the kind field, for example you could program an agent that operates on a data base and only accepts kind = DS_SQL messages. Note that here the DS_ namespace is used to avoid confusion with messages defined by othe programmers.

Dirk 2002-09-01