blob: 1ec51004facd1a23a404abe2bf1b74cf3b6bc7a6 [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;
yangg06170572015-01-12 13:12:45 -080062using grpc::ServerReader;
63using grpc::ServerReaderWriter;
64using grpc::ServerWriter;
65using grpc::SslServerCredentialsOptions;
66using grpc::testing::Payload;
67using grpc::testing::PayloadType;
68using grpc::testing::SimpleRequest;
69using grpc::testing::SimpleResponse;
70using grpc::testing::StreamingInputCallRequest;
71using grpc::testing::StreamingInputCallResponse;
72using grpc::testing::StreamingOutputCallRequest;
73using grpc::testing::StreamingOutputCallResponse;
74using grpc::testing::TestService;
75using grpc::Status;
76
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010077// In some distros, gflags is in the namespace google, and in some others,
78// in gflags. This hack is enabling us to find both.
Craig Tiller47c83fd2015-02-21 22:45:35 -080079namespace google {}
80namespace gflags {}
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010081using namespace google;
82using namespace gflags;
83
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080084static bool got_sigint = false;
85
yangg06170572015-01-12 13:12:45 -080086bool SetPayload(PayloadType type, int size, Payload* payload) {
87 PayloadType response_type = type;
88 // TODO(yangg): Support UNCOMPRESSABLE payload.
89 if (type != PayloadType::COMPRESSABLE) {
90 return false;
91 }
92 payload->set_type(response_type);
93 std::unique_ptr<char[]> body(new char[size]());
94 payload->set_body(body.get(), size);
95 return true;
96}
97
98class TestServiceImpl : public TestService::Service {
99 public:
100 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
101 grpc::testing::Empty* response) {
102 return Status::OK;
103 }
104
105 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
106 SimpleResponse* response) {
107 if (request->has_response_size() && request->response_size() > 0) {
108 if (!SetPayload(request->response_type(), request->response_size(),
109 response->mutable_payload())) {
110 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
111 }
112 }
113 return Status::OK;
114 }
115
116 Status StreamingOutputCall(
117 ServerContext* context, const StreamingOutputCallRequest* request,
118 ServerWriter<StreamingOutputCallResponse>* writer) {
119 StreamingOutputCallResponse response;
120 bool write_success = true;
121 response.mutable_payload()->set_type(request->response_type());
122 for (int i = 0; write_success && i < request->response_parameters_size();
123 i++) {
124 response.mutable_payload()->set_body(
125 grpc::string(request->response_parameters(i).size(), '\0'));
126 write_success = writer->Write(response);
127 }
128 if (write_success) {
129 return Status::OK;
130 } else {
131 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
132 }
133 }
134
135 Status StreamingInputCall(ServerContext* context,
136 ServerReader<StreamingInputCallRequest>* reader,
137 StreamingInputCallResponse* response) {
138 StreamingInputCallRequest request;
139 int aggregated_payload_size = 0;
140 while (reader->Read(&request)) {
141 if (request.has_payload() && request.payload().has_body()) {
142 aggregated_payload_size += request.payload().body().size();
143 }
144 }
145 response->set_aggregated_payload_size(aggregated_payload_size);
146 return Status::OK;
147 }
148
149 Status FullDuplexCall(
150 ServerContext* context,
151 ServerReaderWriter<StreamingOutputCallResponse,
152 StreamingOutputCallRequest>* stream) {
153 StreamingOutputCallRequest request;
154 StreamingOutputCallResponse response;
155 bool write_success = true;
156 while (write_success && stream->Read(&request)) {
157 response.mutable_payload()->set_type(request.payload().type());
158 if (request.response_parameters_size() == 0) {
159 return Status(grpc::StatusCode::INTERNAL,
160 "Request does not have response parameters.");
161 }
162 response.mutable_payload()->set_body(
163 grpc::string(request.response_parameters(0).size(), '\0'));
164 write_success = stream->Write(response);
165 }
166 if (write_success) {
167 return Status::OK;
168 } else {
169 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
170 }
171 }
172
173 Status HalfDuplexCall(
174 ServerContext* context,
175 ServerReaderWriter<StreamingOutputCallResponse,
176 StreamingOutputCallRequest>* stream) {
177 std::vector<StreamingOutputCallRequest> requests;
178 StreamingOutputCallRequest request;
179 while (stream->Read(&request)) {
180 requests.push_back(request);
181 }
182
183 StreamingOutputCallResponse response;
184 bool write_success = true;
185 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
186 response.mutable_payload()->set_type(requests[i].payload().type());
187 if (requests[i].response_parameters_size() == 0) {
188 return Status(grpc::StatusCode::INTERNAL,
189 "Request does not have response parameters.");
190 }
191 response.mutable_payload()->set_body(
192 grpc::string(requests[i].response_parameters(0).size(), '\0'));
193 write_success = stream->Write(response);
194 }
195 if (write_success) {
196 return Status::OK;
197 } else {
198 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
199 }
200 }
201};
202
203void RunServer() {
204 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800205 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800206 TestServiceImpl service;
207
208 SimpleRequest request;
209 SimpleResponse response;
210
211 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800212 builder.RegisterService(&service);
Craig Tiller42bc87c2015-02-23 08:50:19 -0800213 std::shared_ptr<ServerCredentials> creds = grpc::InsecureServerCredentials();
yangg06170572015-01-12 13:12:45 -0800214 if (FLAGS_enable_ssl) {
215 SslServerCredentialsOptions ssl_opts = {
Julien Boeuf8fbcc432015-01-15 16:44:13 -0800216 "", {{test_server1_key, test_server1_cert}}};
Craig Tiller42bc87c2015-02-23 08:50:19 -0800217 creds = grpc::SslServerCredentials(ssl_opts);
yangg06170572015-01-12 13:12:45 -0800218 }
Craig Tiller42bc87c2015-02-23 08:50:19 -0800219 builder.AddPort(server_address.str(), creds);
yangg06170572015-01-12 13:12:45 -0800220 std::unique_ptr<Server> server(builder.BuildAndStart());
221 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800222 while (!got_sigint) {
yangg06170572015-01-12 13:12:45 -0800223 std::this_thread::sleep_for(std::chrono::seconds(5));
224 }
225}
226
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800227static void sigint_handler(int x) { got_sigint = true; }
228
yangg06170572015-01-12 13:12:45 -0800229int main(int argc, char** argv) {
230 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100231 ParseCommandLineFlags(&argc, &argv, true);
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800232 signal(SIGINT, sigint_handler);
yangg06170572015-01-12 13:12:45 -0800233
234 GPR_ASSERT(FLAGS_port != 0);
235 RunServer();
236
237 grpc_shutdown();
238 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800239}