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