OpenSDN source code
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/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 using namespace boost::placeholders;
17 
18 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
19 void StatsClientLocal::Initialize() {
20  boost::system::error_code ec;
21  stats_socket_->connect(stats_server_ep_, ec);
22  if (ec) {
23  SANDESH_LOG(ERROR, "LOCAL could not connect to socket: " << ec.message());
24  is_connected_ = false;
25  return;
26  }
27  is_connected_ = true;
28 }
29 
30 size_t StatsClientLocal::SendBuf(uint8_t *data, size_t size) {
31  if (!is_connected_) {
32  Initialize();
33  }
34  boost::system::error_code ec;
35  size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec);
36  if (ec) {
37  SANDESH_LOG(ERROR, "LOCAL could not send to socket: " << ec.message());
38  is_connected_ = false;
39  }
40  return ret;
41 }
42 
43 bool StatsClientLocal::SendMsg(Sandesh *sandesh) {
44  std::scoped_lock lock(send_mutex_);
45  uint8_t *buffer;
46  int32_t xfer = 0, ret = 0;
47  uint32_t offset;
48  namespace sandesh_prot = contrail::sandesh::protocol;
49  namespace sandesh_trans = contrail::sandesh::transport;
50  boost::shared_ptr<sandesh_trans::TMemoryBuffer> btrans(
51  new sandesh_trans::TMemoryBuffer(kEncodeBufferSize));
52  boost::shared_ptr<sandesh_prot::TJSONProtocol> prot(
53  new sandesh_prot::TJSONProtocol(btrans));
54  if ((ret = sandesh->Write(prot)) < 0) {
55  SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<<
56  sandesh->Name() << " : " << sandesh->source() << ":" <<
57  sandesh->module() << ":" << sandesh->instance_id() <<
58  " Sequence Number:" << sandesh->seqnum());
60  SandeshTxDropReason::WriteFailed);
61  return true;
62  }
63  xfer += ret;
64  btrans->getBuffer(&buffer, &offset);
65  SendBuf(buffer, offset);
66  return true;
67 }
68 #endif
69 
71  boost::system::error_code ec;
72  stats_socket_->open(boost::asio::ip::udp::v4(), ec);
73  if (ec) {
74  SANDESH_LOG(ERROR, "REMOTE could not open socket: " << ec.message());
75  is_connected_ = false;
76  return;
77  }
78  stats_socket_->connect(stats_server_ep_, ec);
79  if (ec) {
80  SANDESH_LOG(ERROR, "REMOTE could not connect address: " << ec.message());
81  is_connected_ = false;
82  stats_socket_->close();
83  return;
84  }
85  is_connected_ = true;
86 }
87 
88 size_t StatsClientRemote::SendBuf(uint8_t *data, size_t size) {
89  if (!is_connected_) {
90  Initialize();
91  }
92  boost::system::error_code ec;
93  size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec);
94  if (ec) {
95  SANDESH_LOG(ERROR, "REMOTE could not send to socket: " << ec.message());
96  is_connected_ = false;
97  }
98  return ret;
99 }
100 
102  std::scoped_lock lock(send_mutex_);
103  uint8_t *buffer;
104  int32_t xfer = 0, ret = 0;
105  uint32_t offset;
106  namespace sandesh_prot = contrail::sandesh::protocol;
107  namespace sandesh_trans = contrail::sandesh::transport;
108  boost::shared_ptr<sandesh_trans::TMemoryBuffer> btrans(
109  new sandesh_trans::TMemoryBuffer(kEncodeBufferSize));
110  boost::shared_ptr<sandesh_prot::TJSONProtocol> prot(
111  new sandesh_prot::TJSONProtocol(btrans));
112  if ((ret = sandesh->Write(prot)) < 0) {
113  SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<<
114  sandesh->Name() << " : " << sandesh->source() << ":" <<
115  sandesh->module() << ":" << sandesh->instance_id() <<
116  " Sequence Number:" << sandesh->seqnum());
118  SandeshTxDropReason::WriteFailed);
119  return true;
120  }
121  xfer += ret;
122  btrans->getBuffer(&buffer, &offset);
123  SendBuf(buffer, offset);
124  return true;
125 }
static void UpdateTxMsgFailStats(const std::string &msg_name, uint64_t bytes, SandeshTxDropReason::type dreason)
Definition: sandesh.cc:891
virtual bool SendMsg(Sandesh *sandesh)
virtual size_t SendBuf(uint8_t *data, size_t size)
Definition: stats_client.cc:88
virtual void Initialize()
Definition: stats_client.cc:70
#define SANDESH_LOG(_Level, _Msg)
Definition: cpp/sandesh.h:476
static int kEncodeBufferSize
Definition: sandesh_http.cc:53