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