next up previous contents
Next: Traveling and coming back Up: Hello World Previous: Test it out   Contents

Source code

 package org.kaariboga.agents;

 import java.lang.InterruptedException;

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

 /**
  * The HelloAgents just prints Hello World to the screen.
  */
 public class HelloAgent extends Kaariboga
 {
     /**
      * How often did the agent travel?
      */
     int trips = 0;

     /**
      * 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 HelloAgent(String name) {
         super("Hallodri_" + name);
     }

     /**
      * This is automically called if the agent arrives on
      * a base.
      */
     public void run() {
         System.out.println("Hello World! ");
         if (trips > 0) fireDestroyRequest();
     }

     /**
      * Called by the base when the agent arrives on the base.
      */
     public void onArrival() {
         ++trips;
     }
 }
This shouldn't be hard to understand.
import org.kaariboga.core.Kaariboga;
import org.kaariboga.core.KaaribogaEvent;
First we must import the important classes. The Kaariboga class provides a framework for all of our mobile agents. If our Kaariboga agent wants something from the server it fires an event. So we have to import the KaaribogaEvent.
public class HelloAgent extends Kaariboga
Here we define our agent class called HelloAgent. It inherits all the abilities of the Kaariboga class which contains all basic functionality of a mobile agent thus saving us a lot of work. If you use your agents only in a local environment you can choose any name you want for it's class name. However if you consider sending it around the internet you have to comply with the kaariboga naming conventions because every class must have a unique name. So if your name is John Doe it might be a good idea to name the class JD_HelloAgent. Please have a look at the kaariboga name spaces and register your prefix if neccessary.
public HelloAgent(String name){
        super("Hallodri_" + name);
}
This is the constructor of the HelloAgent. It is automatically invoked at creation time. Each kaariboga must have a unique name. Normally the server generates one for you when it loads the agent. This might be a very boring big number. But we like it some more personal and add an extra prefix to the name.
    public void run(){
        System.out.println("Hello World! ");
        if (trips > 0) fireDestroyRequest();
    }
If the kaariboga is loaded the method run is automatically invoked. In this case it does just two things: First it prints out Hello World to the screen. If it has already traveled to another computer it wants to be destroyed. So it fires a destroy request. The server listens to this request and removes the agent from it's memory.
    public void onArrival(){
        ++trips;
    }
The onArrival method is invoked when the agent arrives at another computer. Here the number of trips is increased.
next up previous contents
Next: Traveling and coming back Up: Hello World Previous: Test it out   Contents
Dirk 2002-09-01