Line data Source code
1 : /* 2 : * * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. 3 : * */ 4 : 5 : // 6 : // stats_client.h 7 : // 8 : 9 : #ifndef __STATS_CLIENT_H__ 10 : #define __STATS_CLIENT_H__ 11 : 12 : #include <mutex> 13 : 14 : #include <boost/asio.hpp> 15 : #include <io/udp_server.h> 16 : #include <sandesh/sandesh.h> 17 : #include <sandesh/sandesh_util.h> 18 : 19 : class StatsClient { 20 : public: 21 : static const uint32_t kEncodeBufferSize = 2048; 22 : StatsClient() {}; 23 : StatsClient(boost::asio::io_context& io_service, const std::string& endpoint); 24 0 : ~StatsClient() {} 25 : virtual void Initialize() = 0; 26 : virtual bool IsConnected() = 0; 27 : virtual bool SendMsg(Sandesh *sandesh) = 0; 28 : virtual size_t SendBuf(uint8_t *data, size_t size) = 0; 29 : }; 30 : 31 : #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) 32 : class StatsClientLocal : public StatsClient { 33 : public: 34 : StatsClientLocal(boost::asio::io_context& io_service, const std::string& stats_collector): 35 : stats_server_ep_(boost::asio::local::datagram_protocol::endpoint(stats_collector)), 36 : is_connected_(false) { 37 : stats_socket_.reset(new boost::asio::local::datagram_protocol::socket(io_service)); 38 : } 39 0 : virtual ~StatsClientLocal() {stats_socket_->close();} 40 : virtual void Initialize(); 41 0 : virtual bool IsConnected() {return is_connected_;} 42 : virtual bool SendMsg(Sandesh *sandesh); 43 : virtual size_t SendBuf(uint8_t *data, size_t size); 44 : private: 45 : boost::asio::local::datagram_protocol::endpoint stats_server_ep_; 46 : boost::scoped_ptr<boost::asio::local::datagram_protocol::socket> stats_socket_; 47 : std::mutex send_mutex_; 48 : bool is_connected_; 49 : }; 50 : #endif 51 : 52 : class StatsClientRemote : public StatsClient { 53 : public: 54 : StatsClientRemote(boost::asio::io_context& io_service, const std::string& stats_collector): 55 : is_connected_(false) { 56 : UdpServer::Endpoint stats_ep; 57 : MakeEndpoint(&stats_ep, stats_collector); 58 : stats_server_ep_ = stats_ep; 59 : stats_socket_.reset(new UdpServer::Socket(io_service)); 60 : } 61 0 : virtual ~StatsClientRemote() {stats_socket_->close();} 62 : virtual void Initialize(); 63 0 : virtual bool IsConnected() {return is_connected_;} 64 : virtual bool SendMsg(Sandesh *sandesh); 65 : virtual size_t SendBuf(uint8_t *data, size_t size); 66 : private: 67 : UdpServer::Endpoint stats_server_ep_; 68 : boost::scoped_ptr<UdpServer::Socket> stats_socket_; 69 : std::mutex send_mutex_; 70 : bool is_connected_; 71 : }; 72 : 73 : #endif // __STATS_CLIENT_H__