Package jade.proto.states
Class MsgReceiver
- java.lang.Object
-
- jade.core.behaviours.Behaviour
-
- jade.core.behaviours.SimpleBehaviour
-
- jade.proto.states.MsgReceiver
-
- All Implemented Interfaces:
Serializable,Serializable
public class MsgReceiver extends SimpleBehaviour
This behaviour is a simple implementation of a message receiver. It puts into the given key of the given datastore the received message according to the given message template and timeout. All these data must be passed in the constructor. If the timeout expires before any message arrives, the behaviour terminates and put null into the datastore.- Version:
- $Date$ $Revision$
- Author:
- Tiziana Trucco - TILab
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jade.core.behaviours.Behaviour
Behaviour.RunnableChangedEvent
-
-
Field Summary
Fields Modifier and Type Field Description protected longdeadlinestatic intINFINITEA numeric constant to mean that the deadline for the receive operation will never expire.static intINTERRUPTEDA numeric constant to mean that the receive operation was interrupted.protected ObjectreceivedMsgKeyprotected MessageTemplatetemplatestatic intTIMEOUT_EXPIREDA numeric constant to mean that a timeout expired.-
Fields inherited from class jade.core.behaviours.Behaviour
myAgent, myEvent, NOTIFY_DOWN, NOTIFY_UP, parent, STATE_BLOCKED, STATE_READY, STATE_RUNNING
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedMsgReceiver()MsgReceiver(Agent a, MessageTemplate mt, long deadline, DataStore s, Object msgKey)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaction()Runs the behaviour.booleandone()Check if this behaviour is done.protected voidhandleMessage(ACLMessage msg)This is invoked when a message matching the specified template is received or the timeout has expired (themsgparameter is null in this case).voidinterrupt()Signal an interruption to this receiver, and cause the ongoing receive operation to abort.intonEnd()This method is just an empty placeholder for subclasses.voidreset(MessageTemplate mt, long deadline, DataStore s, Object msgKey)Reset this behaviour, possibly replacing the receive templatt and other data.voidsetDeadline(long deadline)This method allows modifying the deadlinevoidsetReceivedKey(Object key)This method allows modifying the key in the DS where to put the received messagevoidsetTemplate(MessageTemplate mt)This method allows modifying the template-
Methods inherited from class jade.core.behaviours.SimpleBehaviour
reset
-
Methods inherited from class jade.core.behaviours.Behaviour
actionWrapper, block, block, getAgent, getBehaviourName, getDataStore, getExecutionState, getParent, getRestartCounter, handle, handleBlockEvent, handleRestartEvent, isRunnable, onStart, restart, root, setAgent, setBehaviourName, setDataStore, setExecutionState
-
-
-
-
Field Detail
-
TIMEOUT_EXPIRED
public static final int TIMEOUT_EXPIRED
A numeric constant to mean that a timeout expired.- See Also:
- Constant Field Values
-
INTERRUPTED
public static final int INTERRUPTED
A numeric constant to mean that the receive operation was interrupted.- See Also:
- Constant Field Values
-
INFINITE
public static final int INFINITE
A numeric constant to mean that the deadline for the receive operation will never expire.- See Also:
- Constant Field Values
-
template
protected MessageTemplate template
-
deadline
protected long deadline
-
receivedMsgKey
protected Object receivedMsgKey
-
-
Constructor Detail
-
MsgReceiver
public MsgReceiver(Agent a, MessageTemplate mt, long deadline, DataStore s, Object msgKey)
Constructor.- Parameters:
a- a reference to the Agentmt- the MessageTemplate of the message to be received, if null the first received message is returned by this behaviourdeadline- a timeout for waiting until a message arrives. It must be expressed as an absolute time, as it would be returned bySystem.currentTimeMillisec()s- the dataStore for this bheaviourmsgKey- the key where the beahviour must put the received message into the DataStore.
-
MsgReceiver
protected MsgReceiver()
-
-
Method Detail
-
action
public void action()
Description copied from class:BehaviourRuns the behaviour. This abstract method must be implemented byBehavioursubclasses to perform ordinary behaviour duty. An agent schedules its behaviours calling theiraction()method; since all the behaviours belonging to the same agent are scheduled cooperatively, this method must not enter in an endless loop and should return as soon as possible to preserve agent responsiveness. To split a long and slow task into smaller section, recursive behaviour aggregation may be used.- Specified by:
actionin classBehaviour- See Also:
CompositeBehaviour
-
done
public boolean done()
Description copied from class:BehaviourCheck if this behaviour is done. The agent scheduler calls this method to see whether aBehaviourstill need to be run or it has completed its task. Concrete behaviours must implement this method to return their completion state. Finished behaviours are removed from the scheduling queue, while others are kept within to be run again when their turn comes again.
-
onEnd
public int onEnd()
Description copied from class:BehaviourThis method is just an empty placeholder for subclasses. It is invoked just once after this behaviour has ended. Therefore, it acts as an epilog for the task represented by thisBehaviour.
Note thatonEndis called after the behaviour has been removed from the pool of behaviours to be executed by an agent. Therefore callingreset()is not sufficient to cyclically repeat the task represented by thisBehaviour. In order to achieve that, thisBehaviourmust be added again to the agent (usingmyAgent.addBehaviour(this)). The same applies to in the case of aBehaviourthat is a child of aParallelBehaviour.
-
handleMessage
protected void handleMessage(ACLMessage msg)
This is invoked when a message matching the specified template is received or the timeout has expired (themsgparameter is null in this case). Users may redefine this method to react to this event. The default implementation of does nothing.
-
reset
public void reset(MessageTemplate mt, long deadline, DataStore s, Object msgKey)
Reset this behaviour, possibly replacing the receive templatt and other data.- Parameters:
mt- The template to match ACL messages against during the receive operation.deadline- The relative timeout of the receive operation. If theINFINITEconstant is used, then no deadline is set and the operation will wait until a matching ACL message arrives.s- The datastore where the received ACL message is to be put.msgKey- The key to use to put the received message into the selected datastore.
-
setDeadline
public void setDeadline(long deadline)
This method allows modifying the deadline
-
setTemplate
public void setTemplate(MessageTemplate mt)
This method allows modifying the template
-
setReceivedKey
public void setReceivedKey(Object key)
This method allows modifying the key in the DS where to put the received message
-
interrupt
public void interrupt()
Signal an interruption to this receiver, and cause the ongoing receive operation to abort.
-
-