next up previous contents
Next: Executing programs Up: Traveling around in a Previous: Test it out   Contents

Source code

package org.kaariboga.agents;

import java.io.Serializable;
import java.lang.InterruptedException;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.*;

import org.kaariboga.core.Kaariboga;
import org.kaariboga.core.KaaribogaAddress;
import org.kaariboga.core.KaaribogaEvent;
import org.kaariboga.plugins.domainPlugIn.*;

/**
 * Pops up a hello Window on every server in the domain when
 * a domain server is installed.
 */
public class HelloDomain extends Kaariboga
{
    /**
     * List of all the servers in the domain.
     */
    Vector v;

    /**
     * Points to the next destination in v.
     */
    int i;

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

    /**
     * Initializes v with all servers connected to the domain.
     */
    public void onCreate(){
        Object service = base.getServiceObject( this, "kaariboga.org/DomainService" );
        if (    ( service != null )
             && ( service instanceof DomainService ) )
        {
            DomainService serviceObject = (DomainService) service;
            Enumeration enum = serviceObject.getServers().elements();
            i = 0;
            v = new Vector();
            while ( enum.hasMoreElements() ){
                v.addElement( enum.nextElement() );
            }
        }
        else fireDestroyRequest();
    }


    /**
     * Shows a window.
     */
    public void onArrival(){
        new Popup().start();
    }

    /**
     * This is automically called if the agent arrives on
     * a base.
     */
    public void run(){
        try{
            if (i < v.size()){
                destination = (KaaribogaAddress) v.elementAt(i);
                ++i;
                System.out.println("Try to dispatch");
                fireDispatchRequest();
            }
            else fireDestroyRequest();
        }
        catch (ArrayIndexOutOfBoundsException e){
            System.err.println ("HelloDomain: Index out of bounds!");
            fireDestroyRequest();
        }
    }

    /**
     * Use a thread to let a window pop up.
     */
    public class Popup extends Thread implements Serializable{

        /**
         * Pop up window.
         */
        public void run(){
            JOptionPane dialog = new JOptionPane();
            dialog.showMessageDialog (null, "Hi there!");
        }
    }

}
First we have to get a list of all base servers connected to the domain.
    public void onCreate(){
        Object service = base.getServiceObject( this, "kaariboga.org/DomainService" );
        if (    ( service != null )
             && ( service instanceof DomainService ) )
        {
            DomainService serviceObject = (DomainService) service;
            Enumeration enum = serviceObject.getServers().elements();
            i = 0;
            v = new Vector();
            while ( enum.hasMoreElements() ){
                v.addElement( enum.nextElement() );
            }
        }
        else fireDestroyRequest();
    }
onCreate just initializes a vector with all the servers connected to the domain. Note that the vector is only created once. So if some servers connect to or disconnect from the domain the agent would not realize it. This is a bit tricky, because it makes use of a plug-in. Plug-ins make it possible to add arbitrary extensions to a KaaribogaBase server.
Object service = base.getServiceObject( this, "kaariboga.org/DomainService" );
This code requests a service object called "kaariboga.org/DomainService" from the base. A plug-in usually installs a service object to provide the requested functionality.
if (    ( service != null )
     && ( service instanceof DomainService ) )
This makes shure, that the service object exists and is of the requested type. Note, that an agent must know, what methods are provided by a service object.
DomainService serviceObject = (DomainService) service;
Enumeration enum = serviceObject.getServers().elements();
i = 0;
v = new Vector();
while ( enum.hasMoreElements() ){
    v.addElement( enum.nextElement() );
}
The DomainService provides a method called getServers(), which lists all servers in the domain. In the run method you will find the following code.
            if (i < v.size()){
                 destination = (KaaribogaAddress) v.elementAt(i);
                 ++i;
                 System.out.println("Try to dispatch");
                 fireDispatchRequest();
             }
This manages the traveling. i points to the current location in the vector v holding the list of all connected base servers. The agent must do something. This is triggered in onArrival.
    public void onArrival(){
         new Popup().start();
     }
onArrival is called every time the agent arrives at a base server. Lets see what it does.
    public class Popup extends Thread implements Serializable{

        public void run(){
             JOptionPane dialog = new JOptionPane();
             dialog.showMessageDialog (null, "Hi there!");
         }
     }
This lets a message dialog pop up. It is implemented as an own thread. The agent starts the thread and continues traveling before the user has clicked the OK button on the message window.
next up previous contents
Next: Executing programs Up: Traveling around in a Previous: Test it out   Contents
Dirk 2002-09-01