yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 36 | #include <fstream> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 37 | #include <memory> |
| 38 | #include <sstream> |
| 39 | #include <thread> |
| 40 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 41 | #include <gflags/gflags.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 42 | #include <grpc++/security/server_credentials.h> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 43 | #include <grpc++/server.h> |
| 44 | #include <grpc++/server_builder.h> |
| 45 | #include <grpc++/server_context.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 46 | #include <grpc/grpc.h> |
| 47 | #include <grpc/support/log.h> |
| 48 | #include <grpc/support/useful.h> |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 49 | |
Makarand Dharmapurikar | 1ed0b8e | 2016-09-14 15:01:16 -0700 | [diff] [blame] | 50 | #include "src/core/lib/support/string.h" |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 51 | #include "src/core/lib/transport/byte_stream.h" |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 52 | #include "src/proto/grpc/testing/empty.grpc.pb.h" |
| 53 | #include "src/proto/grpc/testing/messages.grpc.pb.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 54 | #include "src/proto/grpc/testing/test.grpc.pb.h" |
| 55 | #include "test/cpp/interop/server_helper.h" |
| 56 | #include "test/cpp/util/test_config.h" |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 57 | |
yang-g | 035cf09 | 2015-09-18 12:11:05 -0700 | [diff] [blame] | 58 | DEFINE_bool(use_tls, false, "Whether to use tls."); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 59 | DEFINE_int32(port, 0, "Server port."); |
Mark D. Roth | c99fd89 | 2016-09-06 08:16:13 -0700 | [diff] [blame] | 60 | DEFINE_int32(max_send_message_size, -1, "The maximum send message size."); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 61 | |
| 62 | using grpc::Server; |
| 63 | using grpc::ServerBuilder; |
| 64 | using grpc::ServerContext; |
| 65 | using grpc::ServerCredentials; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 66 | using grpc::ServerReader; |
| 67 | using grpc::ServerReaderWriter; |
| 68 | using grpc::ServerWriter; |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 69 | using grpc::WriteOptions; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 70 | using grpc::SslServerCredentialsOptions; |
David Garcia Quintas | 7c0d914 | 2015-07-23 04:58:20 -0700 | [diff] [blame] | 71 | using grpc::testing::InteropServerContextInspector; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 72 | using grpc::testing::Payload; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 73 | using grpc::testing::SimpleRequest; |
| 74 | using grpc::testing::SimpleResponse; |
| 75 | using grpc::testing::StreamingInputCallRequest; |
| 76 | using grpc::testing::StreamingInputCallResponse; |
| 77 | using grpc::testing::StreamingOutputCallRequest; |
| 78 | using grpc::testing::StreamingOutputCallResponse; |
| 79 | using grpc::testing::TestService; |
| 80 | using grpc::Status; |
| 81 | |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 82 | const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial"; |
| 83 | const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin"; |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 84 | const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent"; |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 85 | |
| 86 | void MaybeEchoMetadata(ServerContext* context) { |
| 87 | const auto& client_metadata = context->client_metadata(); |
| 88 | GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1); |
| 89 | GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1); |
| 90 | |
| 91 | auto iter = client_metadata.find(kEchoInitialMetadataKey); |
| 92 | if (iter != client_metadata.end()) { |
| 93 | context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data()); |
| 94 | } |
| 95 | iter = client_metadata.find(kEchoTrailingBinMetadataKey); |
| 96 | if (iter != client_metadata.end()) { |
| 97 | context->AddTrailingMetadata( |
| 98 | kEchoTrailingBinMetadataKey, |
| 99 | grpc::string(iter->second.begin(), iter->second.end())); |
| 100 | } |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 101 | // Check if client sent a magic key in the header that makes us echo |
| 102 | // back the user-agent (for testing purpose) |
| 103 | iter = client_metadata.find(kEchoUserAgentKey); |
| 104 | if (iter != client_metadata.end()) { |
| 105 | iter = client_metadata.find("user-agent"); |
| 106 | if (iter != client_metadata.end()) { |
| 107 | context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data()); |
| 108 | } |
| 109 | } |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 110 | } |
| 111 | |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 112 | bool SetPayload(int size, Payload* payload) { |
| 113 | std::unique_ptr<char[]> body(new char[size]()); |
| 114 | payload->set_body(body.get(), size); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 118 | bool CheckExpectedCompression(const ServerContext& context, |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 119 | const bool compression_expected) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 120 | const InteropServerContextInspector inspector(context); |
| 121 | const grpc_compression_algorithm received_compression = |
| 122 | inspector.GetCallCompressionAlgorithm(); |
| 123 | |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 124 | if (compression_expected) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 125 | if (received_compression == GRPC_COMPRESS_NONE) { |
| 126 | // Expected some compression, got NONE. This is an error. |
| 127 | gpr_log(GPR_ERROR, |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 128 | "Expected compression but got uncompressed request from client."); |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 129 | return false; |
| 130 | } |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 131 | if (!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS)) { |
| 132 | gpr_log(GPR_ERROR, |
| 133 | "Failure: Requested compression in a compressable request, but " |
| 134 | "compression bit in message flags not set."); |
| 135 | return false; |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 136 | } |
| 137 | } else { |
| 138 | // Didn't expect compression -> make sure the request is uncompressed |
| 139 | if (inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS) { |
| 140 | gpr_log(GPR_ERROR, |
| 141 | "Failure: Didn't requested compression, but compression bit in " |
| 142 | "message flags set."); |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 149 | class TestServiceImpl : public TestService::Service { |
| 150 | public: |
| 151 | Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request, |
| 152 | grpc::testing::Empty* response) { |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 153 | MaybeEchoMetadata(context); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 154 | return Status::OK; |
| 155 | } |
| 156 | |
Makarand Dharmapurikar | 1ed0b8e | 2016-09-14 15:01:16 -0700 | [diff] [blame] | 157 | // Response contains current timestamp. We ignore everything in the request. |
David Garcia Quintas | b529997 | 2016-10-12 14:13:42 -0700 | [diff] [blame] | 158 | Status CacheableUnaryCall(ServerContext* context, |
| 159 | const SimpleRequest* request, |
| 160 | SimpleResponse* response) { |
Makarand Dharmapurikar | af564a1 | 2016-09-28 12:50:37 -0700 | [diff] [blame] | 161 | gpr_timespec ts = gpr_now(GPR_CLOCK_PRECISE); |
Makarand Dharmapurikar | c9beaca | 2016-09-30 11:26:13 -0700 | [diff] [blame] | 162 | std::string timestamp = std::to_string((long long unsigned)ts.tv_nsec); |
Makarand Dharmapurikar | 1ed0b8e | 2016-09-14 15:01:16 -0700 | [diff] [blame] | 163 | response->mutable_payload()->set_body(timestamp.c_str(), timestamp.size()); |
Makarand Dharmapurikar | 012fc18 | 2016-09-28 10:46:27 -0700 | [diff] [blame] | 164 | context->AddInitialMetadata("cache-control", "max-age=60, public"); |
Makarand Dharmapurikar | 1ed0b8e | 2016-09-14 15:01:16 -0700 | [diff] [blame] | 165 | return Status::OK; |
| 166 | } |
| 167 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 168 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 169 | SimpleResponse* response) { |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 170 | MaybeEchoMetadata(context); |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 171 | if (request->has_response_compressed()) { |
| 172 | const bool compression_requested = request->response_compressed().value(); |
| 173 | gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s", |
| 174 | compression_requested ? "enabled" : "disabled", __func__); |
| 175 | if (compression_requested) { |
| 176 | // Any level would do, let's go for HIGH because we are overachievers. |
| 177 | context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH); |
| 178 | } else { |
| 179 | context->set_compression_level(GRPC_COMPRESS_LEVEL_NONE); |
| 180 | } |
| 181 | } |
| 182 | if (!CheckExpectedCompression(*context, |
| 183 | request->expect_compressed().value())) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 184 | return Status(grpc::StatusCode::INVALID_ARGUMENT, |
| 185 | "Compressed request expectation not met."); |
| 186 | } |
David Garcia Quintas | 864d186 | 2015-08-13 15:26:00 -0700 | [diff] [blame] | 187 | if (request->response_size() > 0) { |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 188 | if (!SetPayload(request->response_size(), response->mutable_payload())) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 189 | return Status(grpc::StatusCode::INVALID_ARGUMENT, |
| 190 | "Error creating payload."); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 193 | |
| 194 | if (request->has_response_status()) { |
David Garcia Quintas | 2e1bb1b | 2015-08-10 14:05:57 -0700 | [diff] [blame] | 195 | return Status( |
| 196 | static_cast<grpc::StatusCode>(request->response_status().code()), |
| 197 | request->response_status().message()); |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 198 | } |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 199 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 200 | return Status::OK; |
| 201 | } |
| 202 | |
| 203 | Status StreamingOutputCall( |
| 204 | ServerContext* context, const StreamingOutputCallRequest* request, |
| 205 | ServerWriter<StreamingOutputCallResponse>* writer) { |
| 206 | StreamingOutputCallResponse response; |
| 207 | bool write_success = true; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 208 | for (int i = 0; write_success && i < request->response_parameters_size(); |
| 209 | i++) { |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 210 | if (!SetPayload(request->response_parameters(i).size(), |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 211 | response.mutable_payload())) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 212 | return Status(grpc::StatusCode::INVALID_ARGUMENT, |
| 213 | "Error creating payload."); |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 214 | } |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 215 | WriteOptions wopts; |
| 216 | if (request->response_parameters(i).has_compressed()) { |
David Garcia Quintas | 446f70e | 2016-06-21 17:13:28 -0700 | [diff] [blame] | 217 | // Compress by default. Disabled on a per-message basis. |
| 218 | context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH); |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 219 | const bool compression_requested = |
| 220 | request->response_parameters(i).compressed().value(); |
| 221 | gpr_log(GPR_DEBUG, "Request for compression (%s) present for %s", |
| 222 | compression_requested ? "enabled" : "disabled", __func__); |
| 223 | if (!compression_requested) { |
| 224 | wopts.set_no_compression(); |
| 225 | } // else, compression is already enabled via the context. |
| 226 | } |
Makarand Dharmapurikar | 509246f | 2016-06-09 16:28:02 -0700 | [diff] [blame] | 227 | int time_us; |
| 228 | if ((time_us = request->response_parameters(i).interval_us()) > 0) { |
| 229 | // Sleep before response if needed |
| 230 | gpr_timespec sleep_time = |
| 231 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 232 | gpr_time_from_micros(time_us, GPR_TIMESPAN)); |
| 233 | gpr_sleep_until(sleep_time); |
| 234 | } |
David Garcia Quintas | dacce7a | 2016-06-21 16:40:20 -0700 | [diff] [blame] | 235 | write_success = writer->Write(response, wopts); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 236 | } |
| 237 | if (write_success) { |
| 238 | return Status::OK; |
| 239 | } else { |
| 240 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | Status StreamingInputCall(ServerContext* context, |
| 245 | ServerReader<StreamingInputCallRequest>* reader, |
| 246 | StreamingInputCallResponse* response) { |
| 247 | StreamingInputCallRequest request; |
| 248 | int aggregated_payload_size = 0; |
| 249 | while (reader->Read(&request)) { |
David Garcia Quintas | 5608752 | 2016-06-16 16:52:11 -0700 | [diff] [blame] | 250 | if (!CheckExpectedCompression(*context, |
| 251 | request.expect_compressed().value())) { |
David Garcia Quintas | 74686ce | 2016-06-09 15:33:33 -0700 | [diff] [blame] | 252 | return Status(grpc::StatusCode::INVALID_ARGUMENT, |
| 253 | "Compressed request expectation not met."); |
| 254 | } |
yang-g | f6befe8 | 2015-08-13 13:51:53 -0700 | [diff] [blame] | 255 | if (request.has_payload()) { |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 256 | aggregated_payload_size += request.payload().body().size(); |
| 257 | } |
| 258 | } |
| 259 | response->set_aggregated_payload_size(aggregated_payload_size); |
| 260 | return Status::OK; |
| 261 | } |
| 262 | |
| 263 | Status FullDuplexCall( |
| 264 | ServerContext* context, |
| 265 | ServerReaderWriter<StreamingOutputCallResponse, |
| 266 | StreamingOutputCallRequest>* stream) { |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 267 | MaybeEchoMetadata(context); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 268 | StreamingOutputCallRequest request; |
| 269 | StreamingOutputCallResponse response; |
| 270 | bool write_success = true; |
| 271 | while (write_success && stream->Read(&request)) { |
Mark D. Roth | dbf2adc | 2016-07-07 08:19:27 -0700 | [diff] [blame] | 272 | if (request.has_response_status()) { |
| 273 | return Status( |
| 274 | static_cast<grpc::StatusCode>(request.response_status().code()), |
| 275 | request.response_status().message()); |
| 276 | } |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 277 | if (request.response_parameters_size() != 0) { |
| 278 | response.mutable_payload()->set_type(request.payload().type()); |
| 279 | response.mutable_payload()->set_body( |
| 280 | grpc::string(request.response_parameters(0).size(), '\0')); |
Makarand Dharmapurikar | 509246f | 2016-06-09 16:28:02 -0700 | [diff] [blame] | 281 | int time_us; |
| 282 | if ((time_us = request.response_parameters(0).interval_us()) > 0) { |
| 283 | // Sleep before response if needed |
| 284 | gpr_timespec sleep_time = |
| 285 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 286 | gpr_time_from_micros(time_us, GPR_TIMESPAN)); |
| 287 | gpr_sleep_until(sleep_time); |
| 288 | } |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 289 | write_success = stream->Write(response); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 290 | } |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 291 | } |
| 292 | if (write_success) { |
| 293 | return Status::OK; |
| 294 | } else { |
| 295 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | Status HalfDuplexCall( |
| 300 | ServerContext* context, |
| 301 | ServerReaderWriter<StreamingOutputCallResponse, |
| 302 | StreamingOutputCallRequest>* stream) { |
| 303 | std::vector<StreamingOutputCallRequest> requests; |
| 304 | StreamingOutputCallRequest request; |
| 305 | while (stream->Read(&request)) { |
| 306 | requests.push_back(request); |
| 307 | } |
| 308 | |
| 309 | StreamingOutputCallResponse response; |
| 310 | bool write_success = true; |
| 311 | for (unsigned int i = 0; write_success && i < requests.size(); i++) { |
| 312 | response.mutable_payload()->set_type(requests[i].payload().type()); |
| 313 | if (requests[i].response_parameters_size() == 0) { |
| 314 | return Status(grpc::StatusCode::INTERNAL, |
| 315 | "Request does not have response parameters."); |
| 316 | } |
| 317 | response.mutable_payload()->set_body( |
| 318 | grpc::string(requests[i].response_parameters(0).size(), '\0')); |
| 319 | write_success = stream->Write(response); |
| 320 | } |
| 321 | if (write_success) { |
| 322 | return Status::OK; |
| 323 | } else { |
| 324 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 325 | } |
| 326 | } |
| 327 | }; |
| 328 | |
Nicolas "Pixel" Noble | 6570b83 | 2016-07-18 23:29:50 +0200 | [diff] [blame] | 329 | void grpc::testing::interop::RunServer( |
| 330 | std::shared_ptr<ServerCredentials> creds) { |
| 331 | GPR_ASSERT(FLAGS_port != 0); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 332 | std::ostringstream server_address; |
Yang Gao | dab7095 | 2015-01-23 16:06:36 -0800 | [diff] [blame] | 333 | server_address << "0.0.0.0:" << FLAGS_port; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 334 | TestServiceImpl service; |
| 335 | |
| 336 | SimpleRequest request; |
| 337 | SimpleResponse response; |
| 338 | |
| 339 | ServerBuilder builder; |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 340 | builder.RegisterService(&service); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 341 | builder.AddListeningPort(server_address.str(), creds); |
Mark D. Roth | c99fd89 | 2016-09-06 08:16:13 -0700 | [diff] [blame] | 342 | if (FLAGS_max_send_message_size >= 0) { |
| 343 | builder.SetMaxSendMessageSize(FLAGS_max_send_message_size); |
| 344 | } |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 345 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 346 | gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
David Garcia Quintas | 7fa08e2 | 2016-12-04 22:11:57 -0800 | [diff] [blame^] | 347 | while (!gpr_atm_no_barrier_load(&g_got_sigint)) { |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 348 | sleep(5); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 349 | } |
| 350 | } |