| 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 |
|
| 25 |
std::string ToString(const grpc::string_ref& r) { |
| 26 |
return std::string(r.data(), r.size()); |
| 27 |
} |
| 28 |
|
| 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 |
|
| 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 |
|
| 49 |
ServerUnaryReactor* CallbackStreamingTestService::Echo( |
| 50 |
CallbackServerContext* context, const EchoRequest* /*request*/, |
| 51 |
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 |
ServerBidiReactor<EchoRequest, EchoResponse>* |
| 65 |
CallbackStreamingTestService::BidiStream(CallbackServerContext* context) { |
| 66 |
class Reactor : public ServerBidiReactor<EchoRequest, EchoResponse> { |
| 67 |
public: |
| 68 |
explicit Reactor(CallbackServerContext* context) { |
| 69 |
message_size_ = GetIntValueFromMetadata(kServerMessageSize, |
| 70 |
context->client_metadata(), 0); |
| 71 |
StartRead(&request_); |
| 72 |
} |
| 73 |
void OnDone() override { |
| 74 |
GPR_ASSERT(finished_); |
| 75 |
delete this; |
| 76 |
} |
| 77 |
void OnCancel() override {} |
| 78 |
void OnReadDone(bool ok) override { |
| 79 |
if (!ok) { |
| 80 |
// Stream is over |
| 81 |
Finish(grpc::Status::OK); |
| 82 |
finished_ = true; |
| 83 |
return; |
| 84 |
} |
| 85 |
if (message_size_ > 0) { |
| 86 |
response_.set_message(std::string(message_size_, 'a')); |
| 87 |
} else { |
| 88 |
response_.set_message(""); |
| 89 |
} |
| 90 |
StartWrite(&response_); |
| 91 |
} |
| 92 |
void OnWriteDone(bool ok) override { |
| 93 |
if (!ok) { |
| 94 |
gpr_log(GPR_ERROR, "Server write failed"); |
| 95 |
return; |
| 96 |
} |
| 97 |
StartRead(&request_); |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
EchoRequest request_; |
| 102 |
EchoResponse response_; |
| 103 |
int message_size_; |
| 104 |
bool finished_{false}; |
| 105 |
}; |
| 106 |
|
| 107 |
return new Reactor(context); |
| 108 |
} |
| 109 |
} // namespace testing |
| 110 |
} // namespace grpc |