summaryrefslogtreecommitdiffhomepage
path: root/tools/grpc/cpp
diff options
context:
space:
mode:
authorPavel Odintsov <pavel.odintsov@gmail.com>2015-09-04 13:49:25 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2015-09-04 13:49:25 +0900
commit3077880219958ac5877fd9ea7b20b8e7081cef1a (patch)
tree3c33f671dafc5a2ef68f2bf9ff2d35ee26af19e0 /tools/grpc/cpp
parent148ad5969714bb923d34971f1885d20381d81206 (diff)
Add gRPC API client examples in C++
Diffstat (limited to 'tools/grpc/cpp')
-rw-r--r--tools/grpc/cpp/Makefile79
-rw-r--r--tools/grpc/cpp/README.md60
-rw-r--r--tools/grpc/cpp/gobgp_api_client.cc63
3 files changed, 202 insertions, 0 deletions
diff --git a/tools/grpc/cpp/Makefile b/tools/grpc/cpp/Makefile
new file mode 100644
index 00000000..e277acfb
--- /dev/null
+++ b/tools/grpc/cpp/Makefile
@@ -0,0 +1,79 @@
+CXX = g++
+CPPFLAGS += -I/opt/protobuf_3.0.0_alpha4/include -I/opt/grpc/include -pthread
+CXXFLAGS += -std=c++11
+LDFLAGS += -L/opt/grpc/lib -L/opt/protobuf_3.0.0_alpha4/lib -lgrpc++_unsecure -lgrpc -lgpr -lprotobuf -lpthread -ldl
+PROTOC = protoc
+GRPC_CPP_PLUGIN = grpc_cpp_plugin
+GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
+
+PROTOS_PATH = .
+
+vpath %.proto $(PROTOS_PATH)
+
+all: system-check gobgp_api_client
+
+gobgp_api_client: gobgp_api_client.pb.o gobgp_api_client.grpc.pb.o gobgp_api_client.o
+ $(CXX) $^ $(LDFLAGS) -o $@
+
+%.grpc.pb.cc: %.proto
+ $(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
+
+%.pb.cc: %.proto
+ $(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
+
+clean:
+ rm -f *.o *.pb.cc *.pb.h gobgp_api_client
+
+
+# The following is to test your system and ensure a smoother experience.
+# They are by no means necessary to actually compile a grpc-enabled software.
+
+PROTOC_CMD = which $(PROTOC)
+PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q libprotoc.3
+PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN)
+HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
+ifeq ($(HAS_PROTOC),true)
+HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)
+endif
+HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false)
+
+SYSTEM_OK = false
+ifeq ($(HAS_VALID_PROTOC),true)
+ifeq ($(HAS_PLUGIN),true)
+SYSTEM_OK = true
+endif
+endif
+
+system-check:
+ifneq ($(HAS_VALID_PROTOC),true)
+ @echo " DEPENDENCY ERROR"
+ @echo
+ @echo "You don't have protoc 3.0.0 installed in your path."
+ @echo "Please install Google protocol buffers 3.0.0 and its compiler."
+ @echo "You can find it here:"
+ @echo
+ @echo " https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-1"
+ @echo
+ @echo "Here is what I get when trying to evaluate your version of protoc:"
+ @echo
+ -$(PROTOC) --version
+ @echo
+ @echo
+endif
+ifneq ($(HAS_PLUGIN),true)
+ @echo " DEPENDENCY ERROR"
+ @echo
+ @echo "You don't have the grpc c++ protobuf plugin installed in your path."
+ @echo "Please install grpc. You can find it here:"
+ @echo
+ @echo " https://github.com/grpc/grpc"
+ @echo
+ @echo "Here is what I get when trying to detect if you have the plugin:"
+ @echo
+ -which $(GRPC_CPP_PLUGIN)
+ @echo
+ @echo
+endif
+ifneq ($(SYSTEM_OK),true)
+ @false
+endif
diff --git a/tools/grpc/cpp/README.md b/tools/grpc/cpp/README.md
new file mode 100644
index 00000000..924af8ed
--- /dev/null
+++ b/tools/grpc/cpp/README.md
@@ -0,0 +1,60 @@
+Here you could find nice examples for gobgpd API with C++ client.
+
+I'm using Ubuntu 14.04 LTS x86_64.
+
+For gRPC we need so much dependencies, please make coffee and be ready!
+
+Install ProtoBuffers:
+```bash
+apt-get update
+apt-get install -y gcc make autoconf automake git libtool g++ curl
+
+cd /usr/src
+wget https://github.com/google/protobuf/archive/v3.0.0-alpha-4.tar.gz
+tar -xf v3.0.0-alpha-4.tar.gz
+cd protobuf-3.0.0-alpha-4/
+./autogen.sh
+./configure --prefix=/opt/protobuf_3.0.0_alpha4
+make -j 4
+make install
+```
+
+Install gRPC:
+```bash
+apt-get update
+apt-get install -y gcc make autoconf automake git libtool g++ python-all-dev python-virtualenv
+
+cd /usr/src/
+git clone https://github.com/grpc/grpc.git
+cd grpc
+git submodule update --init
+make -j 4
+make install prefix=/opt/grpc
+```
+
+Add libs to the system path:
+```bash
+echo "/opt/grpc/lib" > /etc/ld.so.conf.d/grpc.conf
+echo "/opt/protobuf_3.0.0_alpha4/lib" > /etc/ld.so.conf.d/protobuf.conf
+ldconfig
+```
+
+Clone this repository and build API example:
+```bash
+export PATH="$PATH:/opt//grpc/bin:/opt/protobuf_3.0.0_alpha4/bin/"
+
+cd /usr/src
+git clone https://github.com/osrg/gobgp.git
+cd gobgp/api/cpp
+cp ../gobgp.proto gobgp_api_client.proto
+make
+```
+
+Let's run it:
+```bash
+./gobgp_api_client
+We received: Peer AS: 65001
+Peer router id: 213.133.111.200
+Peer flops: 0
+BGP state: BGP_FSM_ESTABLISHED
+```
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 <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+
+#include <grpc/grpc.h>
+#include <grpc++/channel.h>
+#include <grpc++/client_context.h>
+#include <grpc++/create_channel.h>
+#include <grpc++/security/credentials.h>
+#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> 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<Grpc::Stub> 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;
+}