summaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2018-12-24 23:18:37 +0900
committerFUJITA Tomonori <fujita.tomonori@gmail.com>2018-12-24 23:18:37 +0900
commitbb1bbdd197a971917ae24946a4c4d2da539e5d55 (patch)
tree83ad4849b9b497a1222aad2d914616c007f9827d /tools
parent28d96f985bc59f344894f2e37cea5cb1bea062e5 (diff)
docs: update C++ gRPC API example
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/grpc/cpp/Makefile45
-rw-r--r--tools/grpc/cpp/add_path.cc92
-rw-r--r--tools/grpc/cpp/build.sh11
-rw-r--r--tools/grpc/cpp/gobgp_api_client.cc86
4 files changed, 124 insertions, 110 deletions
diff --git a/tools/grpc/cpp/Makefile b/tools/grpc/cpp/Makefile
index d50df9d9..ddedd69f 100644
--- a/tools/grpc/cpp/Makefile
+++ b/tools/grpc/cpp/Makefile
@@ -1,38 +1,57 @@
+#
+# Copyright 2015 gRPC authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
SYSTEM ?= $(HOST_SYSTEM)
CXX = g++
-CPPFLAGS += -I/usr/local/include -pthread
+CPPFLAGS += `pkg-config --cflags protobuf grpc`
CXXFLAGS += -std=c++11
ifeq ($(SYSTEM),Darwin)
-LDFLAGS += -L/usr/local/lib `pkg-config --libs grpc++` \
- -lgrpc++_reflection \
- -lprotobuf -lpthread -ldl
+LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
+ -lgrpc++_reflection\
+ -ldl
else
-LDFLAGS += -L/usr/local/lib `pkg-config --libs grpc++` \
- -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed \
- -lprotobuf -lpthread -ldl
+LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++ grpc`\
+ -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
+ -ldl
endif
PROTOC = protoc
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
-PROTOS_PATH = .
+PROTOS_PATH = ../../../api
vpath %.proto $(PROTOS_PATH)
-all: system-check gobgp_api_client
+#all: system-check greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server
+all: add_path
-gobgp_api_client: gobgp_api_client.pb.o gobgp_api_client.grpc.pb.o gobgp_api_client.o
- $(CXX) $^ $(LDFLAGS) -L. -lgobgp -o $@
+add_path: gobgp.pb.o gobgp.grpc.pb.o attribute.pb.o attribute.grpc.pb.o capability.pb.o capability.grpc.pb.o add_path.o
+ $(CXX) $^ $(LDFLAGS) -o $@
+.PRECIOUS: %.grpc.pb.cc
%.grpc.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
+.PRECIOUS: %.pb.cc
%.pb.cc: %.proto
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
clean:
- rm -f *.o *.pb.cc *.pb.h gobgp_api_client
+ rm -f *.o *.pb.cc *.pb.h greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server
# The following is to test your system and ensure a smoother experience.
@@ -62,7 +81,7 @@ ifneq ($(HAS_VALID_PROTOC),true)
@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 " https://github.com/google/protobuf/releases/tag/v3.0.0"
@echo
@echo "Here is what I get when trying to evaluate your version of protoc:"
@echo
diff --git a/tools/grpc/cpp/add_path.cc b/tools/grpc/cpp/add_path.cc
new file mode 100644
index 00000000..6a3d1a31
--- /dev/null
+++ b/tools/grpc/cpp/add_path.cc
@@ -0,0 +1,92 @@
+#include <iostream>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <string.h>
+
+#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.grpc.pb.h"
+#include "attribute.grpc.pb.h"
+
+using grpc::Channel;
+using grpc::ClientContext;
+using grpc::Status;
+
+using gobgpapi::GobgpApi;
+
+class GobgpClient
+{
+ public:
+ GobgpClient(std::shared_ptr<Channel> channel)
+ : stub_(GobgpApi::NewStub(channel)) {}
+ void AddPath()
+ {
+ std::cout << "In addRoute \n";
+ // Parameters to AddPath API
+ gobgpapi::AddPathRequest request;
+ ClientContext context;
+ gobgpapi::AddPathResponse response;
+
+ // Path info variable
+ gobgpapi::Path *current_path = new gobgpapi::Path;
+
+ // Updating family info of current_path
+ gobgpapi::Family *current_family = new gobgpapi::Family;
+ current_family->set_afi(gobgpapi::Family::AFI_IP);
+ current_family->set_safi(gobgpapi::Family::SAFI_UNICAST);
+ current_path->set_allocated_family(current_family);
+
+ // Updating nlri info for current_path
+ google::protobuf::Any *current_nlri = new google::protobuf::Any;
+ gobgpapi::IPAddressPrefix current_ipaddrprefix;
+ current_ipaddrprefix.set_prefix("10.0.0.0");
+ current_ipaddrprefix.set_prefix_len(24);
+ current_nlri->PackFrom(current_ipaddrprefix);
+ current_path->set_allocated_nlri(current_nlri);
+
+ // Updating OriginAttribute info for current_path
+ google::protobuf::Any *current_origin = current_path->add_pattrs();
+ gobgpapi::OriginAttribute current_origin_t;
+ current_origin_t.set_origin(0);
+ current_origin->PackFrom(current_origin_t);
+
+ // Updating NextHopAttribute info for current_path
+ google::protobuf::Any *current_next_hop = current_path->add_pattrs();
+ gobgpapi::NextHopAttribute current_next_hop_t;
+ current_next_hop_t.set_next_hop("1.1.1.1");
+ current_next_hop->PackFrom(current_next_hop_t);
+ // Updating CommunitiesAttribute for current_path
+ google::protobuf::Any *current_communities = current_path->add_pattrs();
+ gobgpapi::CommunitiesAttribute current_communities_t;
+ current_communities_t.add_communities(100);
+ current_communities->PackFrom(current_communities_t);
+
+ // Populating the request attributes
+ request.set_table_type(gobgpapi::TableType::GLOBAL);
+ request.set_allocated_path(current_path);
+
+ Status status = stub_->AddPath(&context, request, &response);
+ if (status.ok())
+ {
+ }
+ else
+ {
+ std::cout << status.error_code() << ": " << status.error_message()
+ << std::endl;
+ }
+ }
+
+ private:
+ std::unique_ptr<GobgpApi::Stub> stub_;
+};
+
+int main(int argc, char **argv)
+{
+ GobgpClient client(grpc::CreateChannel("localhost:50051", grpc::InsecureChannelCredentials()));
+
+ client.AddPath();
+}
diff --git a/tools/grpc/cpp/build.sh b/tools/grpc/cpp/build.sh
deleted file mode 100644
index 61b5e060..00000000
--- a/tools/grpc/cpp/build.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/bash
-
-GOBGP_PATH=${GOPATH}/src/github.com/osrg/gobgp
-
-cd ${GOBGP_PATH}/cmd/gobgp/lib
-go build -buildmode=c-shared -o libgobgp.so *.go
-cd ${GOBGP_PATH}/tools/grpc/cpp
-ln -sf ${GOBGP_PATH}/cmd/gobgp/lib/libgobgp.h
-ln -sf ${GOBGP_PATH}/cmd/gobgp/lib/libgobgp.so
-ln -sf ${GOBGP_PATH}/api/gobgp.proto gobgp_api_client.proto
-make
diff --git a/tools/grpc/cpp/gobgp_api_client.cc b/tools/grpc/cpp/gobgp_api_client.cc
deleted file mode 100644
index b704c932..00000000
--- a/tools/grpc/cpp/gobgp_api_client.cc
+++ /dev/null
@@ -1,86 +0,0 @@
-#include <iostream>
-#include <memory>
-#include <sstream>
-#include <string>
-#include <string.h>
-
-#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"
-
-extern "C" {
- // Gobgp library
- #include "libgobgp.h"
-}
-
-using grpc::Channel;
-using grpc::ClientContext;
-using grpc::Status;
-
-using gobgpapi::GobgpApi;
-
-class GrpcClient {
- public:
- GrpcClient(std::shared_ptr<Channel> channel) : stub_(GobgpApi::NewStub(channel)) {}
-
- std::string GetNeighbor() {
- gobgpapi::GetNeighborRequest request;
-
- ClientContext context;
-
- gobgpapi::GetNeighborResponse response;
- grpc::Status status = stub_->GetNeighbor(&context, request, &response);
-
- if (status.ok()) {
- std::stringstream buffer;
- for (int i=0; i < response.peers_size(); i++) {
-
- gobgpapi::PeerConf peer_conf = response.peers(i).conf();
- gobgpapi::PeerState peer_info = response.peers(i).info();
- gobgpapi::Timers peer_timers = response.peers(i).timers();
-
- buffer
- << "BGP neighbor is: " << peer_conf.neighbor_address()
- << ", remote AS: " << peer_conf.peer_as() << "\n"
- << "\tBGP version: 4, remote route ID " << peer_conf.id() << "\n"
- << "\tBGP state = " << peer_info.bgp_state()
- << ", up for " << peer_timers.state().uptime() << "\n"
- << "\tBGP OutQ = " << peer_info.out_q()
- << ", Flops = " << peer_info.flops() << "\n"
- << "\tHold time is " << peer_timers.state().hold_time()
- << ", keepalive interval is " << peer_timers.state().keepalive_interval() << "seconds\n"
- << "\tConfigured hold time is " << peer_timers.config().hold_time() << "\n";
-
- }
- return buffer.str();
- } else {
- std::stringstream buffer;
- buffer
- << status.error_code() << "\n"
- << status.error_message() << "\n"
- << status.error_details() << "\n";
- return buffer.str();
- }
- }
-
- private:
- std::unique_ptr<GobgpApi::Stub> stub_;
-};
-
-int main(int argc, char** argv) {
- if(argc < 2) {
- std::cout << "Usage: ./gobgp_api_client [gobgp address]\n";
- return 1;
- }
-
- std::string addr = argv[1];
- GrpcClient gobgp_client(grpc::CreateChannel(addr + ":50051", grpc::InsecureChannelCredentials()));
-
- std::string reply = gobgp_client.GetNeighbor();
- std::cout << reply;
-
- return 0;
-}