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 deﬁnes a protocol name that can be used to deﬁne
URLs. For example, class TCPEngine deﬁnes the protocol with name “tcp”.
In line 22 a new thread is started that will accept incoming messages on
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
25 kalong.registerNetwork( nAdapter );
26 network.registerListener( kAdapter );
27 kalong.registerListener( new KalongListener() );
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.