org.kaariboga.core
Class KaaribogaBase

java.lang.Object
  |
  +--org.kaariboga.core.KaaribogaBase
All Implemented Interfaces:
java.util.EventListener, KaaribogaListener, KaaribogaMessageListener, java.io.Serializable

public class KaaribogaBase
extends java.lang.Object
implements java.io.Serializable, KaaribogaListener, KaaribogaMessageListener

A class to host mobile agents called kaaribogas. It is able to dispatch and receive kaaribogas, handle their requests and messages.

See Also:
Serialized Form

Inner Class Summary
(package private)  class KaaribogaBase.ListenThread
          Listens on the specified port and spawns new threads to receive incoming agents.
(package private)  class KaaribogaBase.ReceiveMessageThread
          Handles a messages incoming through a socket connection.
protected  class KaaribogaBase.SendMessageThread
          Sends a KaaribogaMessage to another base.
 
Field Summary
private  KaaribogaAddress baseAddress
          The KaaribogaAddress of this base
private  java.util.Vector baseListeners
          Classes that listen to the fired base events.
private  KaaribogaAddress boServer
          Base online server that registers all servers in this domain.
(package private)  java.util.Hashtable boxes
          Stores all agents and related information.
protected  ClassManager classManager
          Handles classes and their byte codes.
private  long counter
          Used to generate a unique name for a kaariboga.
(package private)  long delay
          Delay time in milliseconds after which an anused agent class is removed from memory.
private  KaaribogaAddress ipServer
          Server that serves dynamic IP addresses
private  boolean isOnline
          Is this server on- or offline?
private  KaaribogaBase.ListenThread listenThread
          Thread that accepts network connections.
private  java.util.HashMap messageHandlers
          Message handlers that have registered on this base to handle certain kinds of messages.
private  java.lang.Object parent
          The object that created this base.
private  int port
          The port number on which the server listens.
(package private)  java.net.ServerSocket serverSocket
          Listens on the main port.
private  java.util.HashMap services
          The base can be extended by services, that are provided by plug-ins.
 int version
          Not used.
 
Constructor Summary
KaaribogaBase(java.lang.Object parent, ClassManager clManager)
          Creats a new Kaariboga Base.
 
Method Summary
 void addBaseListener(BaseListener l)
          Adds a base event listener.
 void addKaaribogaOnArrival(Kaariboga kaariboga, java.net.InetAddress sender)
          Adds a kaariboga to the base and starts it's thread.
 void addKaaribogaOnCreation(Kaariboga kaariboga, java.net.InetAddress sender)
          Adds a kaariboga to the base, performs some initialisation and starts it's thread.
 boolean addMessageHandler(KbMessageHandler handler, java.lang.String kind)
          Adds a message handler for a specified kind of message to this base.
 boolean addService(KbPlugIn plugIn, java.lang.String name)
          Adds a service to this base.
 void destroyKaariboga(java.lang.Object sender, java.lang.String name)
          Destroys a kaariboga agent with all necessary clean up.
protected  void dispatch(Kaariboga kaariboga, KaaribogaAddress address)
          Dispatches a kaariboga to another base.
 void dispatchKaariboga(java.lang.Object sender, java.lang.String name, KaaribogaAddress destination)
          Dispatches a kaariboga agent with all necessary clean up.
 void dispose()
          Shuts down the network connection.
protected  void fireKaaribogaArrived(java.lang.String name)
          Fires an event if a new kaariboga has arrived.
protected  void fireKaaribogaCreated(java.lang.String name)
          Fires an event if a new kaariboga was created.
protected  void fireKaaribogaDestroyed(java.lang.String name)
          Fires an event if a kaariboga has been destroyed.
protected  void fireKaaribogaLeft(java.lang.String name)
          Fires an event if a kaariboga was dispatched to another base.
 java.lang.String generateName()
          Generates a unique name that can be used to initialize agents.
 KaaribogaAddress getBaseAddress(java.lang.Object sender)
          Returns the KaaribogaAddress of this base.
 java.util.Enumeration getKaaribogaNames(java.lang.Object sender)
          Gets the names of all kaariboga agents on this base.
 boolean getOnlineStatus()
          Determines if the server is online.
 java.lang.String[] getServiceNames(java.lang.Object sender)
          Returns the names of all the services currently available.
 java.lang.Object getServiceObject(java.lang.Object sender, java.lang.String name)
          Returns a service object for a specified service name.
 void goOffline(java.lang.Object sender)
          Performs all neccessary actions to go offline
 void goOnline(java.lang.Object sender, int portNo, KaaribogaAddress boServer, KaaribogaAddress ipServer)
          Performs all neccessary actions to go online
 void goOnlineForDynamicIP(java.lang.Object sender)
          If the server has no static IP address this method is called to determine the dynamic IP address.
 void handleMessage(KaaribogaMessage msg)
          Handles incoming messages from other bases and kaaribogas.
 void kaaribogaDestroyRequest(KaaribogaEvent e)
          Handles the destroy request of a kaariboga.
 void kaaribogaDispatchRequest(KaaribogaEvent e)
          Handles the dispatch request of a kaariboga.
 void kaaribogaMessage(KaaribogaMessageEvent e)
          Handles messages that are fired by kaaribogas.
 void kaaribogaSleepRequest(KaaribogaEvent e)
          Handles the sleep request of a kaariboga.
 void removeBaseListener(BaseListener l)
          Removes a base event listener.
 boolean removeMessageHandler(KbMessageHandler handler, java.lang.String kind)
          Removes a message handler for a specified kind of message to this base.
 boolean removeService(KbPlugIn plugIn, java.lang.String name)
          Removes the service named name from this base.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

