Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : // 6 : // sandesh_client.h 7 : // 8 : // Sandesh Analytics Database Client 9 : // 10 : 11 : #ifndef __SANDESH_CLIENT_H__ 12 : #define __SANDESH_CLIENT_H__ 13 : 14 : #include <boost/asio.hpp> 15 : #include <boost/statechart/state_machine.hpp> 16 : #include <boost/asio/ip/tcp.hpp> 17 : #include <boost/scoped_ptr.hpp> 18 : #include <boost/ptr_container/ptr_map.hpp> 19 : #include <boost/tuple/tuple.hpp> 20 : 21 : #include <io/tcp_server.h> 22 : #include <io/ssl_server.h> 23 : #include <io/ssl_session.h> 24 : #include <base/queue_task.h> 25 : #include <base/timer.h> 26 : #include <sandesh/stats_client.h> 27 : #include <sandesh/sandesh_session.h> 28 : #include "sandesh_client_sm.h" 29 : #include "sandesh.h" 30 : 31 : class SandeshClient; 32 : class Sandesh; 33 : class SandeshUVE; 34 : class SandeshHeader; 35 : 36 : bool DoCloseSMSession(uint64_t now_usec, uint64_t last_close_usec, 37 : uint64_t last_close_interval_usec, int *close_interval_msec); 38 : 39 : class SandeshClient : public SslServer, public SandeshClientSM::Mgr { 40 : public: 41 : static const int kInitialSMSessionCloseIntervalMSec = 10 * 1000; 42 : static const int kMaxSMSessionCloseIntervalMSec = 60 * 1000; 43 : 44 : SandeshClient(EventManager *evm, const std::vector<Endpoint> &collectors, 45 : const SandeshConfig &config, 46 : bool periodicuve = false); 47 : 48 : virtual ~SandeshClient(); 49 : 50 : void Initiate(); 51 : void Shutdown(); 52 : 53 : virtual SandeshSession *CreateSMSession( 54 : SslSession::EventObserver eocb, 55 : SandeshReceiveMsgCb rmcb, 56 : TcpServer::Endpoint ep); 57 : 58 : void InitializeSMSession(int connects); 59 105 : void DeleteSMSession(SandeshSession * session) { 60 105 : DeleteSession(session); 61 105 : } 62 : bool CloseSMSession(); 63 : bool ReceiveMsg(const std::string& msg, 64 : const SandeshHeader &header, const std::string &sandesh_name, 65 : const uint32_t header_offset); 66 : void SendUVE(int count, 67 : const std::string & stateName, const std::string & server, 68 : const Endpoint & server_ip, const std::vector<Endpoint> & collector_eps); 69 : 70 : bool SendSandesh(Sandesh *snh); 71 : 72 : bool SendSandeshUVE(Sandesh *snh_uve); 73 : 74 : SandeshClientSM::State state() { 75 : return sm_->state(); 76 : } 77 : 78 175 : bool IsSession() { 79 175 : if (sm_->session()) return true; 80 84 : else return false; 81 : } 82 : 83 13835 : SandeshSession * session() const { 84 13835 : return sm_->session(); 85 : } 86 : 87 : SandeshClientSM* state_machine() const { 88 : return sm_.get(); 89 : } 90 : 91 105 : StatsClient* stats_client() const { 92 105 : return stats_client_.get(); 93 : } 94 : 95 : void SetDscpValue(uint8_t value); 96 0 : uint8_t dscp_value() const { return dscp_value_; } 97 : 98 : void SetSessionWaterMarkInfo(Sandesh::QueueWaterMarkInfo &scwm); 99 : void ResetSessionWaterMarkInfo(); 100 : void GetSessionWaterMarkInfo( 101 : std::vector<Sandesh::QueueWaterMarkInfo> &scwm_info) const; 102 : void ReConfigCollectors(const std::vector<std::string>&); 103 : 104 0 : int session_close_interval_msec() const { 105 0 : return session_close_interval_msec_; 106 : } 107 0 : uint64_t session_close_time_usec() const { 108 0 : return session_close_time_usec_; 109 : } 110 : 111 : friend class CollectorInfoRequest; 112 : protected: 113 : virtual SslSession *AllocSession(SslSocket *socket); 114 : bool CloseSMSessionInternal(); 115 : 116 : private: 117 : static const int kSMTaskInstance = 0; 118 : static const std::string kSMTask; 119 : static const int kSessionTaskInstance = Task::kTaskInstanceAny; 120 : static const std::string kSessionWriterTask; 121 : static const std::string kSessionReaderTask; 122 : static const std::vector<Sandesh::QueueWaterMarkInfo> kSessionWaterMarkInfo; 123 : 124 : int sm_task_instance_; 125 : int sm_task_id_; 126 : int session_task_instance_; 127 : int session_writer_task_id_; 128 : int session_reader_task_id_; 129 : uint8_t dscp_value_; 130 : std::vector<Endpoint> collectors_; 131 : std::string stats_collector_; 132 : boost::scoped_ptr<SandeshClientSM> sm_; 133 : boost::scoped_ptr<StatsClient> stats_client_; 134 : std::vector<Sandesh::QueueWaterMarkInfo> session_wm_info_; 135 : static bool task_policy_set_; 136 : int session_close_interval_msec_; 137 : uint64_t session_close_time_usec_; 138 : 139 : bool ReceiveCtrlMsg(const std::string &msg, 140 : const SandeshHeader &header, const std::string &sandesh_name, 141 : const uint32_t header_offset); 142 : 143 : DISALLOW_COPY_AND_ASSIGN(SandeshClient); 144 : }; 145 : 146 : #endif // __SANDESH_CLIENT_H__