-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_server.cc
134 lines (121 loc) · 4.07 KB
/
test_server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// gRPC server for testing.
#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include "test.grpc.pb.h"
#include "homa.h"
#include "homa_listener.h"
#include "time_trace.h"
#include "util.h"
class TestImpl : public test::Test::Service {
public:
grpc::Status Sum(grpc::ServerContext* context, const test::SumArgs *args,
test::SumResult *status) override
{
// printf("Sum invoked with arguments %d and %d\n",
// args->op1(), args->op2());
tt("Sum service method invoked");
// printf("%lu metadata values from client\n",
// context->client_metadata().size());
// for (auto md: context->client_metadata()) {
// printf("Incoming metadata: name '%.*s', value '%.*s'\n",
// static_cast<int>(md.first.length()), md.first.data(),
// static_cast<int>(md.second.length()), md.second.data());
// }
status->set_sum(args->op1() + args->op2());
return grpc::Status::OK;
}
grpc::Status SumMany(grpc::ServerContext* context,
grpc::ServerReader<test::Value>* reader, test::SumResult *status)
override
{
test::Value value;
int sum = 0;
while (reader->Read(&value)) {
printf("SumMany received value %d\n", value.value());
sum += value.value();
}
status->set_sum(sum);
printf("Returning result: %d\n", sum);
return grpc::Status::OK;
}
grpc::Status GetValues(grpc::ServerContext* context, const test::Value *arg,
grpc::ServerWriter<test::Value>* writer) override
{
test::Value response;
for (int i = 1; i <= arg->value(); i += 2) {
response.set_value(i);
printf("Writing response with %d\n", i);
writer->Write(response);
}
printf("GetValues finished (input %d)\n", arg->value());
return grpc::Status::OK;
}
grpc::Status IncMany(grpc::ServerContext* context,
grpc::ServerReaderWriter<test::Value, test::Value>* stream) override
{
test::Value request;
test::Value response;
while (stream->Read(&request)) {
printf("IncMany got value %d\n", request.value());
response.set_value(request.value() + 1);
stream->Write(response);
}
printf("IncMany finished\n");
return grpc::Status::OK;
}
grpc::Status PrintTrace(grpc::ServerContext*context,
const test::String *args, test::Empty *status) override
{
TimeTrace::printToFile(args->s().c_str());
return grpc::Status::OK;
}
};
int main(int argc, char** argv) {
recordFunc = TimeTrace::record2;
std::string serverAddress;
bool useHoma = true;
std::vector<std::string> args;
unsigned nextArg;
for (int i = 0; i < argc; i++) {
args.emplace_back(argv[i]);
}
for (nextArg = 1; nextArg < args.size(); nextArg++) {
const char *option = args[nextArg].c_str();
if (strcmp(option, "--tcp") == 0) {
useHoma = false;
continue;
}
break;
}
if (nextArg != args.size()) {
if (nextArg != (args.size() - 1)) {
fprintf(stderr, "Usage: test_server [--tcp] [homa:port]\n");
exit(1);
}
serverAddress = args.back();
}
if (serverAddress.empty()) {
if (useHoma) {
serverAddress = "homa:4000";
} else {
serverAddress = "0.0.0.0:4000";
}
}
TestImpl service;
grpc::ServerBuilder builder;
int actualPort;
if (useHoma) {
builder.AddListeningPort(serverAddress,
HomaListener::insecureCredentials(), &actualPort);
} else {
builder.AddListeningPort(serverAddress,
grpc::InsecureServerCredentials(), &actualPort);
}
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
if (server == nullptr)
exit(1);
std::cout << "Server listening on port " << actualPort << std::endl;
server->Wait();
return 0;
}