From 3077880219958ac5877fd9ea7b20b8e7081cef1a Mon Sep 17 00:00:00 2001 From: Pavel Odintsov Date: Fri, 4 Sep 2015 13:49:25 +0900 Subject: Add gRPC API client examples in C++ --- tools/grpc/cpp/gobgp_api_client.cc | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tools/grpc/cpp/gobgp_api_client.cc (limited to 'tools/grpc/cpp/gobgp_api_client.cc') diff --git a/tools/grpc/cpp/gobgp_api_client.cc b/tools/grpc/cpp/gobgp_api_client.cc new file mode 100644 index 00000000..938b9f0e --- /dev/null +++ b/tools/grpc/cpp/gobgp_api_client.cc @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "gobgp_api_client.grpc.pb.h" + +using grpc::Channel; +using grpc::ClientContext; +using grpc::Status; + +using api::Grpc; + +class GrpcClient { + public: + GrpcClient(std::shared_ptr channel) : stub_(Grpc::NewStub(channel)) {} + + std::string GetAllNeighbor(std::string neighbor_ip) { + api::Arguments request; + request.set_rf(4); + request.set_name(neighbor_ip); + + ClientContext context; + + api::Peer peer; + grpc::Status status = stub_->GetNeighbor(&context, request, &peer); + + if (status.ok()) { + api::PeerConf peer_conf = peer.conf(); + api::PeerInfo peer_info = peer.info(); + + std::stringstream buffer; + + buffer + << "Peer AS: " << peer_conf.remote_as() << "\n" + << "Peer router id: " << peer_conf.id() << "\n" + << "Peer flops: " << peer_info.flops() << "\n" + << "BGP state: " << peer_info.bgp_state(); + + return buffer.str(); + } else { + return "Something wrong"; + } + + } + + private: + std::unique_ptr stub_; +}; + +int main(int argc, char** argv) { + GrpcClient gobgp_client(grpc::CreateChannel("localhost:8080", grpc::InsecureCredentials())); + + std::string reply = gobgp_client.GetAllNeighbor("213.133.111.200"); + std::cout << "We received: " << reply << std::endl; + + return 0; +} -- cgit v1.2.3