Package jade.core.behaviours
Class ReceiverBehaviour
- java.lang.Object
-
- jade.core.behaviours.Behaviour
-
- jade.core.behaviours.ReceiverBehaviour
-
- All Implemented Interfaces:
Serializable,Serializable
public final class ReceiverBehaviour extends Behaviour
Behaviour for receiving an ACL message. This class encapsulates areceive()as an atomic operation. This behaviour terminates when an ACL message is received. The methodgetMessage()allows to get the received message.- Version:
- $Date$ $Revision$
- Author:
- Giovanni Rimassa - Universita' di Parma
- See Also:
SenderBehaviour,Agent.receive(),ACLMessage, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReceiverBehaviour.HandleAn interface representing ACL messages due to arrive within a time limit.static classReceiverBehaviour.NotYetReadyException class for timeouts.static classReceiverBehaviour.TimedOutException class for timeouts.-
Nested classes/interfaces inherited from class jade.core.behaviours.Behaviour
Behaviour.RunnableChangedEvent
-
-
Field Summary
-
Fields inherited from class jade.core.behaviours.Behaviour
myAgent, myEvent, NOTIFY_DOWN, NOTIFY_UP, parent, STATE_BLOCKED, STATE_READY, STATE_RUNNING
-
-
Constructor Summary
Constructors Constructor Description ReceiverBehaviour(Agent a, long millis, MessageTemplate mt)This constructor creates aReceiverBehaviourobject that ends as soon as an ACL message matching a givenMessageTemplatearrives or the passedmillistimeout expires.ReceiverBehaviour(Agent a, ReceiverBehaviour.Handle h, long millis)Receive any ACL message, waiting at mostmillismilliseconds (infinite time ifmillis < 1).ReceiverBehaviour(Agent a, ReceiverBehaviour.Handle h, long millis, MessageTemplate mt)Receive any ACL message matching the given template, witing at mostmillismilliseconds (infinite time ifmillis < 1.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaction()Actual behaviour implementation.booleandone()Checks whether this behaviour ended.ACLMessagegetMessage()This method allows the caller to get the received message.static ReceiverBehaviour.HandlenewHandle()Factory method for message handles.voidreset()Resets this behaviour.-
Methods inherited from class jade.core.behaviours.Behaviour
actionWrapper, block, block, getAgent, getBehaviourName, getDataStore, getExecutionState, getParent, getRestartCounter, handle, handleBlockEvent, handleRestartEvent, isRunnable, onEnd, onStart, restart, root, setAgent, setBehaviourName, setDataStore, setExecutionState
-
-
-
-
Constructor Detail
-
ReceiverBehaviour
public ReceiverBehaviour(Agent a, long millis, MessageTemplate mt)
This constructor creates aReceiverBehaviourobject that ends as soon as an ACL message matching a givenMessageTemplatearrives or the passedmillistimeout expires. The received message can then be got via the methodgetMessage.- Parameters:
a- The agent this behaviour belongs to, and that willreceive()the message.millis- The timeout expressed in milliseconds, an infinite timeout can be expressed by a value < 0.mt- A Message template to match incoming messages against, null to indicate no template and receive any message that arrives.
-
ReceiverBehaviour
public ReceiverBehaviour(Agent a, ReceiverBehaviour.Handle h, long millis)
Receive any ACL message, waiting at mostmillismilliseconds (infinite time ifmillis < 1). When calling this constructor, a suitableHandlemust be created and passed to it. When this behaviour ends, some other behaviour will try to get the ACL message out of the handle, and an exception will be thrown in case of a time out. The following example code explains this:// ReceiverBehaviour creation, e.g. in agent setup() method h = ReceiverBehaviour.newHandle(); // h is an agent instance variable addBehaviour(new ReceiverBehaviour(this, h, 10000); // Wait 10 seconds ... // Some other behaviour, later, tries to read the ACL message // in its action() method try { ACLMessage msg = h.getMessage(); // OK. Message received within timeout. } catch(ReceiverBehaviour.TimedOut rbte) { // Receive timed out } catch(ReceiverBehaviour.NotYetReady rbnyr) { // Message not yet ready, but timeout still active }- Parameters:
a- The agent this behaviour belongs to.h- An Handle representing the message to receive.millis- The maximum amount of time to wait for the message, in milliseconds.- See Also:
ReceiverBehaviour.Handle,newHandle()
-
ReceiverBehaviour
public ReceiverBehaviour(Agent a, ReceiverBehaviour.Handle h, long millis, MessageTemplate mt)
Receive any ACL message matching the given template, witing at mostmillismilliseconds (infinite time ifmillis < 1. When calling this constructor, a suitableHandlemust be created and passed to it.- Parameters:
a- The agent this behaviour belongs to.h- An Handle representing the message to receive.millis- The maximum amount of time to wait for the message, in milliseconds.mt- A Message template to match incoming messages against, null to indicate no template and receive any message that arrives.- See Also:
ReceiverBehaviour(Agent a, Handle h, long millis)
-
-
Method Detail
-
newHandle
public static ReceiverBehaviour.Handle newHandle()
Factory method for message handles. This method returns a newHandleobject, which can be used to retrieve an ACL message out of aReceiverBehaviourobject.- Returns:
- A new
Handleobject. - See Also:
ReceiverBehaviour.Handle
-
action
public void action()
Actual behaviour implementation. This method receives a suitable ACL message and copies it into the message provided by the behaviour creator. It blocks the current behaviour if no suitable message is available.- Specified by:
actionin classBehaviour- See Also:
CompositeBehaviour
-
done
public boolean done()
Checks whether this behaviour ended.
-
reset
public void reset()
Resets this behaviour. This method allows to receive anotherACLMessagewith the sameReceiverBehaviourwithout creating a new object.
-
getMessage
public ACLMessage getMessage() throws ReceiverBehaviour.TimedOut, ReceiverBehaviour.NotYetReady
This method allows the caller to get the received message.- Returns:
- the received message
- Throws:
ReceiverBehaviour.TimedOut- if the timeout passed in the constructor of this class expired before any message (that eventually matched the passed message template) arrivedReceiverBehaviour.NotYetReady- if the message is not yet arrived and the timeout is not yet expired.
-
-