yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, 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 <signal.h> |
| 35 | #include <unistd.h> |
| 36 | |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 37 | #include <fstream> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 38 | #include <memory> |
| 39 | #include <sstream> |
| 40 | #include <thread> |
| 41 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 42 | #include <gflags/gflags.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 43 | #include <grpc++/security/server_credentials.h> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 44 | #include <grpc++/server.h> |
| 45 | #include <grpc++/server_builder.h> |
| 46 | #include <grpc++/server_context.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 47 | #include <grpc/grpc.h> |
| 48 | #include <grpc/support/log.h> |
| 49 | #include <grpc/support/useful.h> |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 50 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 51 | #include "src/proto/grpc/testing/empty.grpc.pb.h" |
| 52 | #include "src/proto/grpc/testing/messages.grpc.pb.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 53 | #include "src/proto/grpc/testing/test.grpc.pb.h" |
| 54 | #include "test/cpp/interop/server_helper.h" |
| 55 | #include "test/cpp/util/test_config.h" |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 56 | |
yang-g | 035cf09 | 2015-09-18 12:11:05 -0700 | [diff] [blame] | 57 | DEFINE_bool(use_tls, false, "Whether to use tls."); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 58 | DEFINE_int32(port, 0, "Server port."); |
| 59 | |
| 60 | using grpc::Server; |
| 61 | using grpc::ServerBuilder; |
| 62 | using grpc::ServerContext; |
| 63 | using grpc::ServerCredentials; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 64 | using grpc::ServerReader; |
| 65 | using grpc::ServerReaderWriter; |
| 66 | using grpc::ServerWriter; |
| 67 | using grpc::SslServerCredentialsOptions; |
David Garcia Quintas | 7c0d914 | 2015-07-23 04:58:20 -0700 | [diff] [blame] | 68 | using grpc::testing::InteropServerContextInspector; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 69 | using grpc::testing::Payload; |
| 70 | using grpc::testing::PayloadType; |
| 71 | using grpc::testing::SimpleRequest; |
| 72 | using grpc::testing::SimpleResponse; |
| 73 | using grpc::testing::StreamingInputCallRequest; |
| 74 | using grpc::testing::StreamingInputCallResponse; |
| 75 | using grpc::testing::StreamingOutputCallRequest; |
| 76 | using grpc::testing::StreamingOutputCallResponse; |
| 77 | using grpc::testing::TestService; |
| 78 | using grpc::Status; |
| 79 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 80 | static bool got_sigint = false; |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 81 | static const char* kRandomFile = "test/cpp/interop/rnd.dat"; |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 82 | |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 83 | const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial"; |
| 84 | const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin"; |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 85 | const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent"; |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 86 | |
| 87 | void MaybeEchoMetadata(ServerContext* context) { |
| 88 | const auto& client_metadata = context->client_metadata(); |
| 89 | GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1); |
| 90 | GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1); |
| 91 | |
| 92 | auto iter = client_metadata.find(kEchoInitialMetadataKey); |
| 93 | if (iter != client_metadata.end()) { |
| 94 | context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data()); |
| 95 | } |
| 96 | iter = client_metadata.find(kEchoTrailingBinMetadataKey); |
| 97 | if (iter != client_metadata.end()) { |
| 98 | context->AddTrailingMetadata( |
| 99 | kEchoTrailingBinMetadataKey, |
| 100 | grpc::string(iter->second.begin(), iter->second.end())); |
| 101 | } |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 102 | // Check if client sent a magic key in the header that makes us echo |
| 103 | // back the user-agent (for testing purpose) |
| 104 | iter = client_metadata.find(kEchoUserAgentKey); |
| 105 | if (iter != client_metadata.end()) { |
| 106 | iter = client_metadata.find("user-agent"); |
| 107 | if (iter != client_metadata.end()) { |
| 108 | context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data()); |
| 109 | } |
| 110 | } |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 111 | } |
| 112 | |
David Garcia Quintas | 5756c8f | 2016-06-07 18:00:44 -0700 | [diff] [blame] | 113 | bool SetPayload(PayloadType response_type, int size, Payload* payload) { |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 114 | payload->set_type(response_type); |
David Garcia Quintas | c899319 | 2015-07-22 09:10:39 -0700 | [diff] [blame] | 115 | switch (response_type) { |
| 116 | case PayloadType::COMPRESSABLE: { |
| 117 | std::unique_ptr<char[]> body(new char[size]()); |
| 118 | payload->set_body(body.get(), size); |
| 119 | } break; |
| 120 | case PayloadType::UNCOMPRESSABLE: { |
| 121 | std::unique_ptr<char[]> body(new char[size]()); |
| 122 | std::ifstream rnd_file(kRandomFile); |
| 123 | GPR_ASSERT(rnd_file.good()); |
| 124 | rnd_file.read(body.get(), size); |
| 125 | GPR_ASSERT(!rnd_file.eof()); // Requested more rnd bytes than available |
| 126 | payload->set_body(body.get(), size); |
| 127 | } break; |
| 128 | default: |
| 129 | GPR_ASSERT(false); |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 130 | } |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 131 | return true; |
| 132 | } |
| 133 | |
David Garcia Quintas | 9c512bd | 2015-07-20 23:43:53 -0700 | [diff] [blame] | 134 | template <typename RequestType> |
| 135 | void SetResponseCompression(ServerContext* context, |
| 136 | const RequestType& request) { |
David Garcia Quintas | 5756c8f | 2016-06-07 18:00:44 -0700 | [diff] [blame] | 137 | if (request.request_compressed_response()) { |
| 138 | // Any level would do, let's go for HIGH because we are overachievers. |
| 139 | context->set_compression_level(GRPC_COMPRESS_LEVEL_HIGH); |
David Garcia Quintas | 9c512bd | 2015-07-20 23:43:53 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 143 | class TestServiceImpl : public TestService::Service { |
| 144 | public: |
| 145 | Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request, |
| 146 | grpc::testing::Empty* response) { |
Makarand Dharmapurikar | bfc7ada | 2016-02-22 15:42:18 -0800 | [diff] [blame] | 147 | MaybeEchoMetadata(context); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 148 | return Status::OK; |
| 149 | } |
| 150 | |
| 151 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 152 | SimpleResponse* response) { |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 153 | MaybeEchoMetadata(context); |
David Garcia Quintas | 2e1bb1b | 2015-08-10 14:05:57 -0700 | [diff] [blame] | 154 | SetResponseCompression(context, *request); |
David Garcia Quintas | 864d186 | 2015-08-13 15:26:00 -0700 | [diff] [blame] | 155 | if (request->response_size() > 0) { |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 156 | if (!SetPayload(request->response_type(), request->response_size(), |
| 157 | response->mutable_payload())) { |
| 158 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 159 | } |
| 160 | } |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 161 | |
| 162 | if (request->has_response_status()) { |
David Garcia Quintas | 2e1bb1b | 2015-08-10 14:05:57 -0700 | [diff] [blame] | 163 | return Status( |
| 164 | static_cast<grpc::StatusCode>(request->response_status().code()), |
| 165 | request->response_status().message()); |
Abhishek Kumar | e1c867d | 2015-08-05 11:04:45 -0700 | [diff] [blame] | 166 | } |
David Garcia Quintas | 80f3995 | 2015-07-21 16:07:36 -0700 | [diff] [blame] | 167 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 168 | return Status::OK; |
| 169 | } |
| 170 | |
| 171 | Status StreamingOutputCall( |
| 172 | ServerContext* context, const StreamingOutputCallRequest* request, |
| 173 | ServerWriter<StreamingOutputCallResponse>* writer) { |
David Garcia Quintas | 9c512bd | 2015-07-20 23:43:53 -0700 | [diff] [blame] | 174 | SetResponseCompression(context, *request); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 175 | StreamingOutputCallResponse response; |
| 176 | bool write_success = true; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 177 | for (int i = 0; write_success && i < request->response_parameters_size(); |
| 178 | i++) { |
David Garcia Quintas | 04ecfa1 | 2015-08-25 14:19:48 -0700 | [diff] [blame] | 179 | if (!SetPayload(request->response_type(), |
| 180 | request->response_parameters(i).size(), |
| 181 | response.mutable_payload())) { |
| 182 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 183 | } |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 184 | write_success = writer->Write(response); |
| 185 | } |
| 186 | if (write_success) { |
| 187 | return Status::OK; |
| 188 | } else { |
| 189 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | Status StreamingInputCall(ServerContext* context, |
| 194 | ServerReader<StreamingInputCallRequest>* reader, |
| 195 | StreamingInputCallResponse* response) { |
| 196 | StreamingInputCallRequest request; |
| 197 | int aggregated_payload_size = 0; |
| 198 | while (reader->Read(&request)) { |
yang-g | f6befe8 | 2015-08-13 13:51:53 -0700 | [diff] [blame] | 199 | if (request.has_payload()) { |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 200 | aggregated_payload_size += request.payload().body().size(); |
| 201 | } |
| 202 | } |
| 203 | response->set_aggregated_payload_size(aggregated_payload_size); |
| 204 | return Status::OK; |
| 205 | } |
| 206 | |
| 207 | Status FullDuplexCall( |
| 208 | ServerContext* context, |
| 209 | ServerReaderWriter<StreamingOutputCallResponse, |
| 210 | StreamingOutputCallRequest>* stream) { |
yang-g | c10348a | 2016-02-19 16:05:10 -0800 | [diff] [blame] | 211 | MaybeEchoMetadata(context); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 212 | StreamingOutputCallRequest request; |
| 213 | StreamingOutputCallResponse response; |
| 214 | bool write_success = true; |
| 215 | while (write_success && stream->Read(&request)) { |
David Garcia Quintas | 9c512bd | 2015-07-20 23:43:53 -0700 | [diff] [blame] | 216 | SetResponseCompression(context, request); |
yang-g | 69563b9 | 2015-07-10 15:32:11 -0700 | [diff] [blame] | 217 | if (request.response_parameters_size() != 0) { |
| 218 | response.mutable_payload()->set_type(request.payload().type()); |
| 219 | response.mutable_payload()->set_body( |
| 220 | grpc::string(request.response_parameters(0).size(), '\0')); |
| 221 | write_success = stream->Write(response); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 222 | } |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 223 | } |
| 224 | if (write_success) { |
| 225 | return Status::OK; |
| 226 | } else { |
| 227 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | Status HalfDuplexCall( |
| 232 | ServerContext* context, |
| 233 | ServerReaderWriter<StreamingOutputCallResponse, |
| 234 | StreamingOutputCallRequest>* stream) { |
| 235 | std::vector<StreamingOutputCallRequest> requests; |
| 236 | StreamingOutputCallRequest request; |
| 237 | while (stream->Read(&request)) { |
| 238 | requests.push_back(request); |
| 239 | } |
| 240 | |
| 241 | StreamingOutputCallResponse response; |
| 242 | bool write_success = true; |
| 243 | for (unsigned int i = 0; write_success && i < requests.size(); i++) { |
| 244 | response.mutable_payload()->set_type(requests[i].payload().type()); |
| 245 | if (requests[i].response_parameters_size() == 0) { |
| 246 | return Status(grpc::StatusCode::INTERNAL, |
| 247 | "Request does not have response parameters."); |
| 248 | } |
| 249 | response.mutable_payload()->set_body( |
| 250 | grpc::string(requests[i].response_parameters(0).size(), '\0')); |
| 251 | write_success = stream->Write(response); |
| 252 | } |
| 253 | if (write_success) { |
| 254 | return Status::OK; |
| 255 | } else { |
| 256 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 257 | } |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | void RunServer() { |
| 262 | std::ostringstream server_address; |
Yang Gao | dab7095 | 2015-01-23 16:06:36 -0800 | [diff] [blame] | 263 | server_address << "0.0.0.0:" << FLAGS_port; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 264 | TestServiceImpl service; |
| 265 | |
| 266 | SimpleRequest request; |
| 267 | SimpleResponse response; |
| 268 | |
| 269 | ServerBuilder builder; |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 270 | builder.RegisterService(&service); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 271 | std::shared_ptr<ServerCredentials> creds = |
| 272 | grpc::testing::CreateInteropServerCredentials(); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 273 | builder.AddListeningPort(server_address.str(), creds); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 274 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 275 | gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 276 | while (!got_sigint) { |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 277 | sleep(5); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 281 | static void sigint_handler(int x) { got_sigint = true; } |
| 282 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 283 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 284 | grpc::testing::InitTest(&argc, &argv, true); |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 285 | signal(SIGINT, sigint_handler); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 286 | |
| 287 | GPR_ASSERT(FLAGS_port != 0); |
| 288 | RunServer(); |
| 289 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 290 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 291 | } |