Project JXTA

net.jxta.endpoint
Class MessengerState

java.lang.Object
  extended bynet.jxta.endpoint.MessengerState

public abstract class MessengerState
extends Object

This class implements the complete standard messenger life cycle state machine that all messengers must obey. Compliant messengers can be built by implementing and using this class as an engine to orchestrate descreet operations. In order to use this class, one must implement the various abstract Action methods, so that they trigger the required operations. Synchronization has to be externally provided and usualy needs to extend around sections wider than just the invocation of this classe's methods. For example, if the user of this class maintains a queue, the state of the queue must be kept consistent with the invocation of msgsEvent(), saturatedEvent(), and idleEvent(), which all denote different states of that queue. It is suggested to use the instance of this class as the synchronization object.


Constructor Summary
protected MessengerState(boolean connected)
          Constructs a new messenger state machine.
 
Method Summary
 void closeEvent()
           
protected abstract  void closeInputAction()
          Reject new messages from now on.
protected abstract  void closeOutputAction()
          Force close output.
protected abstract  void connectAction()
          Try to make a connection.
 void downEvent()
           
protected abstract  void failAllAction()
          Drain pending messages, all failed.
 int getState()
           
 void idleEvent()
           
 void msgsEvent()
           
 void resolveEvent()
          Event input.
 void saturatedEvent()
           
 void shutdownEvent()
           
protected abstract  void startAction()
          Start sending.
 void upEvent()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MessengerState

protected MessengerState(boolean connected)
Constructs a new messenger state machine. The transistion map is static and we refer to it only to grab the first state. After that, states refer to each other. The only reason they are members in the map is so that we can make references during init.

Parameters:
connected - If true, the initial state is Messenger.CONNECTED else, Messenger.UNRESOLVED.
Method Detail

getState

public int getState()
Returns:
The current state.

resolveEvent

public void resolveEvent()
Event input.


msgsEvent

public void msgsEvent()

saturatedEvent

public void saturatedEvent()

closeEvent

public void closeEvent()

shutdownEvent

public void shutdownEvent()

upEvent

public void upEvent()

downEvent

public void downEvent()

idleEvent

public void idleEvent()

connectAction

protected abstract void connectAction()
Try to make a connection. Called whenever transitioning from a state that neither needs nor has a connection to a state that needs a connection and does not have it. Call upEvent when successfull, or downEvent when failed.


startAction

protected abstract void startAction()
Start sending. Called whenever transitioning to a state that has both a connection and messages to send from a state that lacked either attributes. So, will be called after sending stopped due to broken cnx or idle condition. Call downEvent when stopping due to broken or closed connection, call idleEvent() when no pending message is left.


closeInputAction

protected abstract void closeInputAction()
Reject new messages from now on. Called whenever transitioning from a state that is Messenger.USABLE to a state that is not. No event expected once done.


failAllAction

protected abstract void failAllAction()
Drain pending messages, all failed. Called once output is down and there are still pending messages. Call idleEvent() when done, as a normal result of no pending messages being left.


closeOutputAction

protected abstract void closeOutputAction()
Force close output. Called whenever the underlying connection is to be discarded and never to be needed again. That is either because orderly close has completed, or shutdown is in progress. No event expected once done, but this action must cause any sending in progress to stop eventually. The fact that the sending has stopped must be reported as usual: either with a downEvent(), if the output closure caused the sending process to fail, or with an idleEvent() if the sending of the last message could be sent successfully despite the attempt at interrupting it. Sending is said to be in progress if, and only if, the last call to startAction is more recent than the last call to idleEvent() or downEvent(). It is advisable to also cancel an ongoing connect action, but not mandatory. If a connectAction() later succeeds, then upEvent() must be called as usual. This will result in another call to closeOutputAction().


JXTA J2SE