Title: Mobile Agents
1Mobile Agents
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Reference
- D. B. Lange and M. Oshima, Programming and
Deploying Java Mobile Agents with Aglets,
Addison-Wesley, 1998
3Mobile Agents
- A mobile agent is not bound to the system where
it begins execution. It has the unique ability
to transport itself from one system in a network
to another. The ability to travel allows a
mobile agent to move to a system that contains an
object with which the agent wants to interact and
then to take advantage of being in the same host
or network as the object
4Seven Good Reasons for Using Mobile Agents
- Reduce the network load
- Overcome network latency
- Encapsulate protocols
- Execute asynchronously and autonomously
- Adapt dynamically
- Naturally heterogeneous
- Robust and fault-tolerant
5Network Load Reduction
Application
Service
Host A
Host B
Application
Service
6Disconnected Operation
Application
Service
Application
Service
Application
Service
7Client-Server Paradigm
Server
Client
Know- how
8Code-on-Demand Paradigm
Server
Know- how
Client
Know- how
9Mobile Agent Paradigm
10Mobile Agent Attributes
Principals
Interface
Implementation
State
Identifier
11State
- Needed for the agent to resume computation after
traveling - A snapshot of the agents execution
- Execution state
- Object state
12Implementation
- Needed for location-independent agent execution
- Either taking the implementation code or going to
the destination, seeing what code is already
there, and retrieving any missing code over the
network - Executable at the destination host
- Safe for the host to execute
13Interface
- Needed for agent communication
- Can be anything from a set of method signatures
that allow other agents and applications to
access to a messaging interface that allows
agents to communicate in agentspeak such as KQML
14Identifier and Principals
- Identifier
- Needed to recognize and locate traveling agents
- Unique during its lifetime (immutable)
- Principals
- Needed to determine legal and moral
responsibility - Can be an individual, an organization, or a
corporation - Two main principals Manufacturer and owner
15Place
Host
Agent
Place
Resources
Engine
16Engine
- Workhorse and virtual machine for one or more
places - Provides places and agents with links to the
underlying network and other resources provided
by the host - In a Java-based mobile agent system, the engine
is likely to be identical with the Java virtual
machine and the operating system
17Resources, Location, and Principals
- Resources
- Such as networks, databases, processors, memory,
disks, and other services - Location
- Combination of the name of the place and the
network address of the engine - Typically as a IP address and a port of the
engine with a place name attribute - Principals
- Manufacturer and the place master
18Agent Creation
- Initiated either by another agent or nonagent
system - Three steps
- Instantiation and identifier assignment
- Initialization
- Autonomous execution
19Agent Disposal
- Initiated by the agent itself, by another agent
or nonagent system - Can be disposed of by the system for
- End of lifetime
- No use
- Security violation
- Shutdown
- Two steps
- Preparing for disposal
- Suspension of execution
20Agent Transfer
Receiver
Resume Execution
Network
21Agent Class Transfer
Server
Class Code
Origin
Agent
Class Code
22Agent Communication
Now-Type
Future-Type
One-Way-Type
Receiver
Receiver
Receiver
Sender
Sender
Sender
23Aglet and Proxy
- Aglet
- A mobile Java object
- Autonomous
- Reactive
- Proxy
- Representative of an aglet
- Protects the aglet from direct access
- Provides location transparency for the aglet
- Identifier
24Aglet and Proxy
25Context
- An aglets workspace
- Provides a means for maintaining and managing
running aglets in a uniform execution environment
where the host system is secured against
malicious aglets - Can be located by the combination of the servers
address and the contexts name
26Context
Host
Context
Context
Context
Server Process (Engine)
Network
27Aglet Life-Cycle Model
Context B
Context A
Dispatch
Aglet
Aglet
Dispose
Clone
Retract
Create
Deactivate
Activate
Class File
Disk Storage
28Aglet Event Model
29Aglet Communication Model
Message
Message
Aglet/ Application
Proxy
Aglet
Reply
Reply
30Class Aglet (1)
- Some final methods
- Object clone()
- void dispatch()
- void dispose()
- void deactivate()
- void snapshot()
- void addCloneListener()
- void addMobilityListener()
- void addPersistencyListener()
- AgletInfo getAgletInfo()
- AgletID getAgletID()
- AgletProxy getProxy()
- AgletContext getAgletContext()
31Class Aglet (2)
- Some methods
- void run()
- void onCreation()
- void onDisposing()
- void waitMessage()
- void notifyMessage()
- boolean handleMessage()
32Interface AgletProxy
- Some methods
- Object clone()
- AgletProxy dispatch()
- void dispose()
- void deactivate()
- void activate()
- Object sendMessage()
- FutureReply sendFutureMessage()
- void sendOnewayMessage()
- boolean isRemote()
33Interface AgletContext
- Some methods
- AgletProxy createAglet()
- AgletProxy retractAglet()
- ReplySet multicastMessage()
- Object getProperty()
- void setProperty()
- void start()
- void shutdown()
34Class Message
- Some methods
- Message(String kind)
- Message(String kind, Object value)
- String getKind()
- boolean sameKind()
- long getTimeStamp()
- void sendReply()
- void getArg()
- void setArg()
35Example of Message Handling
- Message myName new Message(my name, Jacob)
- Message yourName new Message(your name?)
- Proxy.sendMessage(myName)
- String name (String)proxy.sendMessage(yourName)
- -------------------------------------------------
- Public boolean handleMessage(Message msg)
- if(msg.sameKind(my name))
- String name (String)msg.getArg()
- return true
- else if(msg.sameKind(your name?))
- msg.sendReply(Yina)
- return true
- else
- return false
36Class FutureReply
- Some methods
- Object getReply()
- boolean isAvailable()
- void waitForReply()
- Example
-
- FutureReply future
- proxy.sendFutureMessage(msg)
- while(!future.isAvailable())
- doPeriodicWork()
-
- Object reply future.getReply()
37Remote File Update
Host
Host
Host
Aglet
Aglet
Aglet
Aglet
Host (Updating)
38Remote File Update Client Agent
- Object args new Object
- new URL(atp//java.trl.ibm.co.jp),
- new File(C/public/test.dat),
- abc,
- xyz
- AgletContext context getAgletContext()
- Context.CreateAglet(null, UpdateFile, args)
39Class UpdateFile (1)
- Public class UpdateFile extends Aglet
- URL destination null
- File file null
- String from null
- String to null
- public void onCreation(Object args)
- destination (URL)((Object)args)0
- file (File)((Object)args)1
- from (String)((Object)args)2
- to (String)((Object)args)3
- addMobilityListener(
- new MobilityAdapter()
- public void onArrival(MobilityEvent e)
- replace(file, from, to)
- dispose() )
40Class UpdateFile (2)
- try
- dispatch(destination)
- catch (Exception e)
- System.out.println(Failed to dispatch)
-
-
- void replace(File file, String from,
- String to)
- // Open file and replace from with to
-
-
41Aglet Creation
Context. createAglet()
Aglet. Aglet()
Aglet. onCreation()
Aglet. run()
42Class CreationExample
- public class CreationExample extends Aglet
- public void run()
- try
- getAgletContext().createAglet(
- getCodeBase(), CreationChild, null )
- catch (Exceptioin e)
- System.out.println(e.getMessage())
-
-
-
- Public class CreationChild extends Aglet
- public CreateChild()
- public void onCreation(Object init)
- public void run()
43Aglet Disposal
Aglet. dispose()
Aglet. onDisposing()
Aglet. run()
44Aglet Event Model
- Events
- CloneEvent
- MobilityEvent
- PersistencyEvent
- Listeners
- CloneListener
- MobilityListener
- PersistencyListener
- Adapters
- CloneAdapter
- MobilityAdapter
- PersistencyAdapter
45Cloning
Aglet. run()
CloneAdapter. onCloning()
Original
CloneAdapter. onCloned()
CloneAdapter. onClone()
Clone
Aglet. run()
46Class CloningExample
- public class CloningExample extends Aglet
- boolean theClone false
- public void onCreation(Object o)
- addCloneListener(
- new CloneAdapter()
- public void onCloning(CloneEvent e)
- public void onClone(CloneEvent e)
- theClone true
- public void onCloned(CloneEvent e)
- )
-
- public void run()
- if(!theClone)
- try clone()
- catch( Exception e )
- else
-
47Dispatching
Aglet. run()
Aglet. dispatch()
Origin
MobilityAdapter. onDispatching()
MobilityAdapter. onArrival()
Destination
Aglet. run()
48Class DispatchingExample (1)
- public class DispatchingExample extends Aglet
- boolean theRemote false
- public void onCreation(object init)
- addMobilityListener(
- new MobilityAdapter()
- public void onDispatching(MobilityEvent
e) -
- //
-
- public void onArrival(MobilityEvent e)
- theRemote true
- //
-
-
- )
-
49Class DispatchingExample (2)
- public void run()
- if(!theRemote)
- try
- URL destination new URL(
- (String)getAgletContext().
- getProperty(location) )
- dispatch( destination )
- catch (Exception e)
- System.out.println(e.getMessage())
-
- else
- // The remote aglet runs here
-
-
50Retracting
AgletContext. retractAglet()
MobilityAdapter. onArrival()
Local
Aglet. run()
Aglet. run()
Remote
MobilityAdapter. onReverting()
51Class RetractionExample
- public class RetractionExample extends Aglet
- public void run()
- try
- AgletProxy proxy
- getAgletContext().createAglet(null,
- RetractionChild, null)
- URL destination
- new URL(atp//some.host.com)
- AgletID aid proxy.getAgletID()
- proxy.dispatch(destination)
- getAgletContext().
- retractAglet(destination, aid)
- catch (Exception e)
- System.out.println(e.getMessage())
-
-
52Class RetractionChild
- public class RetractionChild extends Aglet
- boolean reverted false
- boolean dispatched false
- public void onCreation(Object init)
- addMobilityListener(
- new MobilityAdapter()
- public void onReverting(MobilityEvent e)
- reverted true
-
- public void onArrival(MobilityEvent e)
- dispatched true
-
- )
-
- public void run()
-
53Persistence
Aglet. run()
Aglet. Deactivate()
Before
PersistencyAdapter. onDeactivating()
PersistencyAdapter. onActivation()
After
Aglet. run()
54Class DeactivationExample (1)
- public class DeactivationExample extends Aglet
- private static long SECOND 1000
- boolean activated false
- public void onCreation(Object init)
- addPersistencyListener(
- new PersistencyAdapter()
- public void onDeactivating(
- PersistencyEvent e)
- public void onActivation(
- PersistencyEvent e)
- activated true
-
-
- )
-
55Class DeactivationExample (2)
- public void run()
- if(!activated)
- try
- deactivate( 10SECOND )
- catch (Exception e)
- System.out.println(e.getMessage())
-
- else
- //
-
-
56Directory Listing
Remote Host
Origin
Dispatch
List Directory
Dispatch
Display List
57Class ListingAglet (1)
- public class ListingAglet extends Aglet
- boolean back false
- File dir new File(C\\Public)
- String list
- URL origin null
- public void onCreation(Object o)
- addMobilityListener(
- new MobilityAdapter()
- public void onArrival(MobilityEvent me)
- if( back )
- for(int i0 iltlist.length )
- System.out.println(I
- listi)
- dispose()
- else
58Class ListingAglet (2)
- try
- list dir.list()
- back true
- dispatch(origin)
- catch ( Exception e )
- dispose()
-
-
-
-
- )
- origin getAgletContext().getHostingURL()
- try
- dispatch(
- new URL(apt//killi.genmagic.com))
- catch( Exception e )
-
59A Traveling Aglet
Origin
dest-1
dispatch(dest-1)
dest-2
dest-1
dispatch(dest-2)
dest-n
dest-
dispatch(dest-n)
(result)
dest-1
60Class ControllerExample
- public class ControllerExample extends Aglet
- public void run()
- try
- AgletProxy proxy getAgletContext().
- createAglet(getCodeBase(),
- ControllerChild, null)
- Vector itinerary new Vector()
- itinerary.addElement(new URL(dest-1))
- itinerary.addElement(new URL(dest-2))
- itinerary.addElement(new URL(dest-n))
- Enumeration enum itinerary.elements()
- while(enum.hasMoreElements())
- proxy
- proxy.dispatch((URL)enum.nextElement())
- catch (Exception e)
-
61Class ControllerChild
- public class ControllerChild extends Aglet
- public void onCreation(Object o)
- addMobilityListener(
- new MobilityAdapter()
- public void onArrival(MobilityEvent me)
- doTask()
-
-
- )
-
- void doTask()
- //
-
62Finding an Aglet
- May be necessary when the aglet is autonomous
- Three possibilities
- Search
- Sending a search aglet
- Using multicast
- Logging
- Every aglet leaves an electronic fingerprint
- Registration
- Every aglet registers its current location in a
database
63AgletFinder Example
Traveler
Register (MobilityListener)
3 Hello
1Register
Traveler Finder
Aglet Finder
2Lookup
64Class AgletFinder
- public class AgletFinder extends
- Hashtable database new Hashtable()
- public void onCreate(Object init)
- getAgletContext().setProperty(AgletFinder,
- getProxy())
-
- public boolean handleMessage(Message msg)
- if(msg.sameKind(Lookup))
- msg.sendReply(database.get(msg.getArg()))
- else if(msg.sameKind(Register))
- database.put(msg.getArg(NAME),
- msg.getArg(PROXY))
- else if(msg.sameKind(Unregister))
- database.remove(msg.getArg(NAME))
- return true
-
65Class Register (1)
- public class Register extends MobilityAdapter
- AgletProxy finder
- Message msg new Message(Register)
- public Register(AgletProxy finder, String
name) - msg.setArg(NAME, name)
- this-gtfinder finder
-
- public void onArrival(MobilityEvent me)
- try
- msg.setArg(PROXY, me.getAgletProxy())
- finder.sendMessage(msg)
- doTask()
-
- catch(Exception e)
-
-
66Class Register (2)
- public void unregister()
- try
- Message unregister
- new Message(Unregister)
- unregister.setArg(NAME,
- msg.getArg(NAME))
- finder.sendMessage(unregister)
- catch (Exception e)
-
-
67Class Traveler
- public class Traveler extends Aglet
- Register register null
- public void onCreation(Object o)
- AgletProxy finder (AgletProxy)
getAgletContext().getProperty(AgletFinder) - register new Register(finder, Traveler)
- addMobilityListener( register )
- try
- dispatch(destination)
- catch (Exception e)
-
- public void onDisposing()
- register.unregister()
-
- public boolean handleMessage(Message msg)
- if (msg.sameKind(Hello)) return true
-
68Class TravelerFinder
- public class TravelerFinder extends Aglet
- AgletProxy finder null
- public void onCreation(Object o)
- finder (AgletProxy) getAgletContext().
- getProperty(AgletFinder)
-
- public void run()
- try
- AgletProxy traveler
- (AgletProxy) finder.sendMessage(
- new Message(Lookup,Traveler) )
- traveler.sendMessage(new Message(Hello))
- catch (Exception e)
-
-
69Aglets in Parallel Execution
Origin
dispatch(dest-n)
and sendFutureMessage(doTask)
Origin
70Class Worker
- public class Worker extends Aglet
- public boolean handleMessage(Message msg)
- if (msg.sameKind(DoTask))
- msg.sendReply(doTask())
- dispose()
- return true
-
71Class ParallelExample (1)
- public class ParallelExample extends Aglet
- public void onCreation(Object init)
- Vector itinerary new Vector()
- try
- itinerary.addElement(new URL(dest-1))
- itinerary.addElement(new URL(dest-2))
- itinerary.addElement(new URL(dest-n))
- catch (Exception e)
-
- AgletContext context getAgletContext()
- Message doTask new Message(DoTask)
- ReplySet replies new ReplySet()
72Class ParallelExample (2)
- Enumeration enum itinerary.elements()
- while (enum.hasMoreElements())
- try
- AgletProxy worker context.createAglet(
- getCodeBase(), Worker, null)
- worker
- worker.dispatch((URL)enum.nextElement())
- replies.addFutureReply(worker.
- sendFutureMessage(doTask))
- catch (Exception e)
-
- while (replies.hasMoreFutureReplies())
- try
- FutureReply future
- replies.getNextFutureReply()
- accumulateResults(future.getResult())
- catch (Exception e)
-
73Class Worker1
- public class Worker1 extends Aglet
- public void onCreation(Object init)
- AgletProxy parent (AgletProxy) init
- addMobilityListener(
- new MobilityAdapter()
- public void onArrival(MobilityEvent me)
- try
- parent.sendMessage(
- new Message(Result, doTask()))
- dispose()
- catch (Exception e)
-
-
-
- )
-
74Class ParallelExample1 (1)
- public class ParallelExample1 extends Aglet
- public void onCreation(Object init)
- Vector itinerary new Vector()
- try
- itinerary.addElement(new URL(dest-1))
- itinerary.addElement(new URL(dest-2))
- itinerary.addElement(new URL(dest-n))
- catch (Exception e)
-
- AgletContext context getAgletContext()
- AgletProxy parent getProxy()
- Enumeration enum itinerary.elements()
75Class ParallelExample1 (2)
- while (enum.hasMoreElements())
- try
- AgletProxy worker context.createAglet(
- getCodeBase(),
- Worker1,
- parent )
- catch (Exception e)
-
-
-
- public boolean handleMessage(Message msg)
- if (msg.sameKind(Result))
- accumulateResults(msg.getArg())
- return true
-
76Traveling Pattern
- Itinerary
- Objectifies aglets itineraries and routing among
destinations - Forwarding
- Provides a way for a host to forward newly
arrived aglets automatically to another host - Ticket
- Objectifies a destination address and
encapsulates the quality of service and
permissions needed to dispatch an aglet to a host
and execute it there
77Task Pattern
- Master-Slave
- Defines a scheme whereby a master aglet can
delegate a task to a slave aglet - Plan
- Provides a way of defining the coordination of
multiple tasks to be performed on multiple hosts
78Interaction Pattern (1)
- Meeting
- Provides a way for two or more aglets to initiate
local interaction at a given host - Locker
- Defines a private storage space for data left by
an aglet before it is temporarily dispatched
(sent) to another destination
79Interaction Pattern (2)
- Messenger
- Defines a surrogate aglet to carry a remote
message from one aglet to another - Finder
- Defines an aglet that provides services for
naming and locating aglets with specific
capabilities - Organized Group
- Composes aglets into groups in which all members
of a group travel together