View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.openr66.context;
21  
22  import org.waarp.common.state.MachineState;
23  import org.waarp.common.state.Transition;
24  
25  import java.util.EnumSet;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  /**
29   * Finite Dual State Machine for OpenR66 (Requester=R, requesteD=D, Sender=S,
30   * Receive=R)
31   */
32  public enum R66FiniteDualStates {
33    OPENEDCHANNEL, CLOSEDCHANNEL, ERROR, STARTUP, AUTHENTR, AUTHENTD, REQUESTR,
34    REQUESTD, VALID, DATAR, DATAS, ENDTRANSFERR, ENDREQUESTR, ENDTRANSFERS,
35    ENDREQUESTS, TEST, INFORMATION, VALIDOTHER, SHUTDOWN, BUSINESSR, BUSINESSD;
36    // not used in LSH
37    // CONNECTERROR,
38    // KEEPALIVEPACKET
39  
40    private enum R66Transition {
41      tOPENEDCHANNEL(OPENEDCHANNEL, EnumSet.of(STARTUP, CLOSEDCHANNEL, ERROR)),
42      tSTARTUP(STARTUP, EnumSet.of(AUTHENTR, AUTHENTD, CLOSEDCHANNEL, ERROR)),
43      tAUTHENTR(AUTHENTR, EnumSet.of(AUTHENTD, CLOSEDCHANNEL, ERROR)),
44      tAUTHENTD(AUTHENTD,
45                EnumSet.of(REQUESTR, VALIDOTHER, INFORMATION, SHUTDOWN, TEST,
46                           BUSINESSR, BUSINESSD, ENDREQUESTS, CLOSEDCHANNEL,
47                           ERROR)),
48      tREQUESTR(REQUESTR, EnumSet.of(VALID, REQUESTD, ERROR, CLOSEDCHANNEL)),
49      tREQUESTD(REQUESTD, EnumSet.of(VALID, DATAS, DATAR, ENDTRANSFERS, ERROR)),
50      tVALID(VALID, EnumSet.of(REQUESTD, DATAR, DATAS, ENDTRANSFERS, ENDTRANSFERR,
51                               CLOSEDCHANNEL, ERROR)),
52      tDATAS(DATAS, EnumSet.of(DATAS, ENDTRANSFERS, VALID, ERROR)),
53      tDATAR(DATAR, EnumSet.of(DATAR, ENDTRANSFERS, ENDTRANSFERR, ERROR)),
54      tENDTRANSFERS(ENDTRANSFERS, EnumSet.of(ENDTRANSFERR, VALID, ERROR)),
55      tENDTRANSFERR(ENDTRANSFERR, EnumSet.of(ENDREQUESTS, CLOSEDCHANNEL, ERROR)),
56      tENDREQUESTS(ENDREQUESTS, EnumSet.of(ENDREQUESTR, CLOSEDCHANNEL, ERROR)),
57      tENDREQUESTR(ENDREQUESTR, EnumSet.of(CLOSEDCHANNEL, ERROR)),
58      tINFORMATION(INFORMATION, EnumSet.of(VALIDOTHER, CLOSEDCHANNEL, ERROR)),
59      tTEST(TEST, EnumSet.of(TEST, VALIDOTHER)),
60      tVALIDOTHER(VALIDOTHER, EnumSet.of(VALIDOTHER, CLOSEDCHANNEL, ERROR)),
61      tSHUTDOWN(SHUTDOWN, EnumSet.of(CLOSEDCHANNEL, SHUTDOWN, ERROR)),
62      tERROR(ERROR, EnumSet.of(ERROR, SHUTDOWN, CLOSEDCHANNEL)),
63      tCLOSEDCHANNEL(CLOSEDCHANNEL,
64                     EnumSet.of(CLOSEDCHANNEL, ERROR, ENDREQUESTR, ENDREQUESTS)),
65      tBUSINESSR(BUSINESSR,
66                 EnumSet.of(ERROR, BUSINESSD, CLOSEDCHANNEL, VALIDOTHER)),
67      tBUSINESSD(BUSINESSD, EnumSet.of(ERROR, BUSINESSD, BUSINESSR, CLOSEDCHANNEL,
68                                       VALIDOTHER));
69  
70      public final Transition<R66FiniteDualStates> elt;
71  
72      R66Transition(final R66FiniteDualStates state,
73                    final EnumSet<R66FiniteDualStates> set) {
74        elt = new Transition<R66FiniteDualStates>(state, set);
75      }
76    }
77  
78    private static final ConcurrentHashMap<R66FiniteDualStates, EnumSet<R66FiniteDualStates>>
79        stateMap =
80        new ConcurrentHashMap<R66FiniteDualStates, EnumSet<R66FiniteDualStates>>();
81  
82    /**
83     * This method should be called once at startup to initialize the Finite
84     * States association.
85     */
86    public static void initR66FiniteStates() {
87      for (final R66Transition trans : R66Transition.values()) {
88        stateMap.put(trans.elt.getState(), trans.elt.getSet());
89      }
90    }
91  
92    /**
93     * @return a new Session MachineState for OpenR66
94     */
95    public static MachineState<R66FiniteDualStates> newSessionMachineState() {
96      return new MachineState<R66FiniteDualStates>(OPENEDCHANNEL, stateMap);
97    }
98  
99    /**
100    * @param machine the Session MachineState to release
101    */
102   public static void endSessionMachineSate(
103       final MachineState<R66FiniteDualStates> machine) {
104     machine.release();
105   }
106 }