1
0
mirror of https://github.com/pavel-odintsov/fastnetmon synced 2024-09-22 05:42:35 +02:00

Less verbode API server and client. Add timeout for client.

This commit is contained in:
Pavel Odintsov 2015-10-15 19:09:51 +02:00
parent d9d2a3b918
commit b37b6bb1c4
2 changed files with 62 additions and 37 deletions

View File

@ -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> channel)
: stub_(Fastnetmon::NewStub(channel)) {}
public:
GreeterClient(std::shared_ptr<Channel> 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<Fastnetmon::Stub> 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<Fastnetmon::Stub> 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;
}

View File

@ -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;