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