public class Agent extends java.lang.Object implements java.lang.Runnable, TimestampProvider, Messenger
init()
and shutdown()
methods to provide the appropriate behavior.
A simple example agent is shown below:
import org.arl.fjage.*; public class MyAgent extends Agent { @Override public void init() { log.info("MyAgent is starting"); add(new MessageBehavior() { @Override public void onReceive(Message msg) { log.info("Got a message!"); // do things with the message } }); // add other behaviors } }
Modifier and Type | Field and Description |
---|---|
protected static java.util.logging.Level |
ALL |
static long |
BLOCKING
Represents a blocking timeout of infinite time.
|
protected static java.util.logging.Level |
FINE |
protected static java.util.logging.Level |
FINER |
protected static java.util.logging.Level |
FINEST |
protected boolean |
ignoreExceptions |
protected static java.util.logging.Level |
INFO |
protected java.util.logging.Logger |
log
Logger for the agent to log messages to.
|
static long |
NON_BLOCKING
Represents a non-blocking timeout of 0 seconds.
|
protected static java.util.logging.Level |
OFF |
protected static java.util.logging.Level |
SEVERE |
protected java.lang.Thread |
thread |
protected long |
tid |
protected static java.util.logging.Level |
WARNING |
Constructor and Description |
---|
Agent() |
Modifier and Type | Method and Description |
---|---|
Behavior |
add(Behavior b)
Adds a behavior to the agent.
|
AgentID |
agent(java.lang.String name)
Convenience method to create agent id for the named agent.
|
AgentID |
agentForService(java.lang.Enum<?> service)
Finds an agent that provides a named service.
|
AgentID |
agentForService(java.lang.String service)
Finds an agent that provides a named service.
|
AgentID[] |
agentsForService(java.lang.Enum<?> service)
Finds all agents that provides a named service.
|
AgentID[] |
agentsForService(java.lang.String service)
Finds all agents that provides a named service.
|
protected void |
block()
Blocks the agent until its woken up by a message or a call to the
wake() method. |
protected void |
block(long millis)
Blocks the agent until its woken up by a message or a call to the
wake() method or until timeout, whichever occurs earlier. |
<T extends java.io.Serializable> |
clone(T obj)
Deep clones an object.
|
long |
currentTimeMillis()
Gets the current platform time in milliseconds.
|
protected void |
delay(long millis)
Blocks the agent for a specified period.
|
boolean |
deregister(java.lang.Enum<?> service)
De-registers the agent from the directory service as a provider of a named
service.
|
boolean |
deregister(java.lang.String service)
De-registers the agent from the directory service as a provider of a named
service.
|
protected void |
die(java.lang.Throwable ex)
Called by the container if the agent terminates abnormally.
|
AgentID |
getAgentID()
Gets the agent id.
|
Container |
getContainer()
Gets the container hosting the agent.
|
java.lang.String |
getName()
Gets the name of the agent.
|
Platform |
getPlatform()
Gets the platform hosting the agent.
|
AgentState |
getState()
Gets the state of the agent.
|
Store |
getStore()
Gets a persistent store for the agent.
|
java.lang.String |
getType()
Gets class name of agent.
|
protected boolean |
getYieldDuringReceive()
Checks if messages will be processed during a blocking receive().
|
protected void |
init()
Called by the container when the agent is started.
|
long |
nanoTime()
Gets the current platform time in nanoseconds.
|
void |
platformSend(Message m)
Sends a message to the recipient indicated in the message on all containers
running on the current platform.
|
RequestSender |
prepareRequest(Message request)
Prepares a request to be sent.
|
void |
println(java.lang.Object msg)
Logs a message at an INFO level.
|
Message |
receive()
Returns a message received by the gateway.
|
Message |
receive(java.lang.Class<?> cls)
Returns a message of a given class received by the gateway.
|
Message |
receive(java.lang.Class<?> cls,
long timeout)
Returns a message of a given class received by the gateway.
|
Message |
receive(long timeout)
Returns a message received by the agent.
|
Message |
receive(Message m)
Returns a response message received by the gateway.
|
Message |
receive(MessageFilter filter)
Returns a message received by the gateway and matching the given filter.
|
Message |
receive(MessageFilter filter,
long timeout)
Returns a message received by the gateway and matching the given filter.
|
Message |
receive(Message m,
long timeout)
Returns a response message received by the gateway.
|
boolean |
register(java.lang.Enum<?> service)
Registers the agent with the directory service as a provider of a named
service.
|
boolean |
register(java.lang.String service)
Registers the agent with the directory service as a provider of a named
service.
|
Message |
request(Message msg)
Sends a request and waits for a response.
|
Message |
request(Message msg,
long timeout)
Sends a request and waits for a response.
|
void |
run()
Lifecycle of the agent.
|
boolean |
send(Message m)
Sends a message to the recipient indicated in the message.
|
void |
setLogLevel(java.util.logging.Level level)
Changes the logging level for the agent.
|
void |
setQueueSize(int size)
Sets the maximum length of the incoming message queue for the agent.
|
protected void |
setYieldDuringReceive(boolean b)
Enables/disables processing of messages during a blocking receive().
|
protected void |
shutdown()
Called by the container when the agent is terminated.
|
void |
stop()
Requests an agent to terminate.
|
boolean |
subscribe(AgentID topic)
Subscribes the agent to receive all messages sent to the given topic.
|
AgentID |
topic()
Returns an object representing the notification topic for this agent.
|
AgentID |
topic(AgentID agent)
Returns an object representing the notification topic for an agent.
|
AgentID |
topic(AgentID agent,
java.lang.Enum<?> topic)
Returns an object representing a named notification topic for an agent.
|
AgentID |
topic(AgentID agent,
java.lang.String topic)
Returns an object representing a named notification topic for an agent.
|
AgentID |
topic(java.lang.Enum<?> topic)
Returns an object representing the named topic.
|
AgentID |
topic(java.lang.String topic)
Returns an object representing the named topic.
|
java.lang.String |
toString()
Gets a string representation of the agent.
|
boolean |
unsubscribe(AgentID topic)
Unsubscribes the agent from a given topic.
|
void |
wake()
Wakes up the agent if it was blocked using
block() . |
public static final long NON_BLOCKING
public static final long BLOCKING
protected static final java.util.logging.Level ALL
protected static final java.util.logging.Level FINEST
protected static final java.util.logging.Level FINER
protected static final java.util.logging.Level FINE
protected static final java.util.logging.Level INFO
protected static final java.util.logging.Level WARNING
protected static final java.util.logging.Level SEVERE
protected static final java.util.logging.Level OFF
protected long tid
protected java.lang.Thread thread
protected boolean ignoreExceptions
protected java.util.logging.Logger log
protected void init()
protected void shutdown()
protected void die(java.lang.Throwable ex)
ex
- exception that caused the agent to die.public AgentID getAgentID()
public java.lang.String getName()
public java.lang.String getType()
public void setLogLevel(java.util.logging.Level level)
level
- log levelLevel
protected void block()
wake()
method.protected void block(long millis)
wake()
method or until timeout, whichever occurs earlier.millis
- timeout in millisecondsprotected void delay(long millis)
Thread.sleep(long)
as this method provides
measures correct delay time for the hosting platform, be it a real-time
or a simulated discrete-time platform.millis
- period to delay in milliseconds.public void wake()
block()
.public void stop()
public Behavior add(Behavior b)
Behavior.onStart()
method of
the behavior is immediately called once its added to the agent.b
- behavior to be added.public AgentState getState()
public Platform getPlatform()
public Container getContainer()
public long currentTimeMillis()
System.currentTimeMillis()
as this method returns the real time or the simulated discrete time depending on
the platform that the agent is running on.currentTimeMillis
in interface TimestampProvider
public long nanoTime()
System.nanoTime()
as this method returns the real time or the simulated discrete time depending on
the platform that the agent is running on.nanoTime
in interface TimestampProvider
public AgentID agent(java.lang.String name)
public AgentID topic(java.lang.String topic)
topic
- name of the topic.public AgentID topic(java.lang.Enum<?> topic)
topic
- name of the topic.public AgentID topic(AgentID agent)
agent
- agent to get notification topic for.public AgentID topic(AgentID agent, java.lang.String topic)
agent
- agent to get notification topic for.topic
- name for the notification topic.public AgentID topic(AgentID agent, java.lang.Enum<?> topic)
agent
- agent to get notification topic for.topic
- name for the notification topic.public AgentID topic()
public boolean send(Message m)
Messenger
public RequestSender prepareRequest(Message request)
request
- Request message.public void platformSend(Message m)
m
- message to be sent.protected void setYieldDuringReceive(boolean b)
b
- true to process messages while waiting, false to disable processing.protected boolean getYieldDuringReceive()
setYieldDuringReceive(boolean)
public Message receive(MessageFilter filter, long timeout)
Messenger
public Message receive(MessageFilter filter)
Messenger
public Message receive()
Messenger
public Message receive(long timeout)
Messenger
public Message receive(java.lang.Class<?> cls)
Messenger
public Message receive(java.lang.Class<?> cls, long timeout)
Messenger
public Message receive(Message m)
Messenger
public Message receive(Message m, long timeout)
Messenger
public Message request(Message msg, long timeout)
Messenger
public Message request(Message msg)
public void setQueueSize(int size)
size
- maximum number of messages in the message queue.public boolean subscribe(AgentID topic)
topic
- the topic to subscribe to.public boolean unsubscribe(AgentID topic)
topic
- the topic to unsubscribe.public boolean register(java.lang.String service)
service
- the named service that the agent provides.public boolean register(java.lang.Enum<?> service)
service
- the named service that the agent provides.public boolean deregister(java.lang.String service)
service
- the named service that the agent no longer provides.public boolean deregister(java.lang.Enum<?> service)
service
- the named service that the agent no longer provides.public AgentID agentForService(java.lang.String service)
service
- the named service of interest.public AgentID agentForService(java.lang.Enum<?> service)
service
- the named service of interest.public AgentID[] agentsForService(java.lang.String service)
service
- the named service of interest.public AgentID[] agentsForService(java.lang.Enum<?> service)
service
- the named service of interest.public void println(java.lang.Object msg)
msg
- message to log.public Store getStore()
public <T extends java.io.Serializable> T clone(T obj)
obj
- object to clone.public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
public final void run()
run
in interface java.lang.Runnable
Runnable.run()