version

public final int version
Not used.

isOnline

private boolean isOnline
Is this server on- or offline? Use GetOnlineStatus to retrieve this value.

parent

private java.lang.Object parent
The object that created this base. This variable is used for security purposes. The parent has more rights than other objects.

messageHandlers

private java.util.HashMap messageHandlers
Message handlers that have registered on this base to handle certain kinds of messages.

services

private java.util.HashMap services
The base can be extended by services, that are provided by plug-ins.

boServer

private KaaribogaAddress boServer
Base online server that registers all servers in this domain.

ipServer

private KaaribogaAddress ipServer
Server that serves dynamic IP addresses

baseListeners

private java.util.Vector baseListeners
Classes that listen to the fired base events.

delay

long delay
Delay time in milliseconds after which an anused agent class is removed from memory.

port

private int port
The port number on which the server listens.

baseAddress

private KaaribogaAddress baseAddress
The KaaribogaAddress of this base

counter

private long counter
Used to generate a unique name for a kaariboga.

listenThread

private volatile KaaribogaBase.ListenThread listenThread
Thread that accepts network connections.

classManager

protected ClassManager classManager
Handles classes and their byte codes.

boxes

java.util.Hashtable boxes
Stores all agents and related information.

serverSocket

java.net.ServerSocket serverSocket
Listens on the main port.
Constructor Detail

KaaribogaBase

public KaaribogaBase(java.lang.Object parent,
                     ClassManager clManager)
Creats a new Kaariboga Base.
Parameters:
clManager - The ClassManager to use.
Method Detail

handleMessage

public void handleMessage(KaaribogaMessage msg)
Handles incoming messages from other bases and kaaribogas. At this time the function just prints out the message contents.

dispatch

protected void dispatch(Kaariboga kaariboga,
                        KaaribogaAddress address)
Dispatches a kaariboga to another base. It stops the kaariboga, spawns a new thread to send it to another base and removes it from the current base.
Parameters:
kaariboga - The agent that has to be dispatched.
address - Address of the destination.

dispose

public void dispose()
Shuts down the network connection.

addKaaribogaOnArrival

public void addKaaribogaOnArrival(Kaariboga kaariboga,
                                  java.net.InetAddress sender)
Adds a kaariboga to the base and starts it's thread. This method should be called to add an arriving Kaariboga agent to the base.

addKaaribogaOnCreation

public void addKaaribogaOnCreation(Kaariboga kaariboga,
                                   java.net.InetAddress sender)
Adds a kaariboga to the base, performs some initialisation and starts it's thread. This method should be called to add a newly created Kaariboga agent to the base.

kaaribogaDispatchRequest

public void kaaribogaDispatchRequest(KaaribogaEvent e)
Handles the dispatch request of a kaariboga.
Specified by:
kaaribogaDispatchRequest in interface KaaribogaListener

kaaribogaSleepRequest

public void kaaribogaSleepRequest(KaaribogaEvent e)
Handles the sleep request of a kaariboga.
Specified by:
kaaribogaSleepRequest in interface KaaribogaListener

kaaribogaDestroyRequest

public void kaaribogaDestroyRequest(KaaribogaEvent e)
Handles the destroy request of a kaariboga.
Specified by:
kaaribogaDestroyRequest in interface KaaribogaListener

getKaaribogaNames

public java.util.Enumeration getKaaribogaNames(java.lang.Object sender)
Gets the names of all kaariboga agents on this base.
Parameters:
sender - The object that sends the request.

generateName

public java.lang.String generateName()
Generates a unique name that can be used to initialize agents.

getBaseAddress

public KaaribogaAddress getBaseAddress(java.lang.Object sender)
Returns the KaaribogaAddress of this base.
Parameters:
sender - The object that requests the address.

goOnlineForDynamicIP

public void goOnlineForDynamicIP(java.lang.Object sender)
                          throws java.net.UnknownHostException,
                                 java.io.IOException,
                                 java.lang.InterruptedException
