public abstract class Activity
extends java.lang.Object
An Activity
is some task that a peer is currently working one. An
activity is always currently in some state.
It is possible to directly extend this class and have complete control over the implementation of the activity simply by handling incoming messages and taking some action at that point in time. This is recommended either for very simple P2P scenarios where not much state is maintained, or for very complicated ones that do not fit into the provided framework.
For most cases however, it is probably best to implement the activity as an
FSM (a Finite State Machine) that does from state to state based on incoming
messages and/or related sub-activities. In this case, the FSMActivity
class
must be extended instead.
Constructor and Description |
---|
Activity(HyperGraphPeer thisPeer)
Constructor for brand new activities - a random UUID is generated to
identify the newly constructed activity.
|
Activity(HyperGraphPeer thisPeer,
java.util.UUID id)
Construct an new
Activity from a given UUID. |
Modifier and Type | Method and Description |
---|---|
protected void |
addAction(java.lang.Runnable action)
Add an action to the action queue to be scheduled for execution some time
in the future.
|
protected Json |
createMessage(Performative performative,
java.lang.Object content) |
boolean |
equals(java.lang.Object x) |
java.util.concurrent.Future<ActivityResult> |
getFuture()
Return the
Future object representing the completion
of this activity. |
java.util.UUID |
getId()
Return the UUID of this activity.
|
protected PeerInterface |
getPeerInterface()
Return the
PeerInterface instance for communicating
with other peers. |
WorkflowState |
getState()
Return this activity's workflow state.
|
HyperGraphPeer |
getThisPeer()
Return the
HyperGraphPeer where this activity executes. |
java.lang.String |
getType()
Return the type name of this activity.
|
abstract void |
handleMessage(Json message)
Handle an incoming that was identified as belonging to this activity.
|
int |
hashCode() |
abstract void |
initiate()
Called by the framework to initiate a new activity.
|
protected java.util.concurrent.Future<java.lang.Boolean> |
post(HGPeerIdentity target,
Json msg) |
protected java.util.concurrent.Future<java.lang.Boolean> |
post(java.lang.Object target,
Json msg) |
protected java.util.concurrent.Future<java.lang.Boolean> |
reply(Json msg,
Performative perf,
java.lang.Object content) |
protected void |
send(HGPeerIdentity target,
Json msg)
A convenience method to send a message to a target peer.
|
protected void |
send(java.lang.Object target,
Json msg)
A convenience method to send a message to a target peer.
|
java.lang.String |
toString() |
public Activity(HyperGraphPeer thisPeer)
Constructor for brand new activities - a random UUID is generated to identify the newly constructed activity.
thisPeer
- public Activity(HyperGraphPeer thisPeer, java.util.UUID id)
Construct an new Activity
from a given UUID. Use this
constructor for new activities if you have your own means of UUID
generation or for an existing activity to be instantiated locally.
thisPeer
- id
- public HyperGraphPeer getThisPeer()
Return the HyperGraphPeer
where this activity executes.
protected PeerInterface getPeerInterface()
Return the PeerInterface
instance for communicating
with other peers. This is a convenience method that is equivalent to
getThisPeer().getPeerInterface()
.
protected Json createMessage(Performative performative, java.lang.Object content)
protected void send(HGPeerIdentity target, Json msg)
A convenience method to send a message to a target peer.
target
- The message recipient.msg
- The message.protected java.util.concurrent.Future<java.lang.Boolean> post(HGPeerIdentity target, Json msg)
protected void send(java.lang.Object target, Json msg)
A convenience method to send a message to a target peer.
target
- The message recipient - could be a HGPeerIdentity
or a transport-dependent network target.msg
- The message.protected java.util.concurrent.Future<java.lang.Boolean> post(java.lang.Object target, Json msg)
protected java.util.concurrent.Future<java.lang.Boolean> reply(Json msg, Performative perf, java.lang.Object content)
protected void addAction(java.lang.Runnable action) throws java.lang.InterruptedException
Add an action to the action queue to be scheduled for execution some time in the future.
action
- The action in the form of a Runnable
object.
If null
, it will be ignored.java.lang.InterruptedException
- If the current thread is interrupted during
the put into the action queue(which is a blocking queue).public abstract void initiate()
Called by the framework to initiate a new activity. This method is only invoked
at the peer initiating the activity. Once an activity has been initiated, its state
changes to Started
.
public abstract void handleMessage(Json message)
Handle an incoming that was identified as belonging to this activity.
message
- The full message.public final WorkflowState getState()
Return this activity's workflow state.
public final java.util.concurrent.Future<ActivityResult> getFuture()
Return the Future
object representing the completion
of this activity.
public java.lang.String getType()
Return the type name of this activity. By the default to fully-qualified class name is returned. This method can be overridden by sub-classes to provide a short and/or more human-readable type name.
public final java.util.UUID getId()
public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object x)
equals
in class java.lang.Object