next up previous contents
Next: Self Reproduction Up: Communication between mobile agents Previous: Source code for MessageReceiver   Contents

Source code for MessageSender

 package org.kaariboga.agents;

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

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

 /**
  * The MessageSender agent sends a message to all agents on the base server.
  */
 public class MessageSender 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 MessageSender (String name){
         super("MessageSender_" + name);
     }

     /**
      * Sends a message to all agents on this base and destroys itself.
      */
     public void run(){

         // open dialog for message
         JOptionPane dialog = new JOptionPane();
         String content = dialog.showInputDialog (null, "Please type in a message.");

         // send Message
         // Note: if you want to send Messages to other servers you will
         // need code like this:
         // KaaribogaAddress baseAdr  = base.getBaseAddress(this);
         // KaaribogaAddress sender  = new KaaribogaAddress (baseAdr.host, baseAdr.port, getName());
         // KaaribogaAddress recipient = new KaaribogaAddress (host, port, name);         
         Enumeration names = base.getKaaribogaNames (this);
         while (names.hasMoreElements()){            
             // simple constructors for address save for local use
             KaaribogaAddress sender  = new KaaribogaAddress (getName());
             KaaribogaAddress recipient = new KaaribogaAddress ((String) names.nextElement()); 
             KaaribogaMessage message = new KaaribogaMessage (sender, recipient, "MESSAGE", content, null);
             fireKaaribogaMessage (message);
         }        

         fireDestroyRequest();
     }
    
 }
Lets look at the important part.
  Enumeration names = base.getKaaribogaNames (this);
  while (names.hasMoreElements()){            
      // simple constructors for address save for local use
      KaaribogaAddress sender  = new KaaribogaAddress (getName());
      KaaribogaAddress recipient = new KaaribogaAddress ((String) names.nextElement()); 
      KaaribogaMessage message = new KaaribogaMessage (sender, recipient, "MESSAGE", content, null);
      fireKaaribogaMessage (message);
  }
First the MessageSender requests the names of all kaariboga agents on the same base by calling base.getKaaribogaNames(this). For security reasons the agent has to identify itself by using the parameter this. The while loop is used to iterate through all agents sending a message to each one. Each message must be initialized with a sender and a receiver. If you just want to address agents on the same base you may use a the simplified address form that contains just the name of the receiver like recipient = new KaaribogaAddress ((String) names.nextElement()). However if you want to address agents on other network locations you need to provide a full address with some code like
  KaaribogaAddress baseAdr  = base.getBaseAddress(this);
  KaaribogaAddress sender  = new KaaribogaAddress (baseAdr.host, baseAdr.port, getName());
  KaaribogaAddress recipient = new KaaribogaAddress (host, port, name);
The message is send by fireKaaribogaMessage (message). The base server will then decide what to do with this message. This way you don't have to worry about any routing or addressing sockets. As allready mentioned in the MessageReceiver section you could define all kinds of arbitrary messages suited to your needs.
next up previous contents
Next: Self Reproduction Up: Communication between mobile agents Previous: Source code for MessageReceiver   Contents
Dirk 2002-09-01