public class Agent extends AbstractAgent
AbstractAgent.ReturnCode, AbstractAgent.State
logger
Modifier and Type | Method and Description |
---|---|
List<Message> |
broadcastMessageWithRoleAndWaitForReplies(String community,
String group,
String role,
Message message,
String senderRole,
Integer timeOutMilliSeconds)
Broadcasts a message and wait for answers considering a timeout duration.
|
int |
getThreadPriority()
Returns this thread's priority.
|
boolean |
isDaemon()
Tells if the agent is a daemon.
|
AbstractAgent.ReturnCode |
killAgent(AbstractAgent target,
int timeOutSeconds)
Kills the targeted agent.
|
protected void |
live()
This method corresponds to the second behavior which is called by the MaDKit kernel
when a threaded agent is launched (i.e. an agent which subclasses this class).
|
protected void |
pause(int milliSeconds)
Stops the agent's process for a while.
|
Message |
sendMessageAndWaitForReply(AgentAddress receiver,
Message messageToSend)
Sends a message and waits indefinitely for an answer to it.
|
Message |
sendMessageAndWaitForReply(AgentAddress receiver,
Message messageToSend,
int timeOutMilliSeconds)
Sends a message and waits for an answer to it.
|
Message |
sendMessageAndWaitForReply(String community,
String group,
String role,
Message messageToSend)
Sends a message to an agent having this position in the organization
and waits indefinitely for an answer to it.
|
Message |
sendMessageAndWaitForReply(String community,
String group,
String role,
Message messageToSend,
int timeOutMilliSeconds)
Sends a message to an agent having this position in the organization
and waits for an answer to it.
|
Message |
sendMessageWithRoleAndWaitForReply(AgentAddress receiver,
Message messageToSend,
String senderRole)
Sends a message and waits for an answer to it.
|
Message |
sendMessageWithRoleAndWaitForReply(AgentAddress receiver,
Message messageToSend,
String senderRole,
Integer timeOutMilliSeconds)
Sends a message and waits for an answer to it.
|
Message |
sendMessageWithRoleAndWaitForReply(String community,
String group,
String role,
Message messageToSend,
String senderRole)
Sends a message to an agent having this position in the organization
and waits indefinitely for an answer to it.
|
Message |
sendMessageWithRoleAndWaitForReply(String community,
String group,
String role,
Message messageToSend,
String senderRole,
Integer timeOutMilliSeconds)
Sends a message to an agent having this position in the organization
and waits for an answer to it.
|
Message |
sendReplyAndWaitForReply(Message messageToReplyTo,
Message reply)
Sends a reply message and waits indefinitely for an answer to it.
|
Message |
sendReplyAndWaitForReply(Message messageToReplyTo,
Message reply,
int timeOutMilliSeconds)
Sends a reply message and waits for an answer to it.
|
Message |
sendReplyWithRoleAndWaitForReply(Message messageToReplyTo,
Message reply,
String senderRole)
Sends a reply message and waits indefinitely for an answer to it.
|
Message |
sendReplyWithRoleAndWaitForReply(Message messageToReplyTo,
Message reply,
String senderRole,
Integer timeOutMilliSeconds)
Sends a reply message and waits for an answer to it.
|
void |
setThreadPriority(int newPriority)
Changes the priority of the agent's thread.
|
Message |
waitAnswer(Message query)
Retrieves and removes the next message that is a reply
to the query message, waiting for ever if necessary
until a matching reply becomes available.
|
Message |
waitAnswer(Message query,
Integer timeOutMilliSeconds)
Retrieves and removes the next message that is a reply
to the query message, waiting for ever if necessary
until a matching reply becomes available.
|
Message |
waitNextMessage()
This method is the blocking version of nextMessage().
|
Message |
waitNextMessage(Integer timeOutMilliseconds,
MessageFilter filter)
This method gets the next message of the mailbox or waits
for a new incoming acceptable message up to a certain delay.
|
Message |
waitNextMessage(long timeOutMilliseconds)
This method gets the next message of the mailbox or waits
for a new incoming message considering a certain delay.
|
Message |
waitNextMessage(MessageFilter filter)
Retrieves and removes the next message that complies
with the filter, waiting for ever if necessary
until a matching message becomes available.
|
activate, broadcastMessage, broadcastMessageWithRole, bucketModeCreateGroup, bucketModeRequestRole, checkAgentAddress, compareTo, createGroup, createGroup, createGroup, createGroupIfAbsent, createGroupIfAbsent, createGroupIfAbsent, createGUIOnStartUp, destroyCommunity, destroyGroup, destroyRole, end, executeThisAgent, executeThisAgent, executeThisAgent, getAgentAddressIn, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getDistantAgentWithRole, getExistingCommunities, getExistingGroups, getExistingRoles, getKernelAddress, getLastReceivedMessage, getLastReceivedMessage, getLogger, getMadkitConfig, getMadkitProperty, getMadkitProperty, getMyGroups, getMyRoles, getName, getNetworkID, getOrganizationSnapShot, getReplyTo, getServerInfo, getSimpleNetworkID, getState, hasGUI, hashCode, hasRole, isAlive, isCommunity, isGroup, isKernelOnline, isMadkitPropertyTrue, isMessageBoxEmpty, isRole, killAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchNode, launchXmlAgents, leaveGroup, leaveRole, nextMessage, nextMessage, nextMessages, proceedEnumMessage, purgeMailbox, receiveMessage, reload, requestRole, requestRole, sendMessage, sendMessage, sendMessageWithRole, sendMessageWithRole, sendReply, sendReplyWithRole, setLogLevel, setMadkitProperty, setMadkitProperty, setName, setupFrame, toString
public void setThreadPriority(int newPriority)
AbstractAgent.activate()
to have a concrete effect.
Default priority is set to Thread.NORM_PRIORITY
- 1
to ensure swing responsiveness.newPriority
- priority to set this thread toIllegalArgumentException
- If the priority is not in the
range Thread.MIN_PRIORITY
to
Thread.MAX_PRIORITY
.SecurityException
- if the current thread cannot modify this thread.Thread
public int getThreadPriority()
Thread
public boolean isDaemon()
true
if the agent is a Daemonprotected void live()
Here is a typical example:
@Override protected void live() { while(true){ Message m = waitNextMessage(); handleMessage(m); //a private method that does the appropriate job } }
public AbstractAgent.ReturnCode killAgent(AbstractAgent target, int timeOutSeconds)
AbstractAgent
AbstractAgent.end()
method until the time out
elapsed.
If the target is in the activate or live method (Agent subclasses), it will be brutally stop and then proceed its end method.
The method returns only when the targeted agent actually ends its life. So if the target contains a infinite loop, the caller can be blocked. Using a timeout thus ensures that the caller will be blocked only a certain amount of time. Using 0 as timeout will stop the target as soon as possible, eventually brutally stop the its life cycle. In such a case, if its end method has not been started, it will never run.
killAgent
in class AbstractAgent
AbstractAgent.ReturnCode.SUCCESS
: If the target's end
method has completed normally.
AbstractAgent.ReturnCode.ALREADY_KILLED
: If the target has been
already killed.AbstractAgent.ReturnCode.NOT_YET_LAUNCHED
: If the target has not been launched.
AbstractAgent.ReturnCode.TIMEOUT
: If the target's end method took
too much time and has been brutally stopped.public Message sendMessageAndWaitForReply(AgentAddress receiver, Message messageToSend)
sendMessageWithRoleAndWaitForReply(receiver, messageToSend, null, null)
receiver
- the targeted agent by the send.messageToSend
- the message to send.null
if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(AgentAddress, Message, String, Integer)
public Message sendMessageAndWaitForReply(AgentAddress receiver, Message messageToSend, int timeOutMilliSeconds)
sendMessageWithRoleAndWaitForReply(receiver, messageToSend, null, timeOutMilliSeconds)
receiver
- the targeted agent by the send.messageToSend
- the message to send.timeOutMilliSeconds
- the maximum time to wait.
If null
the agent will wait indefinitely.null
if the time out has elapsed
or if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(AgentAddress, Message, String, Integer)
public Message sendMessageWithRoleAndWaitForReply(AgentAddress receiver, Message messageToSend, String senderRole)
sendMessageWithRoleAndWaitForReply(receiver, messageToSend, senderRole, null)
receiver
- the targeted agent by the send.messageToSend
- the message to send.senderRole
- the role with which the sending is done.null
if the time out has elapsed
or if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(AgentAddress, Message, String, Integer)
public Message sendMessageWithRoleAndWaitForReply(AgentAddress receiver, Message messageToSend, String senderRole, Integer timeOutMilliSeconds)
receiver
- the targeted agent by the send.messageToSend
- the message to send.senderRole
- the role with which the sending is done.timeOutMilliSeconds
- the maximum time to wait.
If null
the agent will wait indefinitely.null
if the time out has elapsed
or if there was an error when sending the message, that is any AbstractAgent.ReturnCode
different from AbstractAgent.ReturnCode.SUCCESS
(see AbstractAgent.sendMessageWithRole(AgentAddress, Message, String)
).AbstractAgent.sendMessageWithRole(AgentAddress, Message, String)
,
AbstractAgent.ReturnCode
public Message sendMessageAndWaitForReply(String community, String group, String role, Message messageToSend)
sendMessageWithRoleAndWaitForReply(community, group, role, messageToSend, null, null)
community
- the community namegroup
- the group namerole
- the role namemessageToSend
- the message to send.
If null
the agent will wait indefinitely.null
if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(String, String, String, Message, String, Integer)
public Message sendMessageWithRoleAndWaitForReply(String community, String group, String role, Message messageToSend, String senderRole)
sendMessageWithRoleAndWaitForReply(community, group, role, messageToSend, senderRole, null)
community
- the community namegroup
- the group namerole
- the role namemessageToSend
- the message to send.senderRole
- the role with which the sending is done.
If null
the agent will wait indefinitely.null
if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(String, String, String, Message, String, Integer)
public Message sendMessageAndWaitForReply(String community, String group, String role, Message messageToSend, int timeOutMilliSeconds)
sendMessageWithRoleAndWaitForReply(community, group, role, messageToSend, null, timeOutMilliSeconds)
community
- the community namegroup
- the group namerole
- the role namemessageToSend
- the message to send.timeOutMilliSeconds
- the maximum time to wait.
If null
the agent will wait indefinitely.null
if the time out has elapsed
or if there was an error when sending the message.sendMessageWithRoleAndWaitForReply(String, String, String, Message, String, Integer)
public Message sendMessageWithRoleAndWaitForReply(String community, String group, String role, Message messageToSend, String senderRole, Integer timeOutMilliSeconds)
community
- the community namegroup
- the group namerole
- the role namemessageToSend
- the message to send.senderRole
- the role with which the sending is done.timeOutMilliSeconds
- the maximum time to wait.
If null
the agent will wait indefinitely.null
if the time out has elapsed
or if there was an error when sending the message.public Message sendReplyAndWaitForReply(Message messageToReplyTo, Message reply)
sendReplyWithRoleAndWaitForReply(messageToReplyTo, reply, null, null)
.messageToReplyTo
- the original message previously received.reply
- the new message.sendReplyWithRoleAndWaitForReply(Message, Message, String, Integer)
public Message sendReplyAndWaitForReply(Message messageToReplyTo, Message reply, int timeOutMilliSeconds)
sendReplyWithRoleAndWaitForReply(messageToReplyTo, reply, null, timeOutMilliSeconds)
.messageToReplyTo
- the original message previously receivedreply
- the new messagesendReplyWithRoleAndWaitForReply(Message, Message, String, Integer)
public Message sendReplyWithRoleAndWaitForReply(Message messageToReplyTo, Message reply, String senderRole)
sendReplyWithRoleAndWaitForReply(messageToReplyTo, reply, senderRole, null)
.messageToReplyTo
- the original message previously receivedreply
- the new messagesendReplyWithRoleAndWaitForReply(Message, Message, String, Integer)
public Message sendReplyWithRoleAndWaitForReply(Message messageToReplyTo, Message reply, String senderRole, Integer timeOutMilliSeconds)
messageToReplyTo
- the original message previously receivedreply
- the reply message.senderRole
- the role with which the reply is sent.timeOutMilliSeconds
- the maximum time to wait.
If null
the agent will wait indefinitely.null
if the time out has elapsed
or if there was an error when sending the reply, that is any AbstractAgent.ReturnCode
different from AbstractAgent.ReturnCode.SUCCESS
(see AbstractAgent.sendReplyWithRole(Message, Message, String)
).AbstractAgent.sendReplyWithRole(Message, Message, String)
,
AbstractAgent.ReturnCode
public List<Message> broadcastMessageWithRoleAndWaitForReplies(String community, String group, String role, Message message, String senderRole, Integer timeOutMilliSeconds)
community
- group
- role
- message
- senderRole
- timeOutMilliSeconds
- message
which has been broadcasted.public Message waitNextMessage()
waitNextMessage(long)
public Message waitNextMessage(long timeOutMilliseconds)
timeOutMilliseconds
- the maximum time to wait, in milliseconds.null
if no message
has been received before the time out delay is elapsedpublic Message waitNextMessage(MessageFilter filter)
filter
- public Message waitNextMessage(Integer timeOutMilliseconds, MessageFilter filter)
timeOutMilliseconds
- the maximum time to wait, in milliseconds.filter
- null
otherwise.protected void pause(int milliSeconds)
milliSeconds
- the number of milliseconds for which the agent should pause.public Message waitAnswer(Message query)
query
- the message for which a reply is waited forpublic Message waitAnswer(Message query, Integer timeOutMilliSeconds)
query
- the message for which a reply is waited fortimeOutMilliSeconds
- the maximum time to wait, in milliseconds.
Fabien Michel, Olivier Gutknecht, Jacques Ferber - September 27 2016