7 #include <grpcpp/grpcpp.h>
8 #include <grpcpp/server.h>
9 #include <grpcpp/server_builder.h>
10 #include <grpcpp/server_context.h>
12 #include <grpcpp/ext/proto_server_reflection_plugin.h>
13 #include <grpcpp/health_check_service_interface.h>
15 #include "agl_shell.grpc.pb.h"
16 #include "grpc-async.h"
19 CallData::Proceed(void)
23 // Make this instance progress to the PROCESS state.
25 std::cout << "Creating Call data for new client connections: "
28 // As part of the initial CREATE state, we *request* that the
29 // system start processing AppStatusState requests.
31 // In this request, "this" acts are the tag uniquely
32 // identifying the request (so that different CallData
33 // instances can serve different requests concurrently), in
34 // this case the memory address of this CallData instance.
35 m_service->RequestAppStatusState(&m_ctx, &m_request, &m_responder,
36 m_cq, m_cq, (void *) this);
39 // Spawn a new CallData instance to serve new clients while we
40 // process the one for this CallData. The instance will
41 // deallocate itself as part of its FINISH state.
42 CallData *cd = new CallData(m_service, m_cq);
44 // The actual processing.
45 m_status = PROCESSING;
49 if (m_repliesSent == MAX_REPLIES) {
50 // And we are done! Let the gRPC runtime know we've
51 // finished, using the memory address of this instance
52 // as the uniquely identifying tag for the event.
54 m_responder.Finish(Status::OK, this);
56 // The actual processing.
57 m_status = PROCESSING;
62 GPR_ASSERT(m_status == FINISH);
63 std::cout << "Completed RPC for: " << this << std::endl;
64 // Once in the FINISH state, deallocate ourselves (CallData).
72 GrpcServiceImpl::~GrpcServiceImpl()
75 // Always shutdown the completion queue after the server.
80 GrpcServiceImpl::Run(void)
82 std::string server_address(kDefaultGrpcServiceAddress);
84 grpc::ServerBuilder builder;
85 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
87 builder.RegisterService(&m_service);
88 m_cq = builder.AddCompletionQueue();
90 m_server = builder.BuildAndStart();
91 std::cout << "Server listening on " << server_address << std::endl;
93 // Proceed to the server's main loop.
98 GrpcServiceImpl::HandleRpcs(void)
100 // Spawn a new CallData instance to serve new clients.
101 CallData *cd = new CallData(&m_service, m_cq.get());
103 // uniquely identifies a request.
107 // Block waiting to read the next event from the completion queue. The
108 // event is uniquely identified by its tag, which in this case is the
109 // memory address of a CallData instance.
111 // The return value of Next should always be checked. This return value
112 // tells us whether there is any kind of event or cq_ is shutting down.
114 std::cout << "Blocked on next waiting for events" << std::endl;
115 GPR_ASSERT(m_cq->Next(&tag, &ok));
118 std::cout << "Calling tag " << tag << " with Proceed()" << std::endl;
119 static_cast<CallData*>(tag)->Proceed();