Next: Auxiliary Methods
Up: Agent Class
Previous: Agent Class
  Contents
When an Agent is stopped for being moved to another Agent System it has to save
its execution state before. Java does not provide a method to save or serialize
the current stack of a program. There are several mechanism used in the other
Mobile Agent Systems written in Java to hold the information of
the current execution state. The main thing is, that the Agent has to continue
its execution in a given method. It is not possible to know a certain point
/ code line where to start from. Grasshopper uses a kind of start method with
a global variable set to the next block to execute in this method. The start
method is called every time the Agent is (re-) started. Before being stopped,
for example when request to travel, it has to save the next state into this
global variable.
The TAgents Mobile Agent System uses a similar, but more flexible
way of defining the next block to execute. The basic Agent
class provides two method, setNextCommand(String cmd) and delNextCommand(String
cmd), to define the name of the method to call the next time when started.
Internally the Agent class contains a Vector where the commands
are put into. The run() method of the Agent class
checks if there is a command in the command queue (the Vector
cmdQueue ) and calls the method if a command is available. By using
a Vector as holder for the commands it is possible for an
Agent to define a list of commands to execute in order. This means that an Agent
can give a list of methods which are called one after the other. When a Agent
is serialized the Vector will be serialized too and after
deserialization it will contain the same commands as before. When an Agent is
restarted (resumed) again its run() will take the next command out
of the command queue and call this method.
The first method to call should be put into the command queue when the Agent
is initialized. It is not a good idea to put it in the start method since this
method will be called every time an Agent is restarted again, for example after
traveling.
The run() method of the Agent contains the following lines:
public final void run() {
while(!concluded) {
if(running && !cmdQueue.isEmpty()) {
String cmd = (String) cmdQueue.firstElement();
executing = true;
cmdQueue.removeElement(cmd);
dispatcher(cmd);
executing = false;
}
else {
try {
sleep(1000);
} catch(InterruptedException e) {}
}
}
}
The run method continues looping while the Agent is not concluded, means while
the boolean variable conclude is false. When calling the terminateAgent
method this variable will be set to false causing the thread to stop executing.
The boolean variable running is true while the Agent is not stopped.
While this variable is true the Agent looks into the command queue and executes
the next command if one is available. The execution of the command string is
done by the dispatcher. The dispatcher compares all methods
defined by the Agent for a matching to the given command string. When a matching
method is found this will be executed. The dispatcher knows about all
the methods defined in the actual class and all subclasses. So it can process
a command to a subclass method as well.
The Agent provides a method isExecuting() which returns the boolean
variable executing. this variable is set to true when a new command
is processed. This means that it returns true when the Agent is currently executing
a method.
Next: Auxiliary Methods
Up: Agent Class
Previous: Agent Class
  Contents
Thomas Letsch
2001-02-21