📄 messengerstate.java
字号:
/* * * $Id: MessengerState.java,v 1.4 2005/12/20 18:30:01 bondolo Exp $ * * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" * must not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. */package net.jxta.endpoint;/** * 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 discrete operations. * * <p/>In order to use this class, one must implement the various abstract * Action methods, so that they trigger the required operations. * * <p/>Synchronization has to be externally provided and usualy needs to extend * around sections wider than just the invocation of this class' 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 {@link #msgsEvent}, * {@link #saturatedEvent}, and {@link #idleEvent}, which all denote different * states of that queue. It is suggested to use the instance of this class as * the synchronization object. **/public abstract class MessengerState { // All the transition map setup is rather terse because java tends to make // it extremely verbose. We do not want to end up with 1000 lines of code // for what amounts to initializing a table. // Below is a method reference. It permits to put "what to do" in a variable. // The <tt>doIt</tt> method is given the target object because we want our // entire transition table to be a static singleton. Otherwise it would cost // too much initializing each instance of this class. private interface Action { public void doIt(MessengerState s); } // Action method "pointers". // The transition table is static. Otherwise it would cost too much initializing each instance of this class. private final static Action Connect = new Action(){public void doIt(MessengerState s){s.connectAction();} }; private final static Action Closein = new Action(){public void doIt(MessengerState s){s.closeInputAction();} }; private final static Action Start = new Action(){public void doIt(MessengerState s){s.startAction();} }; private final static Action Closeout = new Action(){public void doIt(MessengerState s){s.closeOutputAction();} }; private final static Action Failall = new Action(){public void doIt(MessengerState s){s.failAllAction();} }; private final static Action Closeio = new Action(){public void doIt(MessengerState s){s.closeInputAction();s.closeOutputAction();}}; private final static Action Closefail = new Action(){public void doIt(MessengerState s){s.closeInputAction();s.failAllAction();} }; private final static Action Nop = new Action(){public void doIt(MessengerState s){}; }; // A state: what transition each event causes when in that state. private static class State { int number; State stResolve ; Action acResolve ; State stMsgs ; Action acMsgs ; State stSaturated ; Action acSaturated ; State stClose ; Action acClose ; State stShutdown ; Action acShutdown ; State stUp ; Action acUp ; State stDown ; Action acDown ; State stIdle ; Action acIdle ; void init(int stateNum, Object[] data) { number = stateNum; stResolve = (State) data[0]; acResolve = (Action) data[1]; stMsgs = (State) data[2]; acMsgs = (Action) data[3]; stSaturated = (State) data[4]; acSaturated = (Action) data[5]; stClose = (State) data[6]; acClose = (Action) data[7]; stShutdown = (State) data[8]; acShutdown = (Action) data[9]; stUp = (State) data[10]; acUp = (Action) data[11]; stDown = (State) data[12]; acDown = (Action) data[13]; stIdle = (State) data[14]; acIdle = (Action) data[15]; } } // All the states. (We put them together in a class essentially to simplify initialization). private static class TransitionMap { final static State Unresolved = new State(); final static State ResPending = new State(); final static State Resolving = new State(); final static State ResolSat = new State(); final static State Connected = new State(); final static State Disconned = new State(); final static State Reconning = new State(); final static State ReconSat = new State(); final static State Sending = new State(); final static State SendingSat = new State(); final static State ResClosing = new State(); final static State ReconClosing= new State(); final static State Closing = new State();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -