Line data Source code
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 0 : void StatsClientLocal::Initialize() { 20 0 : boost::system::error_code ec; 21 0 : stats_socket_->connect(stats_server_ep_, ec); 22 0 : if (ec) { 23 0 : SANDESH_LOG(ERROR, "LOCAL could not connect to socket: " << ec.message()); 24 0 : is_connected_ = false; 25 0 : return; 26 : } 27 0 : is_connected_ = true; 28 : } 29 : 30 0 : size_t StatsClientLocal::SendBuf(uint8_t *data, size_t size) { 31 0 : if (!is_connected_) { 32 0 : Initialize(); 33 : } 34 0 : boost::system::error_code ec; 35 0 : size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec); 36 0 : if (ec) { 37 0 : SANDESH_LOG(ERROR, "LOCAL could not send to socket: " << ec.message()); 38 0 : is_connected_ = false; 39 : } 40 0 : return ret; 41 : } 42 : 43 0 : bool StatsClientLocal::SendMsg(Sandesh *sandesh) { 44 0 : std::scoped_lock lock(send_mutex_); 45 : uint8_t *buffer; 46 0 : 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 0 : new sandesh_trans::TMemoryBuffer(kEncodeBufferSize)); 52 : boost::shared_ptr<sandesh_prot::TJSONProtocol> prot( 53 0 : new sandesh_prot::TJSONProtocol(btrans)); 54 0 : if ((ret = sandesh->Write(prot)) < 0) { 55 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<< 56 : sandesh->Name() << " : " << sandesh->source() << ":" << 57 : sandesh->module() << ":" << sandesh->instance_id() << 58 : " Sequence Number:" << sandesh->seqnum()); 59 0 : Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0, 60 : SandeshTxDropReason::WriteFailed); 61 0 : return true; 62 : } 63 0 : xfer += ret; 64 0 : btrans->getBuffer(&buffer, &offset); 65 0 : SendBuf(buffer, offset); 66 0 : return true; 67 0 : } 68 : #endif 69 : 70 0 : void StatsClientRemote::Initialize() { 71 0 : boost::system::error_code ec; 72 0 : stats_socket_->open(boost::asio::ip::udp::v4(), ec); 73 0 : if (ec) { 74 0 : SANDESH_LOG(ERROR, "REMOTE could not open socket: " << ec.message()); 75 0 : is_connected_ = false; 76 0 : return; 77 : } 78 0 : stats_socket_->connect(stats_server_ep_, ec); 79 0 : if (ec) { 80 0 : SANDESH_LOG(ERROR, "REMOTE could not connect address: " << ec.message()); 81 0 : is_connected_ = false; 82 0 : stats_socket_->close(); 83 0 : return; 84 : } 85 0 : is_connected_ = true; 86 : } 87 : 88 0 : size_t StatsClientRemote::SendBuf(uint8_t *data, size_t size) { 89 0 : if (!is_connected_) { 90 0 : Initialize(); 91 : } 92 0 : boost::system::error_code ec; 93 0 : size_t ret = stats_socket_->send(boost::asio::buffer(data, size), 0, ec); 94 0 : if (ec) { 95 0 : SANDESH_LOG(ERROR, "REMOTE could not send to socket: " << ec.message()); 96 0 : is_connected_ = false; 97 : } 98 0 : return ret; 99 : } 100 : 101 0 : bool StatsClientRemote::SendMsg(Sandesh *sandesh) { 102 0 : std::scoped_lock lock(send_mutex_); 103 : uint8_t *buffer; 104 0 : 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 0 : new sandesh_trans::TMemoryBuffer(kEncodeBufferSize)); 110 : boost::shared_ptr<sandesh_prot::TJSONProtocol> prot( 111 0 : new sandesh_prot::TJSONProtocol(btrans)); 112 0 : if ((ret = sandesh->Write(prot)) < 0) { 113 0 : SANDESH_LOG(ERROR, __func__ << ": Sandesh write FAILED: "<< 114 : sandesh->Name() << " : " << sandesh->source() << ":" << 115 : sandesh->module() << ":" << sandesh->instance_id() << 116 : " Sequence Number:" << sandesh->seqnum()); 117 0 : Sandesh::UpdateTxMsgFailStats(sandesh->Name(), 0, 118 : SandeshTxDropReason::WriteFailed); 119 0 : return true; 120 : } 121 0 : xfer += ret; 122 0 : btrans->getBuffer(&buffer, &offset); 123 0 : SendBuf(buffer, offset); 124 0 : return true; 125 0 : }