MFClient  1.3.0
 All Classes Functions Variables Enumerations
SessionState.h
1 /*
2  * Copyright (C) 2015 MarketFactory, Inc. All rights reserved.
3  * Use in source and binary forms, with or without modification, is permitted
4  * provided that the following conditions are met:
5  * 1. You may only use this software for internal evaluation and testing
6  * purposes and may not use the software for engaging
7  * in live trades, unless and until you have entered into a separate agreement
8  * with MarketFactory governing the use of
9  * MarketFactory software in a production environment.
10  * 2. You may not distribute the software (in either source or binary forms) to
11  * third parties.
12  * 3. All copies of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer.
14  * THIS SOFTWARE IS PROVIDED BY MARKETFACTORY AND ITS LICENSORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL MARKETFACTORY OR ITS LICENSORS BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _MFSESSIONSTATE_H_
30 #define _MFSESSIONSTATE_H_
31 
32 #include <boost/atomic.hpp>
33 #include <iostream>
34 
37 class SessionState {
38  public:
39 
41  typedef enum {
42  NEW,
43  LOGGING_ON,
44  LOGGED_ON,
45  LOGGING_OUT_USER_INITIATED,
46  LOGGING_OUT_SERVER_INITIATED,
47  LOGGED_OUT_POSSIBLE_AUTO_RELOGON,
48  LOGGED_OUT_NO_AUTO_RELOGON
49  } State;
50 
51  private:
52  bool internalIsAutoRelogonPossible() {
53  return LOGGED_OUT_POSSIBLE_AUTO_RELOGON == getState();
54  }
55 
56  bool internalIsLoggedOn() { return LOGGED_ON == getState(); }
57 
58  bool internalIsManualLogonPossible() {
59  return (NEW == getState() ||
60  LOGGED_OUT_POSSIBLE_AUTO_RELOGON == getState() ||
61  LOGGED_OUT_NO_AUTO_RELOGON == getState());
62  }
63 
64  bool internalIsWaitingForLogoutResponseFromServer() {
65  return LOGGING_OUT_USER_INITIATED == getState();
66  }
67 
68  bool internalIsLoggingOn() { return LOGGING_ON == getState(); }
69 
70  bool internalIsManualLogoutPossible() { return (LOGGED_ON == getState()); }
71 
72  boost::atomic<State> _state;
73 
74  public:
75 
77  SessionState();
78 
80  State getState() { return _state.load(); }
81 
83  void setState(State state) {
84  _state.store(state);
85 #ifdef DEBUG_SESSIONSTATE
86  std::cerr << "SessionState - setState to " << getCurrentState() << " "
87  << getCurrentStateName() << std::endl;
88 #endif
89  }
90 
94  bool logon() {
95  State currentState = getState();
96  return (internalIsManualLogonPossible() &&
97  _state.compare_exchange_strong(currentState, LOGGING_ON));
98  }
99 
110  bool logout() {
111  bool manLogoutPoss = internalIsManualLogoutPossible();
112  setState(LOGGING_OUT_USER_INITIATED);
113  return manLogoutPoss;
114  }
115 
117  const char* getCurrentStateName() {
118  static const char* States[] = {
119  "NEW", "LOGGING_ON",
120  "LOGGED_ON", "LOGGING_OUT_USER_INITIATED",
121  "LOGGING_OUT_SERVER_INITIATED", "LOGGED_OUT_POSSIBLE_AUTO_RELOGON",
122  "LOGGED_OUT_NO_AUTO_RELOGON"};
123 
124  return States[getState()];
125  }
126 
128  bool isLoggedOn() { return internalIsLoggedOn(); }
129 
131  bool isLoggingOn() { return internalIsLoggingOn(); }
132 
135 
139  if (internalIsLoggingOn()) {
140  setState(LOGGED_ON);
141  }
142  }
143 
144 
148  if (internalIsLoggingOn() || internalIsLoggedOn()) {
149  setState(LOGGING_OUT_SERVER_INITIATED);
150  }
151  }
152 
156 #ifdef DEBUG_SESSIONSTATE
157  std::cerr << "SessionState - logoutResponseFromServerReceived" << std::endl;
158 #endif
159  setState(LOGGED_OUT_NO_AUTO_RELOGON);
160  }
161 
165  bool relogon() {
166  State currentState = getState();
167  return (currentState == LOGGED_OUT_POSSIBLE_AUTO_RELOGON &&
168  _state.compare_exchange_strong(currentState, LOGGING_ON));
169  }
170 
173 #ifdef DEBUG_SESSIONSTATE
174  std::cerr << "SessionState - autoRelogonFailed" << std::endl;
175 #endif
176  setState(LOGGED_OUT_POSSIBLE_AUTO_RELOGON);
177  }
178 
181  return internalIsWaitingForLogoutResponseFromServer();
182  }
183 
191  void disconnected(bool reconnectionPossible) {
192 #ifdef DEBUG_SESSIONSTATE
193  std::cerr << "SessionState - disconnected reconnectionPossible = "
194  << reconnectionPossible << std::endl;
195 #endif
196  if (LOGGING_OUT_USER_INITIATED == getState()) {
197  setState(LOGGED_OUT_NO_AUTO_RELOGON);
198  } else {
199  if (reconnectionPossible) {
200  setState(LOGGED_OUT_POSSIBLE_AUTO_RELOGON);
201  } else {
202  setState(LOGGED_OUT_NO_AUTO_RELOGON);
203  }
204  }
205  }
206 
209 #ifdef DEBUG_SESSIONSTATE
210  std::cerr << "SessionState - connectionFailed" << std::endl;
211 #endif
212  setState(LOGGED_OUT_NO_AUTO_RELOGON);
213  }
214 
217 #ifdef DEBUG_SESSIONSTATE
218  std::cerr << "SessionState - isUserInitiatedLogout" << std::endl;
219 #endif
220  return LOGGING_OUT_USER_INITIATED == getState();
221  }
222 };
223 
224 #endif /* _MFSESSIONSTATE_H_ */
225 
bool isLoggingOn()
Is the session currently logging on?
Definition: SessionState.h:131
bool logout()
Definition: SessionState.h:110
bool isLoggedOn()
Is the session logged on?
Definition: SessionState.h:128
bool isWaitingForLogoutResponseFromServer()
Is the session waiting for a logout response from the server?
Definition: SessionState.h:180
void disconnected(bool reconnectionPossible)
Definition: SessionState.h:191
State
The enumeration of valid Session States.
Definition: SessionState.h:41
void setState(State state)
Bluntly set the new state.
Definition: SessionState.h:83
bool relogon()
If we are logged out and set for autorelogon, do it
Definition: SessionState.h:165
void connectionFailed()
Connection attempt failed. Don&#39;t try to auto login again.
Definition: SessionState.h:208
State getCurrentState()
Get the current state enum value.
Definition: SessionState.h:134
void logonResponseReceived()
Definition: SessionState.h:138
void logoutFromServerReceived()
Definition: SessionState.h:147
void autoRelogonFailed()
Auto relogon failed. Set the state to logged out and possible auto relogon.
Definition: SessionState.h:172
State getState()
Get the current state enum.
Definition: SessionState.h:80
const char * getCurrentStateName()
Get a self-documenting text version of the current state.
Definition: SessionState.h:117
void logoutResponseFromServerReceived()
Definition: SessionState.h:155
Definition: SessionState.h:37
SessionState()
Default constructor that sets the state to NEW.
Definition: SessionState.cpp:10
bool logon()
Definition: SessionState.h:94
bool isUserInitiatedLogout()
Has the user initiated a logout?
Definition: SessionState.h:216