Class ThreadAgent

java.lang.Object
  |
  +--ThreadAgent
All Implemented Interfaces:
MobileObject, java.lang.Runnable, java.io.Serializable

public class ThreadAgent
extends java.lang.Object
implements MobileObject, java.lang.Runnable

MobiliTools $Name: $, $Id: ThreadAgent.java,v 1.1.1.1 2003/03/28 14:47:58 dillense Exp $

This example class shows a straightforward implementation of a thread-based agent activity. The agent activity consists in continuously moving from agency to agency in its region. The agent takes an optional single integer argument, interpreted as a delay in ms (no argument means no delay). The easiest way to use this example is to use GUI-enabled agencies.
NB : THIS EXAMPLE REQUIRES THE MAFFINDER TO BE RUNNING BEFORE LAUNCHING AGENCIES AND AGENTS.

At creation, the agent gets the latest list of agencies. While this list contains only one agency (it cannot be empty!), the agent loops on updating the list and waiting for the specified delay. As soon as the list contains at least two agencies, the agent begins to move, waiting for the specified delay between two consecutive moves.

If the update of agency list fails, the agent suspends itself.

See Also:
Serialized Form

Field Summary
protected  boolean dead
          true if the agent is dead
protected  Location[] itinerary
          latest list of agencies in current region
protected  Agency my_agency
          current host agency
protected  Name my_name
          agent's name
protected  int pause
          delay in ms between consecutive moves or agency list updates
protected  int step
          index in list of agencies for next move
protected  boolean suspended
          true if the agent is suspended
 
Constructor Summary
ThreadAgent()
           
 
Method Summary
 void afterBirth(AgentSystem agency, AgentInfo entry, java.lang.Object arg)
          Initialize the agent and create a thread to run the agent's autonomous activity
 void afterMove(AgentSystem agency, Location dummy1, java.lang.String dummy2)
          Create a thread after move to go on agent's autonomous activity in new host agency.
 void afterMoveFailed(Location dummy1, java.lang.String dummy2, int dummy3, java.lang.String dummy4)
          Nothing special to do if the agent transport/setup phase failed during a move.
 void beforeDeath()
          Make the agent's thread stop as soon as possible.
 void beforeMove(Location dummy1, java.lang.String dummy2)
          Nothing special to do before moving.
 void beforeResume()
          Update the itinerary in current agency's region, and create a thread to run the agent's autonomous activity.
 void beforeShutdown()
          Nothing special to do before agency shutdown (just face upcoming death...)
 void beforeSuspend()
          Make the agent's thread stop as soon as possible.
protected  void resetItinerary()
          Update the list of agencies and set the itinerary index to zero.
 void run()
          Agent activity: wait for a while, then move to next agency in itinerary or update itinerary if it contains a single agency.
protected  void runActivity()
          Agent's thread creation.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

my_name

protected Name my_name
agent's name

my_agency

protected transient Agency my_agency
current host agency

itinerary

protected volatile Location[] itinerary
latest list of agencies in current region

pause

protected int pause
delay in ms between consecutive moves or agency list updates

step

protected int step
index in list of agencies for next move

dead

protected boolean dead
true if the agent is dead

suspended

protected boolean suspended
true if the agent is suspended
Constructor Detail

ThreadAgent

public ThreadAgent()
Method Detail

resetItinerary

protected void resetItinerary()
Update the list of agencies and set the itinerary index to zero. Suspend the agent if the list cannot be updated.

runActivity

protected void runActivity()
Agent's thread creation.

run

public void run()
Agent activity: wait for a while, then move to next agency in itinerary or update itinerary if it contains a single agency.
Specified by:
run in interface java.lang.Runnable

afterBirth

public void afterBirth(AgentSystem agency,
                       AgentInfo entry,
                       java.lang.Object arg)
                throws BadOperation
Initialize the agent and create a thread to run the agent's autonomous activity
Specified by:
afterBirth in interface MobileObject
Parameters:
arg - if a non-empty String[], should hold a delay in ms as a single value; this delay is applied between two consecutive moves or itinerary updates. Otherwise, this delay is zero.

beforeSuspend

public void beforeSuspend()
Make the agent's thread stop as soon as possible.
Specified by:
beforeSuspend in interface MobileObject

beforeResume

public void beforeResume()
                  throws BadOperation
Update the itinerary in current agency's region, and create a thread to run the agent's autonomous activity.
Specified by:
beforeResume in interface MobileObject

beforeMove

public void beforeMove(Location dummy1,
                       java.lang.String dummy2)
Nothing special to do before moving.
Specified by:
beforeMove in interface MobileObject

afterMove

public void afterMove(AgentSystem agency,
                      Location dummy1,
                      java.lang.String dummy2)
Create a thread after move to go on agent's autonomous activity in new host agency.
Specified by:
afterMove in interface MobileObject

afterMoveFailed

public void afterMoveFailed(Location dummy1,
                            java.lang.String dummy2,
                            int dummy3,
                            java.lang.String dummy4)
Nothing special to do if the agent transport/setup phase failed during a move.
Specified by:
afterMoveFailed in interface MobileObject

beforeDeath

public void beforeDeath()
Make the agent's thread stop as soon as possible.
Specified by:
beforeDeath in interface MobileObject

beforeShutdown

public void beforeShutdown()
Nothing special to do before agency shutdown (just face upcoming death...)
Specified by:
beforeShutdown in interface MobileObject