1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <vector>
19
20 #include "absl/flags/flag.h"
21 #include "absl/flags/parse.h"
22 #include "absl/strings/str_format.h"
23
24 #include <grpcpp/grpcpp.h>
25
26 #ifdef BAZEL_BUILD
27 #include "examples/protos/helloworld.grpc.pb.h"
28 #else
29 #include "helloworld.grpc.pb.h"
30 #endif
31
32 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
33
34 using grpc::CallbackServerContext;
35 using grpc::Server;
36 using grpc::ServerBidiReactor;
37 using grpc::ServerBuilder;
38 using grpc::Status;
39 using helloworld::Greeter;
40 using helloworld::HelloReply;
41 using helloworld::HelloRequest;
42
43 // Logic behind the server's behavior.
44 class KeyValueStoreServiceImpl final : public Greeter::CallbackService {
SayHelloBidiStream(CallbackServerContext * context)45 ServerBidiReactor<HelloRequest, HelloReply>* SayHelloBidiStream(
46 CallbackServerContext* context) override {
47 class Reactor : public ServerBidiReactor<HelloRequest, HelloReply> {
48 public:
49 explicit Reactor() { StartRead(&request_); }
50
51 void OnReadDone(bool ok) override {
52 if (!ok) {
53 // Client cancelled it
54 std::cout << "OnReadDone Cancelled!" << std::endl;
55 return Finish(grpc::Status::CANCELLED);
56 }
57 response_.set_message(absl::StrCat(request_.name(), " Ack"));
58 StartWrite(&response_);
59 }
60
61 void OnWriteDone(bool ok) override {
62 if (!ok) {
63 // Client cancelled it
64 std::cout << "OnWriteDone Cancelled!" << std::endl;
65 return Finish(grpc::Status::CANCELLED);
66 }
67 StartRead(&request_);
68 }
69
70 void OnDone() override { delete this; }
71
72 private:
73 HelloRequest request_;
74 HelloReply response_;
75 };
76
77 return new Reactor();
78 }
79 };
80
RunServer(uint16_t port)81 void RunServer(uint16_t port) {
82 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
83 KeyValueStoreServiceImpl service;
84
85 ServerBuilder builder;
86 // Listen on the given address without any authentication mechanism.
87 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
88 // Register "service" as the instance through which we'll communicate with
89 // clients. In this case it corresponds to an *synchronous* service.
90 builder.RegisterService(&service);
91 // Finally assemble the server.
92 std::unique_ptr<Server> server(builder.BuildAndStart());
93 std::cout << "Server listening on " << server_address << std::endl;
94
95 // Wait for the server to shutdown. Note that some other thread must be
96 // responsible for shutting down the server for this call to ever return.
97 server->Wait();
98 }
99
main(int argc,char ** argv)100 int main(int argc, char** argv) {
101 absl::ParseCommandLine(argc, argv);
102 RunServer(absl::GetFlag(FLAGS_port));
103 return 0;
104 }
105