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