-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhal-grpc-server.cc
More file actions
104 lines (88 loc) · 2.93 KB
/
Copy pathhal-grpc-server.cc
File metadata and controls
104 lines (88 loc) · 2.93 KB
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
#include <grpcpp/grpcpp.h>
#include <grpc/support/log.h>
#include "halBlockViz.grpc.pb.h"
using namespace hal;
using grpc::Server;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerCompletionQueue;
using grpc::Status;
class BlockVizServer final {
public:
~BlockVizServer() {
_server->Shutdown();
_cq->Shutdown();
}
void run() {
std::string server_address("0.0.0.0:50051");
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 *asynchronous* service.
builder.RegisterService(&_service);
// Get hold of the completion queue used for the asynchronous communication
// with the gRPC runtime.
_cq = builder.AddCompletionQueue();
// Finally assemble the server.
_server = builder.BuildAndStart();
std::cout << "Server listening on " << server_address << std::endl;
// Proceed to the server's main loop.
HandleRpcs();
}
private:
class CallData {
public:
CallData(BlockViz::AsyncService* service, ServerCompletionQueue* cq)
: _service(service), _cq(cq), _responder(&_ctx), _status(CREATE) {
Proceed();
}
void Proceed() {
if (_status == CREATE) {
_status = PROCESS;
} else if (_status == PROCESS) {
new CallData(_service, _cq);
_status = FINISH;
_responder.Finish(_reply, Status::OK, this);
} else {
GPR_ASSERT(_status == FINISH);
delete this;
}
}
private:
BlockViz::AsyncService* _service;
ServerCompletionQueue* _cq;
ServerContext _ctx;
BlockRequest _request;
BlockResults _reply;
ServerAsyncResponseWriter<BlockResults> _responder;
enum CallStatus { CREATE, PROCESS, FINISH };
CallStatus _status; // The current serving state.
};
// This can be run in multiple threads if needed.
void HandleRpcs() {
// Spawn a new CallData instance to serve new clients.
new CallData(&_service, _cq.get());
void* tag; // uniquely identifies a request.
bool ok;
while (true) {
// Block waiting to read the next event from the completion queue. The
// event is uniquely identified by its tag, which in this case is the
// memory address of a CallData instance.
// The return value of Next should always be checked. This return value
// tells us whether there is any kind of event or _cq is shutting down.
GPR_ASSERT(_cq->Next(&tag, &ok));
GPR_ASSERT(ok);
static_cast<CallData*>(tag)->Proceed();
}
}
std::unique_ptr<ServerCompletionQueue> _cq;
BlockViz::AsyncService _service;
std::unique_ptr<Server> _server;
};
int main(int argc, char* argv[]) {
BlockVizServer server;
server.run();
return 0;
}