OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stats_client.cc
Go to the documentation of this file.
1 /*
2  * * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  * */
4 
5 //
6 // stats_client.cc
7 //
8 
9 
10 #include <boost/bind.hpp>
11 #include <boost/assign.hpp>
12 #include <sandesh/transport/TBufferTransports.h>
13 #include <sandesh/protocol/TJSONProtocol.h>
14 #include <stats_client.h>
15 
16 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
17 void StatsClientLocal::Initialize() {
18  boost::system::error_code ec;
19  stats_socket_->connect(stats_server_ep_, ec);
20  if (ec) {
21  SANDESH_LOG(ERROR, "LOCAL could not connect to socket: " << ec.message());
22  is_connected_ = false;
23  return;
24  }
25  is_connected_ = true;
26 }
27 
28 size_t StatsClientLocal::SendBuf(uint8_t *data, size_t size) {
29  if (!is_connected_) {
30  Initialize();
31  }
32  boost::system::error_code ec;
33  size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec);
34  if (ec) {
35  SANDESH_LOG(ERROR, "LOCAL could not send to socket: " << ec.message());
36  is_connected_ = false;
37  }
38  return ret;
39 }
40 
41 bool StatsClientLocal::SendMsg(Sandesh *sandesh) {
42  tbb::mutex::scoped_lock lock(send_mutex_);
43  uint8_t *buffer;
44  int32_t xfer = 0, ret = 0;
45  uint32_t offset;
46  namespace sandesh_prot = contrail::sandesh::protocol;
47  namespace sandesh_trans = contrail::sandesh::transport;
48  boost::shared_ptr<sandesh_trans::TMemoryBuffer> btrans(
49  new sandesh_trans::TMemoryBuffer(kEncodeBufferSize));
50  boost::shared_ptr<sandesh_prot::TJSONProtocol> prot(
51  new sandesh_prot::TJSONProtocol(btrans));
52  if ((ret = sandesh->Write(prot)) < 0) {
53  SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<<
54  sandesh->Name() << " : " << sandesh->source() << ":" <<
55  sandesh->module() << ":" << sandesh->instance_id() <<
56  " Sequence Number:" << sandesh->seqnum());
57  Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0,
58  SandeshTxDropReason::WriteFailed);
59  return true;
60  }
61  xfer += ret;
62  btrans->getBuffer(&buffer, &offset);
63  SendBuf(buffer, offset);
64  return true;
65 }
66 #endif
67 
69  boost::system::error_code ec;
70  stats_socket_->open(boost::asio::ip::udp::v4(), ec);
71  if (ec) {
72  SANDESH_LOG(ERROR, "REMOTE could not open socket: " << ec.message());
73  is_connected_ = false;
74  return;
75  }
76  stats_socket_->connect(stats_server_ep_, ec);
77  if (ec) {
78  SANDESH_LOG(ERROR, "REMOTE could not connect address: " << ec.message());
79  is_connected_ = false;
80  stats_socket_->close();
81  return;
82  }
83  is_connected_ = true;
84 }
85 
86 size_t StatsClientRemote::SendBuf(uint8_t *data, size_t size) {
87  if (!is_connected_) {
88  Initialize();
89  }
90  boost::system::error_code ec;
91  size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec);
92  if (ec) {
93  SANDESH_LOG(ERROR, "REMOTE could not send to socket: " << ec.message());
94  is_connected_ = false;
95  }
96  return ret;
97 }
98 
100  tbb::mutex::scoped_lock lock(send_mutex_);
101  uint8_t *buffer;
102  int32_t xfer = 0, ret = 0;
103  uint32_t offset;
104  namespace sandesh_prot = contrail::sandesh::protocol;
105  namespace sandesh_trans = contrail::sandesh::transport;
106  boost::shared_ptr<sandesh_trans::TMemoryBuffer> btrans(
107  new sandesh_trans::TMemoryBuffer(kEncodeBufferSize));
108  boost::shared_ptr<sandesh_prot::TJSONProtocol> prot(
109  new sandesh_prot::TJSONProtocol(btrans));
110  if ((ret = sandesh->Write(prot)) < 0) {
111  SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<<
112  sandesh->Name() << " : " << sandesh->source() << ":" <<
113  sandesh->module() << ":" << sandesh->instance_id() <<
114  " Sequence Number:" << sandesh->seqnum());
115  Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0,
116  SandeshTxDropReason::WriteFailed);
117  return true;
118  }
119  xfer += ret;
120  btrans->getBuffer(&buffer, &offset);
121  SendBuf(buffer, offset);
122  return true;
123 }
virtual void Initialize()
Definition: stats_client.cc:68
virtual bool SendMsg(Sandesh *sandesh)
Definition: stats_client.cc:99
virtual const char * Name() const
Definition: p/sandesh.h:277
#define SANDESH_LOG(_Level, _Msg)
Definition: p/sandesh.h:474
static void UpdateTxMsgFailStats(const std::string &msg_name, uint64_t bytes, SandeshTxDropReason::type dreason)
Definition: sandesh.cc:890
static std::string module()
Definition: p/sandesh.h:291
tbb::mutex send_mutex_
Definition: stats_client.h:68
boost::scoped_ptr< UdpServer::Socket > stats_socket_
Definition: stats_client.h:67
virtual const uint32_t seqnum()
Definition: p/sandesh.h:275
static const uint32_t kEncodeBufferSize
Definition: stats_client.h:20
virtual int32_t Write(boost::shared_ptr< contrail::sandesh::protocol::TProtocol > oprot) const =0
static std::string source()
Definition: p/sandesh.h:289
static int kEncodeBufferSize
Definition: sandesh_http.cc:51
virtual size_t SendBuf(uint8_t *data, size_t size)
Definition: stats_client.cc:86
static std::string instance_id()
Definition: p/sandesh.h:293
UdpServer::Endpoint stats_server_ep_
Definition: stats_client.h:66