blob: 9810ff6622b2bb920ed09c1a98b2937c9b929ed7 [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
34#include <memory>
35#include <sstream>
36#include <thread>
37
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080038#include <signal.h>
Craig Tillercf133f42015-02-26 14:05:56 -080039#include <unistd.h>
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080040
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010041#include <gflags/gflags.h>
yangg06170572015-01-12 13:12:45 -080042#include <grpc/grpc.h>
43#include <grpc/support/log.h>
44#include "test/core/end2end/data/ssl_test_data.h"
45#include <grpc++/config.h>
46#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.h>
49#include <grpc++/server_credentials.h>
50#include <grpc++/status.h>
51#include <grpc++/stream.h>
52#include "test/cpp/interop/test.pb.h"
53#include "test/cpp/interop/empty.pb.h"
54#include "test/cpp/interop/messages.pb.h"
55
56DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
57DEFINE_int32(port, 0, "Server port.");
58
59using grpc::Server;
60using grpc::ServerBuilder;
61using grpc::ServerContext;
62using grpc::ServerCredentials;
63using grpc::ServerCredentialsFactory;
64using grpc::ServerReader;
65using grpc::ServerReaderWriter;
66using grpc::ServerWriter;
67using grpc::SslServerCredentialsOptions;
68using grpc::testing::Payload;
69using grpc::testing::PayloadType;
70using grpc::testing::SimpleRequest;
71using grpc::testing::SimpleResponse;
72using grpc::testing::StreamingInputCallRequest;
73using grpc::testing::StreamingInputCallResponse;
74using grpc::testing::StreamingOutputCallRequest;
75using grpc::testing::StreamingOutputCallResponse;
76using grpc::testing::TestService;
77using grpc::Status;
78
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010079// In some distros, gflags is in the namespace google, and in some others,
80// in gflags. This hack is enabling us to find both.
81namespace google { }
82namespace gflags { }
83using namespace google;
84using namespace gflags;
85
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080086static bool got_sigint = false;
87
yangg06170572015-01-12 13:12:45 -080088bool SetPayload(PayloadType type, int size, Payload* payload) {
89 PayloadType response_type = type;
90 // TODO(yangg): Support UNCOMPRESSABLE payload.
91 if (type != PayloadType::COMPRESSABLE) {
92 return false;
93 }
94 payload->set_type(response_type);
95 std::unique_ptr<char[]> body(new char[size]());
96 payload->set_body(body.get(), size);
97 return true;
98}
99
100class TestServiceImpl : public TestService::Service {
101 public:
102 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
103 grpc::testing::Empty* response) {
104 return Status::OK;
105 }
106
107 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
108 SimpleResponse* response) {
109 if (request->has_response_size() && request->response_size() > 0) {
110 if (!SetPayload(request->response_type(), request->response_size(),
111 response->mutable_payload())) {
112 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
113 }
114 }
115 return Status::OK;
116 }
117
118 Status StreamingOutputCall(
119 ServerContext* context, const StreamingOutputCallRequest* request,
120 ServerWriter<StreamingOutputCallResponse>* writer) {
121 StreamingOutputCallResponse response;
122 bool write_success = true;
123 response.mutable_payload()->set_type(request->response_type());
124 for (int i = 0; write_success && i < request->response_parameters_size();
125 i++) {
126 response.mutable_payload()->set_body(
127 grpc::string(request->response_parameters(i).size(), '\0'));
128 write_success = writer->Write(response);
129 }
130 if (write_success) {
131 return Status::OK;
132 } else {
133 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
134 }
135 }
136
137 Status StreamingInputCall(ServerContext* context,
138 ServerReader<StreamingInputCallRequest>* reader,
139 StreamingInputCallResponse* response) {
140 StreamingInputCallRequest request;
141 int aggregated_payload_size = 0;
142 while (reader->Read(&request)) {
143 if (request.has_payload() && request.payload().has_body()) {
144 aggregated_payload_size += request.payload().body().size();
145 }
146 }
147 response->set_aggregated_payload_size(aggregated_payload_size);
148 return Status::OK;
149 }
150
151 Status FullDuplexCall(
152 ServerContext* context,
153 ServerReaderWriter<StreamingOutputCallResponse,
154 StreamingOutputCallRequest>* stream) {
155 StreamingOutputCallRequest request;
156 StreamingOutputCallResponse response;
157 bool write_success = true;
158 while (write_success && stream->Read(&request)) {
159 response.mutable_payload()->set_type(request.payload().type());
160 if (request.response_parameters_size() == 0) {
161 return Status(grpc::StatusCode::INTERNAL,
162 "Request does not have response parameters.");
163 }
164 response.mutable_payload()->set_body(
165 grpc::string(request.response_parameters(0).size(), '\0'));
166 write_success = stream->Write(response);
167 }
168 if (write_success) {
169 return Status::OK;
170 } else {
171 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
172 }
173 }
174
175 Status HalfDuplexCall(
176 ServerContext* context,
177 ServerReaderWriter<StreamingOutputCallResponse,
178 StreamingOutputCallRequest>* stream) {
179 std::vector<StreamingOutputCallRequest> requests;
180 StreamingOutputCallRequest request;
181 while (stream->Read(&request)) {
182 requests.push_back(request);
183 }
184
185 StreamingOutputCallResponse response;
186 bool write_success = true;
187 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
188 response.mutable_payload()->set_type(requests[i].payload().type());
189 if (requests[i].response_parameters_size() == 0) {
190 return Status(grpc::StatusCode::INTERNAL,
191 "Request does not have response parameters.");
192 }
193 response.mutable_payload()->set_body(
194 grpc::string(requests[i].response_parameters(0).size(), '\0'));
195 write_success = stream->Write(response);
196 }
197 if (write_success) {
198 return Status::OK;
199 } else {
200 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
201 }
202 }
203};
204
205void RunServer() {
206 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800207 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800208 TestServiceImpl service;
209
210 SimpleRequest request;
211 SimpleResponse response;
212
213 ServerBuilder builder;
214 builder.AddPort(server_address.str());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800215 builder.RegisterService(&service);
yangg06170572015-01-12 13:12:45 -0800216 if (FLAGS_enable_ssl) {
217 SslServerCredentialsOptions ssl_opts = {
Julien Boeuf8fbcc432015-01-15 16:44:13 -0800218 "", {{test_server1_key, test_server1_cert}}};
yangg06170572015-01-12 13:12:45 -0800219 std::shared_ptr<ServerCredentials> creds =
220 ServerCredentialsFactory::SslCredentials(ssl_opts);
221 builder.SetCredentials(creds);
222 }
223 std::unique_ptr<Server> server(builder.BuildAndStart());
224 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800225 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800226 sleep(5);
yangg06170572015-01-12 13:12:45 -0800227 }
228}
229
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800230static void sigint_handler(int x) { got_sigint = true; }
231
yangg06170572015-01-12 13:12:45 -0800232int main(int argc, char** argv) {
233 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100234 ParseCommandLineFlags(&argc, &argv, true);
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800235 signal(SIGINT, sigint_handler);
yangg06170572015-01-12 13:12:45 -0800236
237 GPR_ASSERT(FLAGS_port != 0);
238 RunServer();
239
240 grpc_shutdown();
241 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800242}