-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcp_test.cc
91 lines (81 loc) · 2.36 KB
/
tcp_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This file contains both the client and server code for a simple
// gRPC application.
//
// Usage:
// tcp_test server port
// tcp test client port@host
#include <string>
#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include "test.grpc.pb.h"
// Server-side code to implement the service.
class SumService : public test::Test::Service {
public:
grpc::Status Sum(grpc::ServerContext*context, const test::SumArgs *args,
test::SumResult *result) {
result->set_sum(args->op1() + args->op2());
return grpc::Status::OK;
}
};
// Client-side wrapper class.
class SumClient{
public:
SumClient(const std::shared_ptr<grpc::Channel> channel)
: stub(test::Test::NewStub(channel)) {}
std::unique_ptr<test::Test::Stub> stub;
int Sum(int op1, int op2)
{
test::SumArgs args;
test::SumResult result;
grpc::ClientContext context;
args.set_op1(op1);
args.set_op2(op2);
grpc::Status status = stub->Sum(&context, args, &result);
if (!status.ok()) {
printf("Sum RPC failed!\n");
return -1;
}
return result.sum();
}
};
int main(int argc, char** argv) {
if (argc < 2) {
goto usage;
}
if (strcmp(argv[1], "client") == 0) {
std::string target = "node1:4000";
if (argc == 3) {
target = argv[2];
} else if (argc != 2) {
goto usage;
}
SumClient client(grpc::CreateChannel(target,
grpc::InsecureChannelCredentials()));
int sum = client.Sum(22, 33);
printf("Sum of 22 and 33 is %d\n", sum);
return 0;
} else if (strcmp(argv[1], "server") == 0) {
std::string port = "0.0.0.0:4000";
if (argc == 3) {
port = "0.0.0.0:";
port += argv[2];
} else if (argc != 2) {
goto usage;
}
SumService service;
grpc::ServerBuilder builder;
builder.AddListeningPort(port, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
if (server == nullptr)
exit(1);
server->Wait();
} else {
goto usage;
}
exit(0);
usage:
fprintf(stderr, "Usage: %s client server:port or %s server port\n",
argv[0], argv[0]);
exit(1);
}