From b37b6bb1c40d9955f7e49921b38ca3e19b8aabed Mon Sep 17 00:00:00 2001 From: Pavel Odintsov Date: Thu, 15 Oct 2015 19:09:51 +0200 Subject: [PATCH] Less verbode API server and client. Add timeout for client. --- src/api_client.cpp | 92 +++++++++++++++++++++++++++------------------- src/api_server.cpp | 7 ++++ 2 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/api_client.cpp b/src/api_client.cpp index 5464c3f..a674bde 100644 --- a/src/api_client.cpp +++ b/src/api_client.cpp @@ -13,50 +13,68 @@ using fastmitigation::BanListRequest; using fastmitigation::BanListReply; using fastmitigation::Fastnetmon; +unsigned int client_connection_timeout = 5; + class GreeterClient { - public: - GreeterClient(std::shared_ptr channel) - : stub_(Fastnetmon::NewStub(channel)) {} + public: + GreeterClient(std::shared_ptr channel) : stub_(Fastnetmon::NewStub(channel)) {} - // Assambles the client's payload, sends it and presents the response back - // from the server. - std::string GetBanList(const std::string& user) { - // Data we are sending to the server. - BanListRequest request; - request.set_name(user); + // Assambles the client's payload, sends it and presents the response back + // from the server. + void GetBanList(const std::string& user) { + // Data we are sending to the server. + BanListRequest request; + request.set_name(user); - // Container for the data we expect from the server. - BanListReply reply; + // Container for the data we expect from the server. + BanListReply reply; - // Context for the client. It could be used to convey extra information to - // the server and/or tweak certain RPC behaviors. - ClientContext context; + // Context for the client. It could be used to convey extra information to + // the server and/or tweak certain RPC behaviors. + ClientContext context; - // The actual RPC. - Status status = stub_->GetBanlist(&context, request, &reply); + std::chrono::system_clock::time_point deadline = + std::chrono::system_clock::now() + std::chrono::seconds(client_connection_timeout); - // Act upon its status. - if (status.ok()) { - return reply.message(); - } else { - return "RPC failed"; - } - } + context.set_deadline(deadline); - private: - std::unique_ptr stub_; + // The actual RPC. + Status status = stub_->GetBanlist(&context, request, &reply); + + // Act upon its status. + if (status.ok()) { + std::cout << "Server answer: " << reply.message() << std::endl; + } else { + if (status.error_code() == grpc::DEADLINE_EXCEEDED) { + std::cerr << "Could not connect to API server. Timeout exceed" << std::endl; + return; + } else { + std::cerr << "RPC failed " + status.error_message(); + return; + } + } + } + + private: + std::unique_ptr stub_; }; -int main(int argc, char** argv) { - // Instantiate the client. It requires a channel, out of which the actual RPCs - // are created. This channel models a connection to an endpoint (in this case, - // localhost at port 50051). We indicate that the channel isn't authenticated - // (use of InsecureCredentials()). - GreeterClient greeter( - grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials())); - std::string user("Paul"); - std::string reply = greeter.GetBanList(user); - std::cout << "Greeter received: " << reply << std::endl; - - return 0; +void silent_logging_function(gpr_log_func_args *args) { + // We do not want any logging here +} + +int main(int argc, char** argv) { + gpr_set_log_function(silent_logging_function); + + // Instantiate the client. It requires a channel, out of which the actual RPCs + // are created. This channel models a connection to an endpoint (in this case, + // localhost at port 50051). We indicate that the channel isn't authenticated + // (use of InsecureCredentials()). + GreeterClient greeter( grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials())); + + std::cout << "Sending request\n" << std::endl; + std::string user("Paul"); + greeter.GetBanList(user); + + return 0; } diff --git a/src/api_server.cpp b/src/api_server.cpp index d4a3d2e..63bbb2f 100644 --- a/src/api_server.cpp +++ b/src/api_server.cpp @@ -17,6 +17,7 @@ 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::cout << "Incoming request" << std::endl; std::string prefix("Hello "); reply->set_message(prefix + request->name()); return Status::OK; @@ -42,7 +43,13 @@ void RunServer() { server->Wait(); } +void silent_logging_function(gpr_log_func_args *args) { + // We do not want any logging here +} + int main(int argc, char** argv) { + gpr_set_log_function(silent_logging_function); + RunServer(); return 0;