blob: 8566ad5bef4c19e33aaac9f53db8c1a84d2c3c18 [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
76bool SetPayload(PayloadType type, int size, Payload* payload) {
77 PayloadType response_type = type;
78 // TODO(yangg): Support UNCOMPRESSABLE payload.
79 if (type != PayloadType::COMPRESSABLE) {
80 return false;
81 }
82 payload->set_type(response_type);
83 std::unique_ptr<char[]> body(new char[size]());
84 payload->set_body(body.get(), size);
85 return true;
86}
87
88class TestServiceImpl : public TestService::Service {
89 public:
90 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
91 grpc::testing::Empty* response) {
92 return Status::OK;
93 }
94
95 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
96 SimpleResponse* response) {
97 if (request->has_response_size() && request->response_size() > 0) {
98 if (!SetPayload(request->response_type(), request->response_size(),
99 response->mutable_payload())) {
100 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
101 }
102 }
103 return Status::OK;
104 }
105
106 Status StreamingOutputCall(
107 ServerContext* context, const StreamingOutputCallRequest* request,
108 ServerWriter<StreamingOutputCallResponse>* writer) {
109 StreamingOutputCallResponse response;
110 bool write_success = true;
111 response.mutable_payload()->set_type(request->response_type());
112 for (int i = 0; write_success && i < request->response_parameters_size();
113 i++) {
114 response.mutable_payload()->set_body(
115 grpc::string(request->response_parameters(i).size(), '\0'));
116 write_success = writer->Write(response);
117 }
118 if (write_success) {
119 return Status::OK;
120 } else {
121 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
122 }
123 }
124
125 Status StreamingInputCall(ServerContext* context,
126 ServerReader<StreamingInputCallRequest>* reader,
127 StreamingInputCallResponse* response) {
128 StreamingInputCallRequest request;
129 int aggregated_payload_size = 0;
130 while (reader->Read(&request)) {
131 if (request.has_payload() && request.payload().has_body()) {
132 aggregated_payload_size += request.payload().body().size();
133 }
134 }
135 response->set_aggregated_payload_size(aggregated_payload_size);
136 return Status::OK;
137 }
138
139 Status FullDuplexCall(
140 ServerContext* context,
141 ServerReaderWriter<StreamingOutputCallResponse,
142 StreamingOutputCallRequest>* stream) {
143 StreamingOutputCallRequest request;
144 StreamingOutputCallResponse response;
145 bool write_success = true;
146 while (write_success && stream->Read(&request)) {
147 response.mutable_payload()->set_type(request.payload().type());
148 if (request.response_parameters_size() == 0) {
149 return Status(grpc::StatusCode::INTERNAL,
150 "Request does not have response parameters.");
151 }
152 response.mutable_payload()->set_body(
153 grpc::string(request.response_parameters(0).size(), '\0'));
154 write_success = stream->Write(response);
155 }
156 if (write_success) {
157 return Status::OK;
158 } else {
159 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
160 }
161 }
162
163 Status HalfDuplexCall(
164 ServerContext* context,
165 ServerReaderWriter<StreamingOutputCallResponse,
166 StreamingOutputCallRequest>* stream) {
167 std::vector<StreamingOutputCallRequest> requests;
168 StreamingOutputCallRequest request;
169 while (stream->Read(&request)) {
170 requests.push_back(request);
171 }
172
173 StreamingOutputCallResponse response;
174 bool write_success = true;
175 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
176 response.mutable_payload()->set_type(requests[i].payload().type());
177 if (requests[i].response_parameters_size() == 0) {
178 return Status(grpc::StatusCode::INTERNAL,
179 "Request does not have response parameters.");
180 }
181 response.mutable_payload()->set_body(
182 grpc::string(requests[i].response_parameters(0).size(), '\0'));
183 write_success = stream->Write(response);
184 }
185 if (write_success) {
186 return Status::OK;
187 } else {
188 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
189 }
190 }
191};
192
193void RunServer() {
194 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800195 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800196 TestServiceImpl service;
197
198 SimpleRequest request;
199 SimpleResponse response;
200
201 ServerBuilder builder;
202 builder.AddPort(server_address.str());
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800203 builder.RegisterService(&service);
yangg06170572015-01-12 13:12:45 -0800204 if (FLAGS_enable_ssl) {
205 SslServerCredentialsOptions ssl_opts = {
Julien Boeuf8fbcc432015-01-15 16:44:13 -0800206 "", {{test_server1_key, test_server1_cert}}};
yangg06170572015-01-12 13:12:45 -0800207 std::shared_ptr<ServerCredentials> creds =
208 ServerCredentialsFactory::SslCredentials(ssl_opts);
209 builder.SetCredentials(creds);
210 }
211 std::unique_ptr<Server> server(builder.BuildAndStart());
212 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
213 while (true) {
214 std::this_thread::sleep_for(std::chrono::seconds(5));
215 }
216}
217
218int main(int argc, char** argv) {
219 grpc_init();
220 google::ParseCommandLineFlags(&argc, &argv, true);
221
222 GPR_ASSERT(FLAGS_port != 0);
223 RunServer();
224
225 grpc_shutdown();
226 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800227}