1 /*
2 *
3 * Copyright 2019 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 "test/cpp/microbenchmarks/callback_test_service.h"
20
21 namespace grpc {
22 namespace testing {
23 namespace {
24
ToString(const grpc::string_ref & r)25 std::string ToString(const grpc::string_ref& r) {
26 return std::string(r.data(), r.size());
27 }
28
GetIntValueFromMetadataHelper(const char * key,const std::multimap<grpc::string_ref,grpc::string_ref> & metadata,int default_value)29 int GetIntValueFromMetadataHelper(
30 const char* key,
31 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
32 int default_value) {
33 if (metadata.find(key) != metadata.end()) {
34 std::istringstream iss(ToString(metadata.find(key)->second));
35 iss >> default_value;
36 }
37
38 return default_value;
39 }
40
GetIntValueFromMetadata(const char * key,const std::multimap<grpc::string_ref,grpc::string_ref> & metadata,int default_value)41 int GetIntValueFromMetadata(
42 const char* key,
43 const std::multimap<grpc::string_ref, grpc::string_ref>& metadata,
44 int default_value) {
45 return GetIntValueFromMetadataHelper(key, metadata, default_value);
46 }
47 } // namespace
48
Echo(experimental::CallbackServerContext * context,const EchoRequest *,EchoResponse * response)49 experimental::ServerUnaryReactor* CallbackStreamingTestService::Echo(
50 experimental::CallbackServerContext* context,
51 const EchoRequest* /*request*/, EchoResponse* response) {
52 int response_msgs_size = GetIntValueFromMetadata(
53 kServerMessageSize, context->client_metadata(), 0);
54 if (response_msgs_size > 0) {
55 response->set_message(std::string(response_msgs_size, 'a'));
56 } else {
57 response->set_message("");
58 }
59 auto* reactor = context->DefaultReactor();
60 reactor->Finish(::grpc::Status::OK);
61 return reactor;
62 }
63
64 experimental::ServerBidiReactor<EchoRequest, EchoResponse>*
BidiStream(experimental::CallbackServerContext * context)65 CallbackStreamingTestService::BidiStream(
66 experimental::CallbackServerContext* context) {
67 class Reactor
68 : public experimental::ServerBidiReactor<EchoRequest, EchoResponse> {
69 public:
70 explicit Reactor(experimental::CallbackServerContext* context) {
71 message_size_ = GetIntValueFromMetadata(kServerMessageSize,
72 context->client_metadata(), 0);
73 StartRead(&request_);
74 }
75 void OnDone() override {
76 GPR_ASSERT(finished_);
77 delete this;
78 }
79 void OnCancel() override {}
80 void OnReadDone(bool ok) override {
81 if (!ok) {
82 // Stream is over
83 Finish(::grpc::Status::OK);
84 finished_ = true;
85 return;
86 }
87 if (message_size_ > 0) {
88 response_.set_message(std::string(message_size_, 'a'));
89 } else {
90 response_.set_message("");
91 }
92 StartWrite(&response_);
93 }
94 void OnWriteDone(bool ok) override {
95 if (!ok) {
96 gpr_log(GPR_ERROR, "Server write failed");
97 return;
98 }
99 StartRead(&request_);
100 }
101
102 private:
103 EchoRequest request_;
104 EchoResponse response_;
105 int message_size_;
106 bool finished_{false};
107 };
108
109 return new Reactor(context);
110 }
111 } // namespace testing
112 } // namespace grpc
113