|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectdimawo.agents.AbstractAgent
public abstract class AbstractAgent
Agents are well suited to the design of complex parallel applications involving mostly asynchronous operations. The behavior of an application composed of agents emerges from the exchange of messages in an asynchronous way between the agents of the application. Agent based programming (ABP) can be seen as an asynchronous form of object-oriented programming (OOP): the execution of the application is based on agents (objects) exchanging messages (method calls). The main difference between the 2 approaches is that in ABP, calls are asynchronous (a call does not immediately generate a result).
This class represents an asynchronous agent template. Messages submitted to
an instance of this class are inserted into a blocking queue by
submitMessage
. This method is protected so
subclasses of AbstractAgent are able to statically (i.e. at compile time)
"control" the type of the messages they accept (for example,
only String
objects) by defining particular
submission methods (for example, submitString(String)).
An asynchronous agent can be in 3 states: init, running and stopped. Init state indicates the agent has been instantiated but has not yet been started (i.e. it does not yet handle messages, though these can already be inserted into message queue). Running state implies the agent is currently handling messages. Finally, stopped state means the agent has been stopped and no more messages are going to be handled.
The agent is in its initial state after it has been instantiated. An agent
changes to running state after a call to start
method. A call to start
starts the thread that will
execute message handling code (see run
).
Before any message is handled, the init
method is
called (this method must be defined by a subclass of AbstractAgent).
After that, the message handling code extracts each message from
queue and handles it according to its type
(see messageHandling
method).
If init
throws a Throwable, the agent changes to stopped state and no
message is handled.
Stopped state can also be reached if the handling of a message causes a
Throwable to be thrown. Finally, a call to stop
inserts a
special message into queue. The handling of this message causes the agent
to move to stopped state. This implies that the effect of a call to
stop
is not immediate.
As soon as an AbstractAgent reaches stopped state, exit
method
is executed and after that, the thread executing the message handling code
finishes its execution.
When stopped state was reached because a Throwable was thrown by either
init
or messageHandling
, the error
is forwarded to an ErrorHandler
, if any was set,
before exit
is called.
An AbstractAgent is also an ErrorHandler:
signalChildError
inserts a message
into queue. A default behaviour for this message handling is implemented by
handleChildException(UncaughtThrowable)
. Of course, This method can
be overriden.
Finally, an AbstractAgent has a particular logging interface: by default,
all messages printed using
agentPrintMessage(int, String)
or agentPrintMessage(Throwable)
are printed onto standard output (System.out). However, agent's logging
stream can be changed by setPrintStream
.
A name can be associated to an agent, this name is used by logging and is used to name the thread running the message handling code.
Nested Class Summary | |
---|---|
static class |
AbstractAgent.AgentState
Represents the 3 possible states of an AbstractAgent |
Field Summary | |
---|---|
protected String |
agentName
The name of the agent (used for logging and thread name). |
protected ErrorHandler |
errorHandler
Error handler associated to this agent. |
Constructor Summary | |
---|---|
AbstractAgent()
Instantiates an AbstractAgent without error handler and
name. |
|
AbstractAgent(ErrorHandler parent,
String name)
Instantiates an AbstractAgent having given error handler and
name. |
|
AbstractAgent(ErrorHandler parent,
String name,
boolean daemon)
Instantiates an AbstractAgent having given error handler and
name. |
|
AbstractAgent(ErrorHandler parent,
String name,
boolean daemon,
int capacity)
Instantiates an AbstractAgent having given error handler and
name. |
|
AbstractAgent(ErrorHandler parent,
String name,
int capacity)
Instantiates an AbstractAgent having given error handler and
name. |
Method Summary | |
---|---|
void |
agentPrintMessage(int verbLevel,
String message)
Logs a message on standard output if given verbosity level is high enough. |
void |
agentPrintMessage(String message)
Logs a message on standard output if current verbosity level is lesser or equal to 0. |
void |
agentPrintMessage(Throwable e)
Logs the stack trace of given Throwable instance on
standard output. |
protected abstract void |
exit()
Implements the operations to be executed when agent enters STOPPED state. |
protected LinkedList<Object> |
flushPendingMessages()
Lists messages from messages queue and clears it. |
static int |
getDefaultVerbosityLevel()
Returns the default verbosity level of agents. |
AbstractAgent.AgentState |
getState()
Provides the current state of the agent. |
int |
getVerbosityLevel()
Returns the verbosity level of this agent. |
protected void |
handleChildException(UncaughtThrowable o)
Handles an error event taken from message queue. |
protected abstract void |
handleMessage(Object o)
Handles a new message taken from the queue. |
protected abstract void |
init()
Implements the operations to be executed when agent enters INIT state. |
void |
join()
Waits until the agent's thread is stopped. |
void |
join(long millis)
Waits until the agent's thread is stopped or a time-out occurs. |
void |
run()
This method implements the Runnable interface. |
protected void |
setAgentName(String name)
Sets the name of this agent. |
void |
setDaemon(boolean on)
Sets the daemon flag of the thread that will execute message handling code. |
static void |
setDefaultVerbosityLevel(int verbLevel)
Sets the default verbosity level for all agents that will be instantiated after this call. |
void |
setErrorHandler(ErrorHandler err)
Sets the ErrorHandler associated to this agent. |
protected void |
setPrintStream(PrintStream printStream)
Sets the default print stream this agent logs to. |
void |
setVerbosityLevel(int verbLevel)
Sets the verbosity level for this agent. |
void |
signalChildError(Throwable t,
String childName)
Inserts an error event in message queue. |
void |
start()
Starts this agent. |
void |
stop()
Inserts a StopAgent event in the queue of this agent. |
protected void |
submitError(Throwable t)
Inserts a Throwable into message queue. |
protected void |
submitMessage(Object o)
Queues a message in messages queue. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected String agentName
protected ErrorHandler errorHandler
Constructor Detail |
---|
public AbstractAgent()
AbstractAgent
without error handler and
name. The message handling thread is not a daemon and the capacity
of message queue is not limited.
This constructor is equivalent to
AbstractAgent(ErrorHandler, String)
constructor called with the 2 arguments having null
as value.
public AbstractAgent(ErrorHandler parent, String name)
AbstractAgent
having given error handler and
name. The message handling thread is not a daemon and the capacity
of message queue is not limited.
This constructor is equivalent to
AbstractAgent(ErrorHandler, String, boolean)
constructor called with AbstractAgent(parent, name, false)
.
parent
- The error handler to associate to this agent.name
- The name to associate to this agent.ErrorHandler
public AbstractAgent(ErrorHandler parent, String name, boolean daemon)
AbstractAgent
having given error handler and
name. The capacity of message queue is not limited.
This constructor is equivalent to
AbstractAgent(ErrorHandler, String, boolean, int)
constructor called with AbstractAgent(parent, name, daemon, 0)
.
parent
- The ErrorHandler to associate to this agent.name
- The name to associate to this agent.daemon
- Daemon flag for the thread that will execute message
handling code.ErrorHandler
public AbstractAgent(ErrorHandler parent, String name, int capacity)
AbstractAgent
having given error handler and
name. The message handling thread is not a daemon.
This constructor is equivalent to
AbstractAgent(ErrorHandler, String, boolean, int)
constructor called with AbstractAgent(parent, name, false, capacity)
.
parent
- The ErrorHandler to associate to this agent.name
- The name to associate to this agent.capacity
- The capacity of the incoming messages queue
(see submitMessage
).ErrorHandler
public AbstractAgent(ErrorHandler parent, String name, boolean daemon, int capacity)
AbstractAgent
having given error handler and
name.
parent
- The ErrorHandler
to associate to this agent.name
- The name to associate to this agent.daemon
- Daemon flag for the thread that will execute message
handling code.capacity
- The capacity of the incoming messages queue
(see submitMessage
).ErrorHandler
Method Detail |
---|
public void setErrorHandler(ErrorHandler err)
ErrorHandler
associated to this agent.
This method must be called before the agent is started (i.e.
before a call to start()
start).
err
- The ErrorHandler.ErrorHandler
public void setDaemon(boolean on)
start()
).
on
- Value of the daemon flag.Thread.setDaemon(boolean)
public static void setDefaultVerbosityLevel(int verbLevel)
verbLevel
- A verbosity level (see
agentPrintMessage(int, String)
agentPrintMessage).public static int getDefaultVerbosityLevel()
public void setVerbosityLevel(int verbLevel)
verbLevel
- A verbosity level (see
agentPrintMessage(int, String)
agentPrintMessage).public int getVerbosityLevel()
public void stop() throws InterruptedException, AgentException
InterruptedException
- If the agent thread was interrupted.
AgentException
- If the agent is already stopped.public void start() throws AgentException
AgentException
- If the agent was already started.public AbstractAgent.AgentState getState()
public void join() throws InterruptedException
InterruptedException
- if this thread is interrupted.
AgentException
public void join(long millis) throws InterruptedException
millis
- The duration of the time-out given in milliseconds.
InterruptedException
- if this thread is interrupted.public void run()
Runnable
interface. It
describes the sequential code executed by message handling thread:
run
in interface Runnable
public void signalChildError(Throwable t, String childName)
signalChildError
in interface ErrorHandler
t
- The error.childName
- A name describing the error's source i.e. the
component that caused the error.protected void submitMessage(Object o) throws InterruptedException
o
- The message to insert into the queue.
InterruptedException
- If the call is blocking and executing
thread is interrupted.protected void handleChildException(UncaughtThrowable o) throws AgentException
o
- The error.
AgentException
- If the agent does not have an error handler.public void agentPrintMessage(int verbLevel, String message)
verbLevel
- Message's verbosity levelmessage
- The message to log.setVerbosityLevel(int)
public void agentPrintMessage(String message)
message
- The message to log.setVerbosityLevel(int)
public void agentPrintMessage(Throwable e)
Throwable
instance on
standard output.
e
- The given Throwable
instance.protected LinkedList<Object> flushPendingMessages()
protected void setPrintStream(PrintStream printStream)
printStream
- A print stream.protected void setAgentName(String name)
name
- The name of the agent.protected void submitError(Throwable t)
Throwable
into message queue. When the
Throwable
is taken from queue, it is thrown and causes
therefore the agent to terminate its execution.
t
- The Throwable
.messageHandling()
,
run()
protected abstract void init() throws Throwable
Throwable
- If an error occurred during initialization.protected abstract void handleMessage(Object o) throws Throwable
o
- A message.
Throwable
- If an error occurred during message handling.protected abstract void exit()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |