/
xmpp_state_machine.h
172 lines (135 loc) · 4.79 KB
/
xmpp_state_machine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#ifndef __XMPP_STATE_MC__
#define __XMPP_STATE_MC__
#include <boost/asio.hpp>
#include <boost/statechart/state_machine.hpp>
#include <tbb/mutex.h>
#include "base/queue_task.h"
#include "base/timer.h"
#include "io/tcp_session.h"
#include "xmpp/xmpp_proto.h"
namespace sc = boost::statechart;
namespace xmsm {
struct Idle;
struct Active;
struct Connect;
struct OpenSent;
struct OpenConfirm;
struct Established;
}
namespace xmsm {
struct EvStart;
struct Idle;
typedef enum {
IDLE = 0,
ACTIVE = 1,
CONNECT = 2,
OPENSENT = 3,
OPENCONFIRM = 4,
ESTABLISHED = 5
} XmState;
}
class XmppConnection;
class XmppSession;
class TcpSession;
class XmppConnectionInfo;
class XmppStateMachine :
public sc::state_machine<XmppStateMachine, xmsm::Idle> {
public:
static const int kOpenTime = 15; // seconds
static const int kConnectInterval = 30; // seconds
static const int kHoldTime = 90; // seconds
static const int kMaxAttempts = 4;
static const int kJitter = 10; // percentage
XmppStateMachine(XmppConnection *connection, bool active);
~XmppStateMachine();
void Initialize();
void Clear();
void SetAdminState(bool down);
// State transitions
void OnStart(const xmsm::EvStart &event);
virtual void StartConnectTimer(int seconds);
void CancelConnectTimer();
virtual void StartOpenTimer(int seconds);
void CancelOpenTimer();
int GetConfiguredHoldTime() const;
virtual void StartHoldTimer();
void CancelHoldTimer();
void ResetSession();
void TimerErrorHandler(std::string name, std::string error);
// Feed session events into the state machine.
void OnSessionEvent(TcpSession *session, TcpSession::Event event);
// Receive Passive Open.
bool PassiveOpen(XmppSession *session);
// Receive incoming message
void OnMessage(XmppSession *session, const XmppStanza::XmppMessage *msg);
//void OnSessionError(Error error);
// transfer the ownership of the session to the connection.
void AssignSession();
// Calculate Timer value for active to connect transition.
int GetConnectTime() const;
std::string StateName() const;
std::string LastStateName() const;
std::string LastStateChangeAt() const;
xmsm::XmState StateType() const;
// getters and setters
XmppConnection *connection() { return connection_; }
bool IsActiveChannel();
bool logUVE();
const char *ChannelType();
void set_session(TcpSession *session);
void clear_session();
void DeleteSession(XmppSession *session);
XmppSession *session() { return session_; }
void set_state(xmsm::XmState state);
xmsm::XmState get_state() { return state_; }
void connect_attempts_inc() { attempts_++; }
void connect_attempts_clear() { attempts_ = 0; }
int get_connect_attempts() const { return attempts_; }
void keepalive_count_inc() { keepalive_count_++; }
void keepalive_count_clear() { keepalive_count_ = 0; }
int get_keepalive_count() const { return keepalive_count_; }
int hold_time() const { return hold_time_; }
virtual int hold_time_msecs() const { return hold_time_ * 1000; }
void set_hold_time(int hold_time) { hold_time_ = hold_time; }
void unconsumed_event(const sc::event_base &event);
void SendConnectionInfo(const std::string &event,
const std::string &nextstate = "");
void SendConnectionInfo(XmppConnectionInfo *info, const std::string &event,
const std::string &nextstate = "");
void set_last_event(const std::string &event);
const std::string &last_event() const { return last_event_; }
bool ConnectTimerCancelled() { return connect_timer_->cancelled(); }
bool OpenTimerCancelled() { return open_timer_->cancelled(); }
bool HoldTimerCancelled() { return hold_timer_->cancelled(); }
void AssertOnHoldTimeout();
private:
friend class XmppStateMachineTest;
bool ConnectTimerExpired();
bool OpenTimerExpired();
bool HoldTimerExpired();
bool Enqueue(const sc::event_base &ev);
bool DequeueEvent(boost::intrusive_ptr<const sc::event_base> &event);
WorkQueue<boost::intrusive_ptr<const sc::event_base> > work_queue_;
XmppConnection *connection_;
XmppSession *session_;
TcpServer *server_;
Timer *connect_timer_;
Timer *open_timer_;
Timer *hold_timer_;
int hold_time_;
uint32_t attempts_;
uint32_t keepalive_count_;
bool deleted_;
bool in_dequeue_;
bool is_active_;
xmsm::XmState state_;
xmsm::XmState last_state_;
uint64_t state_since_;
std::string last_event_;
uint64_t last_event_at_;
DISALLOW_COPY_AND_ASSIGN(XmppStateMachine);
};
#endif