OpenSDN source code
xmpp_state_machine.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef __XMPP_STATE_MC__
7 #define __XMPP_STATE_MC__
8 
9 #include <mutex>
10 
11 #include <boost/asio.hpp>
12 #include <boost/statechart/state_machine.hpp>
13 
14 #include "base/queue_task.h"
15 #include "base/timer.h"
16 #include "io/ssl_session.h"
17 #include "xmpp/xmpp_proto.h"
18 
19 namespace sc = boost::statechart;
20 
21 namespace xmsm {
22  struct Idle;
23  struct Active;
24  struct Connect;
25  struct OpenSent;
26  struct OpenConfirm;
27  struct XmppStreamEstablished;
28 }
29 
30 namespace xmsm {
31  struct EvStart;
32  struct Idle;
33 
34 typedef enum {
35  IDLE = 0,
36  ACTIVE = 1,
37  CONNECT = 2,
38  OPENSENT = 3,
40  ESTABLISHED = 5
42 
43 typedef enum {
48 
49 
50 typedef enum {
54 
55 }
56 
57 class XmppConnection;
58 class XmppSession;
59 class TcpSession;
60 class XmppConnectionInfo;
61 
63  public sc::state_machine<XmppStateMachine, xmsm::Idle> {
64 public:
65  static const int kOpenTime = 15; // seconds
66  static const int kConnectInterval = 30; // seconds
67  static const int kHoldTime = 90; // seconds
68  static const int kMaxAttempts = 4;
69  static const int kJitter = 10; // percentage
70 
71  XmppStateMachine(XmppConnection *connection, bool active, bool auth_enabled = false, int config_hold_time = kHoldTime);
73 
74  void Initialize();
75  void Clear();
76  void SetAdminState(bool down);
77 
78  // State transitions
79  void OnStart(const xmsm::EvStart &event);
80 
81  virtual void StartConnectTimer(int seconds);
82  void CancelConnectTimer();
83  virtual void StartOpenTimer(int seconds);
84  void CancelOpenTimer();
85 
86  int GetConfiguredHoldTime() const;
87  virtual void StartHoldTimer();
88  void CancelHoldTimer();
89  void ResetSession();
90 
91  bool IsAuthEnabled() { return auth_enabled_; }
92 
93  void TimerErrorHandler(std::string name, std::string error);
94 
95  // Feed session events into the state machine.
96  virtual void OnSessionEvent(TcpSession *session, TcpSession::Event event);
97 
98  // Receive Passive Open.
100 
101  // Receive incoming message
104  const XmppStanza::XmppMessage *msg);
105 
106  // Receive incoming ssl events
107  //void OnEvent(XmppSession *session, xmsm::SslHandShakeResponse);
109 
110  //void OnSessionError(Error error);
111 
112  // transfer the ownership of the session to the connection.
113  void AssignSession();
114 
115  // Calculate Timer value for active to connect transition.
116  int GetConnectTime() const;
117 
119  handshake_cb_ = cb;
120  }
121 
123 
124  std::string StateName() const;
125  std::string LastStateName() const;
126  std::string LastStateChangeAt() const;
127  xmsm::XmState StateType() const;
129 
130  // getters and setters
133  connection_ = const_cast<XmppConnection *>(connection);
134  }
136  bool IsActiveChannel();
137  bool logUVE();
138  const char *ChannelType();
140  void clear_session();
142  XmppSession *session() { return session_; }
143  void RemoveSession() { session_ = NULL; }
144  void set_state(xmsm::XmState state);
148  return openconfirm_state_;
149  }
150 
153  int get_connect_attempts() const { return attempts_; }
154 
157  int get_keepalive_count() const { return keepalive_count_; }
158 
159  int hold_time() const { return hold_time_; }
160  virtual int hold_time_msecs() const { return hold_time_ * 1000; }
162 
163  void unconsumed_event(const sc::event_base &event);
164 
165  void SendConnectionInfo(const std::string &event,
166  const std::string &nextstate = "");
167 
168  void SendConnectionInfo(XmppConnectionInfo *info, const std::string &event,
169  const std::string &nextstate = "");
172 
173  void set_last_event(const std::string &event) {
174  std::scoped_lock lock(mutex_);
175  last_event_ = event;
177  }
178 
179  void update_last_event(const std::string &event);
180 
181  const std::string last_event() const {
182  std::scoped_lock lock(mutex_);
183  return last_event_;
184  }
185 
189  void AssertOnHoldTimeout();
190  bool HoldTimerExpired();
191  const Timer *hold_timer() const { return hold_timer_; }
192 
193 private:
194  friend class XmppStateMachineTest;
195 
196  bool ConnectTimerExpired();
197  bool OpenTimerExpired();
198  bool Enqueue(const sc::event_base &ev);
199  bool DequeueEvent(boost::intrusive_ptr<const sc::event_base> event);
200  void ProcessEvent(const sc::event_base &event);
202  const XmppStanza::XmppMessage *msg);
203 
213  uint32_t attempts_;
215  bool deleted_;
222  uint64_t state_since_;
223  std::string last_event_;
224  uint64_t last_event_at_;
226  mutable std::mutex mutex_;
227 
229 };
230 
231 #endif
Definition: timer.h:58
bool cancelled() const
Definition: timer.h:104
xmsm::XmOpenConfirmState get_openconfirm_state()
void update_last_event(const std::string &event)
xmsm::XmOpenConfirmState OpenConfirmStateType() const
void set_state(xmsm::XmState state)
void ProcessEvent(const sc::event_base &event)
void set_connection(const XmppConnection *connection)
xmsm::XmState get_state()
virtual void StartHoldTimer()
void SetAdminState(bool down)
XmppSession * session_
XmppStateMachine(XmppConnection *connection, bool active, bool auth_enabled=false, int config_hold_time=kHoldTime)
void DeleteSession(XmppSession *session)
std::string StateName() const
virtual void StartOpenTimer(int seconds)
static const int kConnectInterval
bool DequeueEvent(boost::intrusive_ptr< const sc::event_base > event)
void SendConnectionInfo(XmppConnectionInfo *info, const std::string &event, const std::string &nextstate="")
void OnStart(const xmsm::EvStart &event)
SslHandShakeCallbackHandler HandShakeCbHandler()
void ProcessStreamHeaderMessage(XmppSession *session, const XmppStanza::XmppMessage *msg)
xmsm::XmState StateType() const
void set_hold_time(int hold_time)
XmppConnection * connection()
const char * ChannelType()
WorkQueue< boost::intrusive_ptr< const sc::event_base > > work_queue_
void set_last_event(const std::string &event)
friend class XmppStateMachineTest
void TimerErrorHandler(std::string name, std::string error)
std::string LastStateName() const
bool PassiveOpen(XmppSession *session)
virtual void StartConnectTimer(int seconds)
void SwapXmppConnection(XmppStateMachine *other)
const Timer * hold_timer() const
XmppConnection * connection_
std::string LastStateChangeAt() const
void ProcessMessage(XmppSession *session, const XmppStanza::XmppMessage *msg)
xmsm::XmOpenConfirmState openconfirm_state_
void OnEvent(SslSession *session, xmsm::SslHandShakeResponse)
bool Enqueue(const sc::event_base &ev)
void OnMessage(XmppSession *session, const XmppStanza::XmppMessage *msg)
void set_session(TcpSession *session)
void SetHandShakeCbHandler(SslHandShakeCallbackHandler cb)
int get_keepalive_count() const
xmsm::XmState state_
XmppSession * session()
void set_openconfirm_state(xmsm::XmOpenConfirmState state)
DISALLOW_COPY_AND_ASSIGN(XmppStateMachine)
int GetConfiguredHoldTime() const
virtual int hold_time_msecs() const
void SendConnectionInfo(const std::string &event, const std::string &nextstate="")
int get_connect_attempts() const
const std::string last_event() const
xmsm::XmState last_state_
static const int kMaxAttempts
void ResurrectOldConnection(XmppConnection *connection, XmppSession *session)
static const int kHoldTime
static const int kOpenTime
static const int kJitter
virtual void OnSessionEvent(TcpSession *session, TcpSession::Event event)
void unconsumed_event(const sc::event_base &event)
SslHandShakeCallbackHandler handshake_cb_
@ EvTLSHANDSHAKE_SUCCESS
@ EvTLSHANDSHAKE_FAILURE
@ OPENCONFIRM_FEATURE_NEGOTIATION
@ OPENCONFIRM_FEATURE_SUCCESS
@ OPENCONFIRM_INIT
boost::function< void(SslSessionPtr, const boost::system::error_code &error)> SslHandShakeCallbackHandler
Definition: ssl_session.h:17
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14