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