OpenSDN source code
sandesh_client_sm.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.h
7 //
8 // Sandesh Client State Machine
9 //
10 
11 #ifndef __SANDESH_CLIENT_SM_H__
12 #define __SANDESH_CLIENT_SM_H__
13 
14 #include <atomic>
15 #include <string>
16 #include <mutex>
17 #include <boost/function.hpp>
18 #include <io/tcp_server.h>
19 #include <sandesh/sandesh_session.h>
20 
21 class SandeshHeader;
22 class SandeshSession;
23 
24 
25 // This is the interface for the Sandesh Client State Machine.
26 //
27 // The user of the state machine instantiates it using
28 // the static function "CreateClientSM"
29 //
30 // The user must provide callbacks by implementing the
31 // SandeshClientSM::Mgr class, which has to be passed in
32 // at creation time.
33 
35 public:
36  class Mgr {
37  public:
38  virtual bool ReceiveMsg(const std::string& msg,
39  const SandeshHeader &header, const std::string &sandesh_name,
40  const uint32_t header_offset) = 0;
41  virtual void SendUVE(int count,
42  const std::string & stateName, const std::string & server,
43  const TcpServer::Endpoint & server_ip,
44  const std::vector<TcpServer::Endpoint> & collectors) = 0;
48  TcpServer::Endpoint ep) = 0;
49  virtual void InitializeSMSession(int connects) = 0;
50  virtual void DeleteSMSession(SandeshSession * session) = 0;
51  virtual StatsClient *stats_client() const = 0;
52  protected:
53  Mgr() {}
54  virtual ~Mgr() {}
55  };
56 
57  typedef enum {
58  IDLE = 0,
60  CONNECT = 2,
62  ESTABLISHED = 4
63  } State;
64  static const int kTickInterval = 30000; // 30 sec .. specified in milliseconds
66  int sm_task_instance, int sm_task_id, bool periodicuve);
67  State state() const { return state_; }
68  virtual const std::string &StateName() const = 0;
70  return session_;
71  }
73  std::scoped_lock l(mtex_); return server_;
74  }
75 
76  // This function is used to start and stop the state machine
77  virtual void SetAdminState(bool down) = 0;
78 
79  // This function should be called when there is a change in the Collector list
80  virtual void SetCollectors(const std::vector<TcpServer::Endpoint> &collectors) = 0;
81 
82  // This function is used to send UVE sandesh's to the server
83  virtual bool SendSandeshUVE(Sandesh* snh) = 0;
84 
85  // This function is used to send sandesh's to the server
86  virtual bool SendSandesh(Sandesh* snh) = 0;
87 
88  virtual ~SandeshClientSM() {}
89 
90 protected:
91  SandeshClientSM(Mgr *mgr): mgr_(mgr), session_(), server_() { state_ = IDLE; }
92 
93  virtual void EnqueDelSession(SandeshSession * session) = 0;
94 
95  void set_session(SandeshSession * session, bool enq) {
96  session = session_.exchange(session);
97  if (session != NULL) {
98  session->set_observer(NULL);
99  session->SetReceiveMsgCb(NULL);
100  session->Close();
101  session->Shutdown();
102  if (enq)
104  }
105  }
106 
107  bool send_session(Sandesh *snh) {
108  return snh->Enqueue(session()->send_queue());
109  }
110 
112  std::scoped_lock l(mtex_); server_ = e;
113  }
114 
115  Mgr * const mgr_;
116  std::atomic<State> state_;
117 
118 private:
119  std::mutex mtex_;
120  std::atomic<SandeshSession *> session_;
122 
124 };
125 
126 #endif
virtual bool ReceiveMsg(const std::string &msg, const SandeshHeader &header, const std::string &sandesh_name, const uint32_t header_offset)=0
virtual void InitializeSMSession(int connects)=0
virtual void DeleteSMSession(SandeshSession *session)=0
virtual StatsClient * stats_client() const =0
virtual SandeshSession * CreateSMSession(TcpSession::EventObserver eocb, SandeshReceiveMsgCb rmcb, TcpServer::Endpoint ep)=0
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
virtual const std::string & StateName() const =0
virtual ~SandeshClientSM()
friend class SandeshClientStateMachineTest
SandeshSession * session()
void set_server(TcpServer::Endpoint e)
SandeshClientSM(Mgr *mgr)
std::atomic< State > state_
TcpServer::Endpoint server_
virtual bool SendSandeshUVE(Sandesh *snh)=0
TcpServer::Endpoint server()
static SandeshClientSM * CreateClientSM(EventManager *evm, Mgr *mgr, int sm_task_instance, int sm_task_id, bool periodicuve)
virtual bool SendSandesh(Sandesh *snh)=0
virtual void SetAdminState(bool down)=0
State state() const
virtual void SetCollectors(const std::vector< TcpServer::Endpoint > &collectors)=0
bool send_session(Sandesh *snh)
std::atomic< SandeshSession * > session_
static const int kTickInterval
void set_session(SandeshSession *session, bool enq)
virtual void EnqueDelSession(SandeshSession *session)=0
virtual void Shutdown()
void SetReceiveMsgCb(SandeshReceiveMsgCb cb)
bool Enqueue(SandeshQueue *queue)
Definition: sandesh.cc:545
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp_server.h:30
void set_observer(EventObserver observer)
Definition: tcp_session.cc:219
boost::function< void(TcpSession *, Event)> EventObserver
Definition: tcp_session.h:59
void Close()
Definition: tcp_session.cc:355
static EventManager evm
boost::function< bool(const std::string &, SandeshSession *)> SandeshReceiveMsgCb