blob: bf576322b40f34f419adc8d7162f6a7c7379a229 [file] [log] [blame]
yangg06170572015-01-12 13:12:45 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
yangg06170572015-01-12 13:12:45 -08004 * 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-g9e2f90c2015-08-21 15:35:03 -070034#include <signal.h>
35#include <unistd.h>
36
David Garcia Quintasc8993192015-07-22 09:10:39 -070037#include <fstream>
yangg06170572015-01-12 13:12:45 -080038#include <memory>
39#include <sstream>
40#include <thread>
41
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010042#include <gflags/gflags.h>
yangg06170572015-01-12 13:12:45 -080043#include <grpc/grpc.h>
44#include <grpc/support/log.h>
David Garcia Quintas7c0d9142015-07-23 04:58:20 -070045#include <grpc/support/useful.h>
yangg06170572015-01-12 13:12:45 -080046#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070049#include <grpc++/security/server_credentials.h>
David Garcia Quintasc8993192015-07-22 09:10:39 -070050
yang-g9e2f90c2015-08-21 15:35:03 -070051#include "test/cpp/interop/server_helper.h"
52#include "test/cpp/util/test_config.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080053#include "src/proto/grpc/testing/test.grpc.pb.h"
54#include "src/proto/grpc/testing/empty.grpc.pb.h"
55#include "src/proto/grpc/testing/messages.grpc.pb.h"
yangg06170572015-01-12 13:12:45 -080056
yang-g035cf092015-09-18 12:11:05 -070057DEFINE_bool(use_tls, false, "Whether to use tls.");
yangg06170572015-01-12 13:12:45 -080058DEFINE_int32(port, 0, "Server port.");
59
60using grpc::Server;
61using grpc::ServerBuilder;
62using grpc::ServerContext;
63using grpc::ServerCredentials;
yangg06170572015-01-12 13:12:45 -080064using grpc::ServerReader;
65using grpc::ServerReaderWriter;
66using grpc::ServerWriter;
67using grpc::SslServerCredentialsOptions;
David Garcia Quintas7c0d9142015-07-23 04:58:20 -070068using grpc::testing::InteropServerContextInspector;
yangg06170572015-01-12 13:12:45 -080069using grpc::testing::Payload;
70using grpc::testing::PayloadType;
71using grpc::testing::SimpleRequest;
72using grpc::testing::SimpleResponse;
73using grpc::testing::StreamingInputCallRequest;
74using grpc::testing::StreamingInputCallResponse;
75using grpc::testing::StreamingOutputCallRequest;
76using grpc::testing::StreamingOutputCallResponse;
77using grpc::testing::TestService;
78using grpc::Status;
79
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080080static bool got_sigint = false;
David Garcia Quintasc8993192015-07-22 09:10:39 -070081static const char* kRandomFile = "test/cpp/interop/rnd.dat";
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080082
yang-gc10348a2016-02-19 16:05:10 -080083const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial";
84const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin";
85
86void 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 }
101}
102
yangg06170572015-01-12 13:12:45 -0800103bool SetPayload(PayloadType type, int size, Payload* payload) {
David Garcia Quintasc8993192015-07-22 09:10:39 -0700104 PayloadType response_type;
105 if (type == PayloadType::RANDOM) {
106 response_type =
107 rand() & 0x1 ? PayloadType::COMPRESSABLE : PayloadType::UNCOMPRESSABLE;
108 } else {
109 response_type = type;
110 }
yangg06170572015-01-12 13:12:45 -0800111 payload->set_type(response_type);
David Garcia Quintasc8993192015-07-22 09:10:39 -0700112 switch (response_type) {
113 case PayloadType::COMPRESSABLE: {
114 std::unique_ptr<char[]> body(new char[size]());
115 payload->set_body(body.get(), size);
116 } break;
117 case PayloadType::UNCOMPRESSABLE: {
118 std::unique_ptr<char[]> body(new char[size]());
119 std::ifstream rnd_file(kRandomFile);
120 GPR_ASSERT(rnd_file.good());
121 rnd_file.read(body.get(), size);
122 GPR_ASSERT(!rnd_file.eof()); // Requested more rnd bytes than available
123 payload->set_body(body.get(), size);
124 } break;
125 default:
126 GPR_ASSERT(false);
David Garcia Quintas80f39952015-07-21 16:07:36 -0700127 }
yangg06170572015-01-12 13:12:45 -0800128 return true;
129}
130
David Garcia Quintas9c512bd2015-07-20 23:43:53 -0700131template <typename RequestType>
132void SetResponseCompression(ServerContext* context,
133 const RequestType& request) {
David Garcia Quintas864d1862015-08-13 15:26:00 -0700134 switch (request.response_compression()) {
135 case grpc::testing::NONE:
136 context->set_compression_algorithm(GRPC_COMPRESS_NONE);
137 break;
138 case grpc::testing::GZIP:
139 context->set_compression_algorithm(GRPC_COMPRESS_GZIP);
140 break;
141 case grpc::testing::DEFLATE:
142 context->set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
143 break;
144 default:
145 abort();
David Garcia Quintas9c512bd2015-07-20 23:43:53 -0700146 }
147}
148
yangg06170572015-01-12 13:12:45 -0800149class TestServiceImpl : public TestService::Service {
150 public:
151 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
152 grpc::testing::Empty* response) {
153 return Status::OK;
154 }
155
156 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
157 SimpleResponse* response) {
yang-gc10348a2016-02-19 16:05:10 -0800158 MaybeEchoMetadata(context);
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700159 SetResponseCompression(context, *request);
David Garcia Quintas864d1862015-08-13 15:26:00 -0700160 if (request->response_size() > 0) {
yangg06170572015-01-12 13:12:45 -0800161 if (!SetPayload(request->response_type(), request->response_size(),
162 response->mutable_payload())) {
163 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
164 }
165 }
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700166
167 if (request->has_response_status()) {
David Garcia Quintas2e1bb1b2015-08-10 14:05:57 -0700168 return Status(
169 static_cast<grpc::StatusCode>(request->response_status().code()),
170 request->response_status().message());
Abhishek Kumare1c867d2015-08-05 11:04:45 -0700171 }
David Garcia Quintas80f39952015-07-21 16:07:36 -0700172
yangg06170572015-01-12 13:12:45 -0800173 return Status::OK;
174 }
175
176 Status StreamingOutputCall(
177 ServerContext* context, const StreamingOutputCallRequest* request,
178 ServerWriter<StreamingOutputCallResponse>* writer) {
David Garcia Quintas9c512bd2015-07-20 23:43:53 -0700179 SetResponseCompression(context, *request);
yangg06170572015-01-12 13:12:45 -0800180 StreamingOutputCallResponse response;
181 bool write_success = true;
yangg06170572015-01-12 13:12:45 -0800182 for (int i = 0; write_success && i < request->response_parameters_size();
183 i++) {
David Garcia Quintas04ecfa12015-08-25 14:19:48 -0700184 if (!SetPayload(request->response_type(),
185 request->response_parameters(i).size(),
186 response.mutable_payload())) {
187 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
188 }
yangg06170572015-01-12 13:12:45 -0800189 write_success = writer->Write(response);
190 }
191 if (write_success) {
192 return Status::OK;
193 } else {
194 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
195 }
196 }
197
198 Status StreamingInputCall(ServerContext* context,
199 ServerReader<StreamingInputCallRequest>* reader,
200 StreamingInputCallResponse* response) {
201 StreamingInputCallRequest request;
202 int aggregated_payload_size = 0;
203 while (reader->Read(&request)) {
yang-gf6befe82015-08-13 13:51:53 -0700204 if (request.has_payload()) {
yangg06170572015-01-12 13:12:45 -0800205 aggregated_payload_size += request.payload().body().size();
206 }
207 }
208 response->set_aggregated_payload_size(aggregated_payload_size);
209 return Status::OK;
210 }
211
212 Status FullDuplexCall(
213 ServerContext* context,
214 ServerReaderWriter<StreamingOutputCallResponse,
215 StreamingOutputCallRequest>* stream) {
yang-gc10348a2016-02-19 16:05:10 -0800216 MaybeEchoMetadata(context);
yangg06170572015-01-12 13:12:45 -0800217 StreamingOutputCallRequest request;
218 StreamingOutputCallResponse response;
219 bool write_success = true;
220 while (write_success && stream->Read(&request)) {
David Garcia Quintas9c512bd2015-07-20 23:43:53 -0700221 SetResponseCompression(context, request);
yang-g69563b92015-07-10 15:32:11 -0700222 if (request.response_parameters_size() != 0) {
223 response.mutable_payload()->set_type(request.payload().type());
224 response.mutable_payload()->set_body(
225 grpc::string(request.response_parameters(0).size(), '\0'));
226 write_success = stream->Write(response);
yangg06170572015-01-12 13:12:45 -0800227 }
yangg06170572015-01-12 13:12:45 -0800228 }
229 if (write_success) {
230 return Status::OK;
231 } else {
232 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
233 }
234 }
235
236 Status HalfDuplexCall(
237 ServerContext* context,
238 ServerReaderWriter<StreamingOutputCallResponse,
239 StreamingOutputCallRequest>* stream) {
240 std::vector<StreamingOutputCallRequest> requests;
241 StreamingOutputCallRequest request;
242 while (stream->Read(&request)) {
243 requests.push_back(request);
244 }
245
246 StreamingOutputCallResponse response;
247 bool write_success = true;
248 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
249 response.mutable_payload()->set_type(requests[i].payload().type());
250 if (requests[i].response_parameters_size() == 0) {
251 return Status(grpc::StatusCode::INTERNAL,
252 "Request does not have response parameters.");
253 }
254 response.mutable_payload()->set_body(
255 grpc::string(requests[i].response_parameters(0).size(), '\0'));
256 write_success = stream->Write(response);
257 }
258 if (write_success) {
259 return Status::OK;
260 } else {
261 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
262 }
263 }
264};
265
266void RunServer() {
267 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800268 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800269 TestServiceImpl service;
270
271 SimpleRequest request;
272 SimpleResponse response;
273
274 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800275 builder.RegisterService(&service);
Yang Gaoa4002072015-04-09 23:25:21 -0700276 std::shared_ptr<ServerCredentials> creds =
277 grpc::testing::CreateInteropServerCredentials();
Nicolas Noblecfd60732015-03-18 16:27:43 -0700278 builder.AddListeningPort(server_address.str(), creds);
yangg06170572015-01-12 13:12:45 -0800279 std::unique_ptr<Server> server(builder.BuildAndStart());
280 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800281 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800282 sleep(5);
yangg06170572015-01-12 13:12:45 -0800283 }
284}
285
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800286static void sigint_handler(int x) { got_sigint = true; }
287
yangg06170572015-01-12 13:12:45 -0800288int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -0700289 grpc::testing::InitTest(&argc, &argv, true);
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800290 signal(SIGINT, sigint_handler);
yangg06170572015-01-12 13:12:45 -0800291
292 GPR_ASSERT(FLAGS_port != 0);
293 RunServer();
294
yangg06170572015-01-12 13:12:45 -0800295 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800296}