1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include <grpcpp/grpcpp.h>
24
25 #ifdef BAZEL_BUILD
26 #include "examples/protos/helloworld.grpc.pb.h"
27 #else
28 #include "helloworld.grpc.pb.h"
29 #endif
30
31 using grpc::Server;
32 using grpc::ServerBuilder;
33 using grpc::ServerContext;
34 using grpc::Status;
35 using helloworld::HelloRequest;
36 using helloworld::HelloReply;
37 using helloworld::Greeter;
38
39 // Logic and data behind the server's behavior.
40 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)41 Status SayHello(ServerContext* context, const HelloRequest* request,
42 HelloReply* reply) override {
43 std::string prefix("Hello ");
44
45 // Get the client's initial metadata
46 std::cout << "Client metadata: " << std::endl;
47 const std::multimap<grpc::string_ref, grpc::string_ref> metadata = context->client_metadata();
48 for (auto iter = metadata.begin(); iter != metadata.end(); ++iter) {
49 std::cout << "Header key: " << iter->first << ", value: ";
50 // Check for binary value
51 size_t isbin = iter->first.find("-bin");
52 if ((isbin != std::string::npos) && (isbin + 4 == iter->first.size())) {
53 std::cout << std::hex;
54 for (auto c : iter->second) {
55 std::cout << static_cast<unsigned int>(c);
56 }
57 std::cout << std::dec;
58 } else {
59 std::cout << iter->second;
60 }
61 std::cout << std::endl;
62 }
63
64 context->AddInitialMetadata("custom-server-metadata", "initial metadata value");
65 context->AddTrailingMetadata("custom-trailing-metadata", "trailing metadata value");
66 reply->set_message(prefix + request->name());
67 return Status::OK;
68 }
69 };
70
RunServer()71 void RunServer() {
72 std::string server_address("0.0.0.0:50051");
73 GreeterServiceImpl service;
74
75 ServerBuilder builder;
76 // Listen on the given address without any authentication mechanism.
77 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
78 // Register "service" as the instance through which we'll communicate with
79 // clients. In this case it corresponds to an *synchronous* service.
80 builder.RegisterService(&service);
81 // Finally assemble the server.
82 std::unique_ptr<Server> server(builder.BuildAndStart());
83 std::cout << "Server listening on " << server_address << std::endl;
84
85 // Wait for the server to shutdown. Note that some other thread must be
86 // responsible for shutting down the server for this call to ever return.
87 server->Wait();
88 }
89
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91 RunServer();
92
93 return 0;
94 }
95