O'Reilly logo

Mobile Agents by Wilhelm R. Rossak, Peter Braun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

258 Chapter 7 Using Kalong
The migration commands of Kalong are very low level, of course. To make
programming of migration strategies easier, the mobile agent toolkit can
define new levels of abstraction on top of Kalong, for example, to bundle
often used sequences of commands into new commands so that program-
ming of migration strategies becomes more comfortable for the programmer.
We will show the Tracy2 approach for this in Chapter 10.
7.2 Using the Kalong Component
In this chapter we give an introduction to the use of Kalong as a software
component and to program migration strategies. This chapter does not con-
tain the full documentation of all classes of the Kalong component. Some
deeper introduction into the main classes and the overall design of Kalong
can be found in the following section. The full documentation of all classes
can be found online at www.mobile-agents.org.
7.2.1 Starting and Configuring Kalong
The main class of the Kalong software component is class Kalong in package
de.fsuj.kalong. It has the following two constructors:
public Kalong()
Creates a new Kalong component.
public Kalong( INetwork network )
Creates a new Kalong component that uses the given network component.
To embed Kalong in an existing mobile agent toolkit, it must be connected
to the agent manager and the network (as long as it was not done with the
constructor already).
public void registerNetwork( INetwork network )
Registers a network component with this instance of Kalong.
public void registerListener( IAgentManager listener )
Registers an agent manager with this instance of Kalong.
Finally the network component must be able to inform Kalong
about incoming messages. For this task, Kalong offers the interface
7.2 Using the Kalong Component 259
INetworkServer, for which Kalong already provides an implementation.
This implementation can be requested using the following method:
public INetworkServer getNetworkServerInterface()
Returns an implementation of interface INetworkServer.
The following method is to check whether an agent context already exists.
public boolean existsAgentContext( String agentName )
Returns true if an agent context with the given name already exists.
The last method is used to delete an agent context.
public void deleteAgentContext( String agentName )
Deletes an agent context locally.
When an agent context is deleted, all information about data items, object
state, and code units are removed from the current agency. Calling this
method does not kill an agent currently roaming the agent system. How-
ever, this agent can no longer use this agency for downloading data items or
code units, which might cause unexpected behavior or might even crash the
agent.
In the following example, we show how to start and configure a Kalong
instance.
1 package test;
2
3 import de.fsuj.kalong.Kalong;
4 import de.fsuj.kalong.IKalong;
5 import de.fsuj.kalong.Network;
6 import de.fsuj.kalong.ProtocolEngine;
7 import de.fsuj.kalong.tcp.TCPEngine;
8
9 public class StartKalong
10 {
11 public static void main( String[] args )
12 {
13 Kalong kalong = null;
14 IKalong iKalong = null;
15 Network network = null;
16 ProtocolEngine tcpProtocol = null;
17
18 kalong = new Kalong();
260 Chapter 7 Using Kalong
An example for a network component is also part of the Web site. The
main class of this component is Network, which works as a manager for
several network transmission protocols. Each transmission protocol must be
implemented by extending class ProtocolEngine.
19 network = new Network();
20 tcpProtocol = new TCPEngine();
21 network.registerProtocolEngine( tcpProtocol );
22 tcpProtocol.startServer( 5555 );
Each protocol engine defines a protocol name that can be used to define
URLs. For example, class TCPEngine defines the protocol with name tcp”.
In line 22 a new thread is started that will accept incoming messages on
port 5555.
Now Kalong must be connected to the network component. Because the
network component is an independent software component, rather than
implementing the interfaces of Kalong, it provides interfaces on its own. To
allow communication between these two interfaces, we implement adapter
classes. For example, for Kalong we create an adapter that implements the
Kalong interface INetwork and accesses the network component transpar-
ently. For the other communication direction, we need another adapter
class that implements an interface of the network components and directs
all method invocations to Kalong. We do not print the source code of both
classes here; the source code can be found on the Web site.
23 NetworkAdapter nAdapter = new NetworkAdapter( network );
24 KalongAdapter kAdapter = new KalongAdapter
( kalong.getNetworkServerInterface());
25 kalong.registerNetwork( nAdapter );
26 network.registerListener( kAdapter );
27 kalong.registerListener( new KalongListener() );
28 }
29 }
In line 27 we register a listener object with Kalong, which will be
informed in case of received agents. This listener must implement interface
IAgentManager; we will show an example of this listener in Section 7.2.3.
The last method of class Kalong is used by the agent manager to request
an implementation of interface IKalong, which contains all functions to
access an agent context and program migration strategies.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required