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