📄 messengerstate.java
字号:
final static State Disconning = new State(); final static State Unresable = new State(); final static State Closed = new State(); final static State Broken = new State(); // The states need to exist before init, because they refer to each other. // We overwrite them in-place with the complete data. static { Object[][] tmpMap = { /* STATE resolve, msgs, saturated, close, shutdown, up, down, idle */ /*UNRESOLVED */{Resolving,Connect,ResPending,Connect,ResolSat,Connect,Closed,Closein, Broken,Closein, Connected,Nop, Unresolved,Nop, Unresolved,Nop }, /*RESOLPENDING */{ResPending,Nop, ResPending,Nop, ResolSat,Nop, ResClosing,Closein, Broken,Closefail, Sending,Start, Unresable,Closefail, Resolving,Nop }, /*RESOLVING */{Resolving,Nop, ResPending,Nop, ResolSat,Nop, Closed,Closein, Broken,Closein, Connected,Nop, Unresable,Closein, Resolving,Nop }, /*RESOLSATURATED */{ResolSat,Nop, ResPending,Nop, ResolSat,Nop, ResClosing,Closein, Broken,Closefail, SendingSat,Start, Unresable,Closefail, Resolving,Nop }, /*CONNECTED */{Connected,Nop, Sending,Start, SendingSat,Start,Closed,Closeio, Broken,Closeio, Connected,Nop, Disconned,Nop, Connected,Nop }, /*DISCONNECTED */{Disconned,Nop, Reconning,Connect, ReconSat,Connect,Closed,Closein, Broken,Closein, Connected,Nop, Disconned,Nop, Disconned,Nop }, /*RECONNECTING */{Reconning,Nop, Reconning,Nop, ReconSat,Nop, ReconClosing,Closein,Broken,Closefail, Sending,Start, Broken,Closefail, Disconned,Nop }, /*RECONSATURATED */{ReconSat,Nop, Reconning,Nop, ReconSat,Nop, ReconClosing,Closein,Broken,Closefail, SendingSat,Start, Broken,Closefail, Disconned,Nop }, /*SENDING */{Sending,Nop, Sending,Nop, SendingSat,Nop, Closing,Closein, Disconning,Closeio, Sending,Nop, Reconning,Connect, Connected,Nop }, /*SENDINGSATURATED*/{SendingSat,Nop, Sending,Nop, SendingSat,Nop, Closing,Closein, Disconning,Closeio, SendingSat,Nop, ReconSat,Connect, Connected,Nop }, /*RESOLCLOSING */{ResClosing,Nop, ResClosing,Nop, ResClosing,Nop, ResClosing,Nop, Broken,Failall, Closing,Start, Unresable,Failall, ResClosing,Nop }, /*RECONCLOSING */{ReconClosing,Nop, ReconClosing,Nop, ReconClosing,Nop,ReconClosing,Nop, Broken,Failall, Closing,Start, Broken,Failall, ReconClosing,Nop}, /*CLOSING */{Closing,Nop, Closing,Nop, Closing,Nop, Closing,Nop, Disconning,Closeout,Closing,Nop, ReconClosing,Connect,Closed,Closeout }, /*DISCONNECTING */{Disconning,Nop, Disconning,Nop, Disconning,Nop, Disconning,Nop, Disconning,Nop, Disconning,Nop, Broken,Failall, Broken,Nop }, /*UNRESOLVABLE */{Unresable,Nop, Unresable,Nop, Unresable,Nop, Unresable,Nop, Unresable,Nop, Unresable,Closeout,Unresable,Nop, Unresable,Nop }, /*CLOSED */{Closed,Nop, Closed,Nop, Closed,Nop, Closed,Nop, Closed,Nop, Closed,Closeout, Closed,Nop, Closed,Nop }, /*BROKEN */{Broken,Nop, Broken,Nop, Broken,Nop, Broken,Nop, Broken,Nop, Broken,Closeout, Broken,Nop, Broken,Nop } }; // install the tmp map in its proper place. Unresolved.init (Messenger.UNRESOLVED , tmpMap[0]); ResPending.init (Messenger.RESOLPENDING , tmpMap[1]); Resolving.init (Messenger.RESOLVING , tmpMap[2]); ResolSat.init (Messenger.RESOLSATURATED , tmpMap[3]); Connected.init (Messenger.CONNECTED , tmpMap[4]); Disconned.init (Messenger.DISCONNECTED , tmpMap[5]); Reconning.init (Messenger.RECONNECTING , tmpMap[6]); ReconSat.init (Messenger.RECONSATURATED , tmpMap[7]); Sending.init (Messenger.SENDING , tmpMap[8]); SendingSat.init (Messenger.SENDINGSATURATED, tmpMap[9]); ResClosing.init (Messenger.RESOLCLOSING , tmpMap[10]); ReconClosing.init(Messenger.RECONCLOSING , tmpMap[11]); Closing.init (Messenger.CLOSING , tmpMap[12]); Disconning.init (Messenger.DISCONNECTING , tmpMap[13]); Unresable.init (Messenger.UNRESOLVABLE , tmpMap[14]); Closed.init (Messenger.CLOSED , tmpMap[15]); Broken.init (Messenger.BROKEN , tmpMap[16]); } } /** * The current state! **/ private volatile State state = null; /** * Constructs a new messenger state machine. * * <p/>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. * * @param connected If <tt>true</tt>, the initial state is {@link Messenger#CONNECTED} otherwise {@link Messenger#UNRESOLVED}. **/ protected MessengerState(boolean connected) { state = connected ? TransitionMap.Connected : TransitionMap.Unresolved; } /** * @return The current state. **/ public int getState() { // getState is always just a peek. It needs no sync. return state.number; } /** * Event input. **/ public void resolveEvent() { Action a = state.acResolve; state = state.stResolve; a.doIt(this); } public void msgsEvent() { Action a = state.acMsgs; state = state.stMsgs; a.doIt(this); } public void saturatedEvent() { Action a = state.acSaturated; state = state.stSaturated; a.doIt(this); } public void closeEvent() { Action a = state.acClose; state = state.stClose; a.doIt(this); } public void shutdownEvent() { Action a = state.acShutdown; state = state.stShutdown; a.doIt(this); } public void upEvent() { Action a = state.acUp; state = state.stUp; a.doIt(this); } public void downEvent() { Action a = state.acDown; state = state.stDown; a.doIt(this); } public void idleEvent() { Action a = state.acIdle; state = state.stIdle; a.doIt(this); } /** * Actions they're always called in sequence by event methods. * * Actions must not call event methods in sequence. **/ /** * 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. **/ protected abstract void connectAction(); /** * 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 {@link #idleEvent} when no pending message is left. **/ protected abstract void startAction(); /** * Reject new messages from now on. Called whenever transitioning from a state that is {@link Messenger#USABLE} to a state * that is not. No event expected once done. **/ protected abstract void closeInputAction(); /** * Drain pending messages, all failed. Called once output is down and there are still pending messages. * Call {@link #idleEvent} when done, as a normal result of no pending messages being left. **/ protected abstract void failAllAction(); /** * 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 * <b>must</b> cause any sending in progress to stop eventually. The fact that the sending has stopped must be reported as * usual: either with a {@link #downEvent}, if the output closure caused the sending process to fail, or with an {@link * #idleEvent} if the sending of the last message could be sent successfully despite the attempt at interrupting it. * * <p/>Sending is said to be in progress if, and only if, the last call to startAction is more recent than the last call to * {@link #idleEvent} or {@link #downEvent}. * * <p/>It is advisable to also cancel an ongoing connect action, but not mandatory. If a {@link #connectAction} later succeeds, * then {@link #upEvent} <b>must</b> be called as usual. This will result in another call to {@link #closeOutputAction}. **/ protected abstract void closeOutputAction();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -