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