OpenSDN source code
sandesh_client_sm_priv.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 //
7 // sandesh_client_sm_priv.h
8 //
9 // Sandesh Client State Machine
10 //
11 
12 #ifndef __SANDESH_CLIENT_SM_PRIV_H__
13 #define __SANDESH_CLIENT_SM_PRIV_H__
14 
15 #include <mutex>
16 
17 #include <boost/asio.hpp>
18 #include <boost/statechart/state_machine.hpp>
19 
20 #include "base/queue_task.h"
21 #include "base/timer.h"
22 #include "io/tcp_session.h"
23 #include "sandesh_client_sm.h"
24 
25 namespace sc = boost::statechart;
26 
27 namespace scm {
28 
29 // states
30 struct Idle;
31 struct Disconnect;
32 struct Connect;
33 struct ClientInit;
34 struct Established;
35 struct EvStart;
36 
37 } // namespace scm
38 
41 typedef boost::function<bool(SandeshClientSMImpl *)> EvValidate;
42 
44  public sc::state_machine<SandeshClientSMImpl, scm::Idle> {
45 
46 public:
47  static const int kConnectInterval = 30;
48  static const int kIdleHoldTime = 5000; //5 sec .. specified in milliseconds
49 
50  SandeshClientSMImpl(EventManager *evm, Mgr *mgr, int sm_task_instance,
51  int sm_task_id, bool periodicuve);
52  virtual ~SandeshClientSMImpl();
53 
54  Mgr * const GetMgr() { return mgr_; }
55  void set_session(SandeshSession * session, bool enq = true) {
57  }
58  bool send_session(Sandesh *snh) {
60  }
61 
62  void SetAdminState(bool down);
63  void SetCollectors(const std::vector<TcpServer::Endpoint>& collectors);
64  void GetCollectors(std::vector<TcpServer::Endpoint>& collectors);
67  bool SendSandeshUVE(Sandesh* snh);
68  bool SendSandesh(Sandesh* snh);
70 
71  // Feed session events into the state machine.
73 
74  // Receive incoming message
75  bool OnMessage(SandeshSession *session, const std::string &msg);
76 
77  // State transitions
78  template <class Ev> void OnIdle(const Ev &event);
79 
80  // In state reactions
81  template <class Ev> void ReleaseSandesh(const Ev &event);
82  template <class Ev> void DeleteTcpSession(const Ev &event);
83 
84  void StartConnectTimer(int seconds);
85  void CancelConnectTimer();
86  bool ConnectTimerRunning();
87  void FireConnectTimer();
88 
89  void StartIdleHoldTimer();
90  void CancelIdleHoldTimer();
91  bool IdleHoldTimerRunning();
92  void IdleHoldTimerFired();
93 
94  bool CollectorUpdate(const std::vector<TcpServer::Endpoint> &collectors);
95  bool CollectorChange();
96 
97  // Calculate Timer value for active to connect transition.
98  int GetConnectTime() const;
99 
100  const std::string &StateName() const;
101  const std::string &StateName(SandeshClientSM::State state) const;
102  const std::string &LastStateName() const;
103 
105  last_state_ = state;
106  state_ = state;
108  }
109  State get_state() const { return state_; }
110 
111  int connects_inc() { connects_++; return connects_; }
112  int connects() const { return connects_; }
115  int idle_hold_time() const { return idle_hold_time_; }
119  }
120 
121  void set_last_event(const std::string &event) {
122  std::scoped_lock lock(mutex_);
123  last_event_ = event;
125  }
126  const std::string last_event() const {
127  std::scoped_lock lock(mutex_);
128  return last_event_;
129  }
131  std::scoped_lock lock(mutex_);
132  last_state_ = IDLE;
133  last_event_ = "";
134  }
135 
136  void set_collector_name(const std::string& cname) { coll_name_ = cname; }
137  std::string collector_name() { return coll_name_; }
138 
139  void unconsumed_event(const sc::event_base &event);
140  void SendUVE () {
142  collectors_);
143  }
144 private:
145  static const int kStatisticsSendInterval = 30000; // 30 sec .. specified in milliseconds
146  struct EventContainer {
147  boost::intrusive_ptr<const sc::event_base> event;
149  };
150 
151  void StartStatisticsTimer();
152  bool StatisticsTimerExpired();
153  void TimerErrorHanlder(std::string name, std::string error);
154  bool ConnectTimerExpired();
155  bool IdleHoldTimerExpired();
156 
157  template <typename Ev> void Enqueue(const Ev &event);
158  bool DequeueEvent(EventContainer ec);
159  void UpdateEventDequeue(const sc::event_base &event);
160  void UpdateEventDequeueFail(const sc::event_base &event);
161  void UpdateEventEnqueue(const sc::event_base &event);
162  void UpdateEventEnqueueFail(const sc::event_base &event);
163  void UpdateEventStats(const sc::event_base &event, bool enqueue, bool fail);
164 
165  std::vector<TcpServer::Endpoint> collectors_;
176  bool deleted_;
180  uint64_t state_since_;
181  std::string last_event_;
182  uint64_t last_event_at_;
183  std::string coll_name_;
184  mutable std::mutex mutex_;
185  std::string generator_key_;
187 
189 
191 };
192 
193 #endif // __SANDESH_CLIENT_SM_PRIV_H__
void GetCollectors(std::vector< TcpServer::Endpoint > &collectors)
void UpdateEventStats(const sc::event_base &event, bool enqueue, bool fail)
void UpdateEventDequeueFail(const sc::event_base &event)
bool send_session(Sandesh *snh)
void UpdateEventEnqueueFail(const sc::event_base &event)
friend class SandeshClientStateMachineTest
bool CollectorUpdate(const std::vector< TcpServer::Endpoint > &collectors)
void OnSessionEvent(TcpSession *session, TcpSession::Event event)
std::vector< TcpServer::Endpoint > collectors_
void Enqueue(const Ev &event)
static const int kIdleHoldTime
void set_last_event(const std::string &event)
void ReleaseSandesh(const Ev &event)
TcpServer::Endpoint GetNextCollector()
void EnqueDelSession(SandeshSession *session)
const std::string last_event() const
void SetAdminState(bool down)
void DeleteTcpSession(const Ev &event)
bool OnMessage(SandeshSession *session, const std::string &msg)
void StartConnectTimer(int seconds)
DISALLOW_COPY_AND_ASSIGN(SandeshClientSMImpl)
bool SendSandeshUVE(Sandesh *snh)
void SetCollectors(const std::vector< TcpServer::Endpoint > &collectors)
WorkQueue< EventContainer > work_queue_
TcpServer::Endpoint GetCollector() const
void set_idle_hold_time(int idle_hold_time)
bool SendSandesh(Sandesh *snh)
void TimerErrorHanlder(std::string name, std::string error)
SandeshEventStatistics event_stats_
static const int kStatisticsSendInterval
void UpdateEventDequeue(const sc::event_base &event)
bool DequeueEvent(EventContainer ec)
void set_state(State state)
TcpServer::Endpoint active_
const std::string & LastStateName() const
static const int kConnectInterval
void OnIdle(const Ev &event)
const std::string & StateName() const
void unconsumed_event(const sc::event_base &event)
void UpdateEventEnqueue(const sc::event_base &event)
void set_collector_name(const std::string &cname)
void set_session(SandeshSession *session, bool enq=true)
SandeshClientSMImpl(EventManager *evm, Mgr *mgr, int sm_task_instance, int sm_task_id, bool periodicuve)
virtual void SendUVE(int count, const std::string &stateName, const std::string &server, const TcpServer::Endpoint &server_ip, const std::vector< TcpServer::Endpoint > &collectors)=0
SandeshSession * session()
std::atomic< State > state_
TcpServer::Endpoint server()
State state() const
bool send_session(Sandesh *snh)
void set_session(SandeshSession *session, bool enq)
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp_server.h:31
Definition: timer.h:58
static EventManager evm
boost::function< bool(SandeshClientSMImpl *)> EvValidate
boost::function< bool(StateMachine *)> EvValidate
Definition: state_machine.h:34
boost::intrusive_ptr< const sc::event_base > event
static uint64_t UTCTimestampUsec()
Definition: time_util.h:14