mirror of
https://github.com/pavel-odintsov/fastnetmon
synced 2024-06-01 22:16:27 +02:00
Add API implementation examples in gRPC
This commit is contained in:
parent
48189da3c5
commit
bcdf5278fd
|
@ -310,6 +310,31 @@ if (ENABLE_GOBGP_SUPPORT)
|
|||
|
||||
target_link_libraries(gobgp_action gobgp_api_client_pb_cc)
|
||||
target_link_libraries(gobgp_action gobgp_api_client_grpc_pb_cc)
|
||||
|
||||
# FastNetMon API
|
||||
execute_process(COMMAND ${PROTOC_BINARY} -I ${PROJECT_BINARY_DIR}/.. --grpc_out=${PROJECT_BINARY_DIR}/.. --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${PROJECT_BINARY_DIR}/../fastnetmon.proto ERROR_VARIABLE PROTOC_STDERR RESULT_VARIABLE PROTOC_RETURN_CODE OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
execute_process(COMMAND ${PROTOC_BINARY} -I ${PROJECT_BINARY_DIR}/.. --cpp_out=${PROJECT_BINARY_DIR}/.. ${PROJECT_BINARY_DIR}/../fastnetmon.proto ERROR_VARIABLE PROTOC_STDERR RESULT_VARIABLE PROTOC_RETURN_CODE OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
add_library(fastnetmon_grpc_pb_cc STATIC fastnetmon.grpc.pb.cc)
|
||||
add_library(fastnetmon_pb_cc STATIC fastnetmon.pb.cc)
|
||||
|
||||
add_executable(api_server api_server.cpp)
|
||||
add_executable(api_client api_client.cpp)
|
||||
|
||||
target_link_libraries(api_client ${GRPC_LIBRARY_GPR_PATH})
|
||||
target_link_libraries(api_client ${GRPC_LIBRARY_GRPC_CPP_UNSECURE_PATH})
|
||||
target_link_libraries(api_client ${GRPC_LIBRARY_GRPC_PATH})
|
||||
target_link_libraries(api_client fastnetmon_grpc_pb_cc)
|
||||
target_link_libraries(api_client fastnetmon_pb_cc)
|
||||
target_link_libraries(api_client ${PROTOCOL_BUFFERS_LIBRARY_PATH})
|
||||
|
||||
target_link_libraries(api_server ${GRPC_LIBRARY_GPR_PATH})
|
||||
target_link_libraries(api_server ${GRPC_LIBRARY_GRPC_CPP_UNSECURE_PATH})
|
||||
target_link_libraries(api_server ${GRPC_LIBRARY_GRPC_PATH})
|
||||
target_link_libraries(api_server fastnetmon_grpc_pb_cc)
|
||||
target_link_libraries(api_server fastnetmon_pb_cc)
|
||||
target_link_libraries(api_server ${PROTOCOL_BUFFERS_LIBRARY_PATH})
|
||||
endif()
|
||||
|
||||
# example plugin
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <grpc++/grpc++.h>
|
||||
|
||||
#include "fastnetmon.grpc.pb.h"
|
||||
|
||||
using grpc::Server;
|
||||
using grpc::ServerBuilder;
|
||||
using grpc::ServerContext;
|
||||
using grpc::Status;
|
||||
using fastmitigation::BanListRequest;
|
||||
using fastmitigation::BanListReply;
|
||||
using fastmitigation::Fastnetmon;
|
||||
|
||||
// Logic and data behind the server's behavior.
|
||||
class GreeterServiceImpl final : public Fastnetmon::Service {
|
||||
Status GetBanlist(ServerContext* context, const BanListRequest* request, BanListReply* reply) override {
|
||||
std::string prefix("Hello ");
|
||||
reply->set_message(prefix + request->name());
|
||||
return Status::OK;
|
||||
}
|
||||
};
|
||||
|
||||
void RunServer() {
|
||||
std::string server_address("0.0.0.0:50051");
|
||||
GreeterServiceImpl service;
|
||||
|
||||
ServerBuilder builder;
|
||||
// Listen on the given address without any authentication mechanism.
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
||||
// Register "service" as the instance through which we'll communicate with
|
||||
// clients. In this case it corresponds to an *synchronous* service.
|
||||
builder.RegisterService(&service);
|
||||
// Finally assemble the server.
|
||||
std::unique_ptr<Server> server(builder.BuildAndStart());
|
||||
std::cout << "Server listening on " << server_address << std::endl;
|
||||
|
||||
// Wait for the server to shutdown. Note that some other thread must be
|
||||
// responsible for shutting down the server for this call to ever return.
|
||||
server->Wait();
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
RunServer();
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package fastmitigation;
|
||||
|
||||
service Fastnetmon {
|
||||
rpc GetBanlist (BanListRequest) returns (BanListReply) {}
|
||||
}
|
||||
|
||||
message BanListRequest {
|
||||
string name = 1;
|
||||
}
|
||||
|
||||
message BanListReply {
|
||||
string message = 1;
|
||||
}
|
Loading…
Reference in New Issue