If the server has no static IP address this method is called to determine the dynamic IP address.
Parameters:
sender - The object that calls this method

goOnline

public void goOnline(java.lang.Object sender,
                     int portNo,
                     KaaribogaAddress boServer,
                     KaaribogaAddress ipServer)
Performs all neccessary actions to go online
Parameters:
sender - The object that calls this method. Only the parent of this class may call this method, agents are not allowed to do this.
portNo - The port on which the server should listen
boServer - The domain server, if there is one. May be null.
ipServer - The server that determines this host's IP address if dynamic IP addresses are used

goOffline

public void goOffline(java.lang.Object sender)
Performs all neccessary actions to go offline
Parameters:
sender - The object that calls this method. Only the parent of this class may call this method, agents are not allowed to do this. XXX This method performs no check for the threads that are still running

getOnlineStatus

public boolean getOnlineStatus()
Determines if the server is online. An agent could check the online state before it tries to travel to another server.

destroyKaariboga

public void destroyKaariboga(java.lang.Object sender,
                             java.lang.String name)
Destroys a kaariboga agent with all necessary clean up.
Parameters:
sender - Object that calls this method.
name - Name of the kaariboga agent.

dispatchKaariboga

public void dispatchKaariboga(java.lang.Object sender,
                              java.lang.String name,
                              KaaribogaAddress destination)
Dispatches a kaariboga agent with all necessary clean up.
Parameters:
sender - Object that calls this method.
name - Name of the kaariboga agent.
destination - Where to send the kaariboga.

addBaseListener

public void addBaseListener(BaseListener l)
Adds a base event listener.

removeBaseListener

public void removeBaseListener(BaseListener l)
Removes a base event listener.

fireKaaribogaCreated

protected void fireKaaribogaCreated(java.lang.String name)
Fires an event if a new kaariboga was created.
Parameters:
name - Name of the kaariboga agent.

fireKaaribogaArrived

protected void fireKaaribogaArrived(java.lang.String name)
Fires an event if a new kaariboga has arrived.
Parameters:
name - Name of the kaariboga agent.

fireKaaribogaDestroyed

protected void fireKaaribogaDestroyed(java.lang.String name)
Fires an event if a kaariboga has been destroyed.
Parameters:
name - Name of the kaariboga agent.

fireKaaribogaLeft

protected void fireKaaribogaLeft(java.lang.String name)
Fires an event if a kaariboga was dispatched to another base.
Parameters:
name - Name of the kaariboga agent.

kaaribogaMessage

public void kaaribogaMessage(KaaribogaMessageEvent e)
Handles messages that are fired by kaaribogas.
Specified by:
kaaribogaMessage in interface KaaribogaMessageListener

addMessageHandler

public boolean addMessageHandler(KbMessageHandler handler,
                                 java.lang.String kind)
Adds a message handler for a specified kind of message to this base. The ability of a base to handle messages may be extended by different message handlers. There may be more than one message handler for one kind of message.
Parameters:
handler - The message handler, that wants to receive the specified kind of message
kind - The kind of message, that should be delivered to the message handler
Returns:
True, if successfull
See Also:
KaaribogaMessage

removeMessageHandler

public boolean removeMessageHandler(KbMessageHandler handler,
                                    java.lang.String kind)
Removes a message handler for a specified kind of message to this base. The message handler must be the same as the one that registered for this kind of message.
Parameters:
handler - The message handler, that wants to receive the specified kind of message
kind - The kind of message, that should be delivered to the message handler
Returns:
True if successfull
See Also:
KaaribogaMessage

addService

public boolean addService(KbPlugIn plugIn,
                          java.lang.String name)
Adds a service to this base. A plug-in may provide different services. Agents may use the service name to get access to a service object.
Parameters:
plugIn - The plugIn that provides the service
name - Name of the service

removeService

public boolean removeService(KbPlugIn plugIn,
                             java.lang.String name)
Removes the service named name from this base. + The service can only be removed by the plug-in that provided it. A plug-in may provide more than one services. So this method does not neccessary remove the plug-in.
Parameters:
plugIn - The plugIn that provides the service
name - Name of the service

getServiceNames

public java.lang.String[] getServiceNames(java.lang.Object sender)
Returns the names of all the services currently available. A service is provides by a plug-in.
Parameters:
sender - The object that calls this method.
Returns:
Names of the available services.

getServiceObject

public java.lang.Object getServiceObject(java.lang.Object sender,
                                         java.lang.String name)
Returns a service object for a specified service name. The caller of this method must make shure, that the returned object is of the expected type.
Parameters:
sender - The object that calls this method.
name - Name of the requested service
Returns:
An object that implements the requested Service, null if the object is not available or the sender is not allowed to request the object.