blob: 22b8910a249d8fc39310ee1ce760224fce2cba5b [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>
yangg06170572015-01-12 13:12:45 -080044#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>
Abhishek Kumar1b3e3cd2015-04-16 20:10:29 -070051#include "test/proto/test.grpc.pb.h"
Abhishek Kumar60572d42015-04-16 20:45:25 -070052#include "test/proto/empty.grpc.pb.h"
53#include "test/proto/messages.grpc.pb.h"
Yang Gaoa4002072015-04-09 23:25:21 -070054#include "test/cpp/interop/server_helper.h"
Yang Gao103837e2015-04-15 15:23:54 -070055#include "test/cpp/util/test_config.h"
yangg06170572015-01-12 13:12:45 -080056
57DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
58DEFINE_int32(port, 0, "Server port.");
59
60using grpc::Server;
61using grpc::ServerBuilder;
62using grpc::ServerContext;
63using grpc::ServerCredentials;
yangg06170572015-01-12 13:12:45 -080064using 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
Craig Tiller2f3e2ec2015-02-20 13:07:50 -080079static bool got_sigint = false;
80
yangg06170572015-01-12 13:12:45 -080081bool SetPayload(PayloadType type, int size, Payload* payload) {
82 PayloadType response_type = type;
83 // TODO(yangg): Support UNCOMPRESSABLE payload.
84 if (type != PayloadType::COMPRESSABLE) {
85 return false;
86 }
87 payload->set_type(response_type);
88 std::unique_ptr<char[]> body(new char[size]());
89 payload->set_body(body.get(), size);
90 return true;
91}
92
93class TestServiceImpl : public TestService::Service {
94 public:
95 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
96 grpc::testing::Empty* response) {
97 return Status::OK;
98 }
99
100 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
101 SimpleResponse* response) {
102 if (request->has_response_size() && request->response_size() > 0) {
103 if (!SetPayload(request->response_type(), request->response_size(),
104 response->mutable_payload())) {
105 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
106 }
107 }
108 return Status::OK;
109 }
110
111 Status StreamingOutputCall(
112 ServerContext* context, const StreamingOutputCallRequest* request,
113 ServerWriter<StreamingOutputCallResponse>* writer) {
114 StreamingOutputCallResponse response;
115 bool write_success = true;
116 response.mutable_payload()->set_type(request->response_type());
117 for (int i = 0; write_success && i < request->response_parameters_size();
118 i++) {
119 response.mutable_payload()->set_body(
120 grpc::string(request->response_parameters(i).size(), '\0'));
121 write_success = writer->Write(response);
122 }
123 if (write_success) {
124 return Status::OK;
125 } else {
126 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
127 }
128 }
129
130 Status StreamingInputCall(ServerContext* context,
131 ServerReader<StreamingInputCallRequest>* reader,
132 StreamingInputCallResponse* response) {
133 StreamingInputCallRequest request;
134 int aggregated_payload_size = 0;
135 while (reader->Read(&request)) {
136 if (request.has_payload() && request.payload().has_body()) {
137 aggregated_payload_size += request.payload().body().size();
138 }
139 }
140 response->set_aggregated_payload_size(aggregated_payload_size);
141 return Status::OK;
142 }
143
144 Status FullDuplexCall(
145 ServerContext* context,
146 ServerReaderWriter<StreamingOutputCallResponse,
147 StreamingOutputCallRequest>* stream) {
148 StreamingOutputCallRequest request;
149 StreamingOutputCallResponse response;
150 bool write_success = true;
151 while (write_success && stream->Read(&request)) {
152 response.mutable_payload()->set_type(request.payload().type());
153 if (request.response_parameters_size() == 0) {
154 return Status(grpc::StatusCode::INTERNAL,
155 "Request does not have response parameters.");
156 }
157 response.mutable_payload()->set_body(
158 grpc::string(request.response_parameters(0).size(), '\0'));
159 write_success = stream->Write(response);
160 }
161 if (write_success) {
162 return Status::OK;
163 } else {
164 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
165 }
166 }
167
168 Status HalfDuplexCall(
169 ServerContext* context,
170 ServerReaderWriter<StreamingOutputCallResponse,
171 StreamingOutputCallRequest>* stream) {
172 std::vector<StreamingOutputCallRequest> requests;
173 StreamingOutputCallRequest request;
174 while (stream->Read(&request)) {
175 requests.push_back(request);
176 }
177
178 StreamingOutputCallResponse response;
179 bool write_success = true;
180 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
181 response.mutable_payload()->set_type(requests[i].payload().type());
182 if (requests[i].response_parameters_size() == 0) {
183 return Status(grpc::StatusCode::INTERNAL,
184 "Request does not have response parameters.");
185 }
186 response.mutable_payload()->set_body(
187 grpc::string(requests[i].response_parameters(0).size(), '\0'));
188 write_success = stream->Write(response);
189 }
190 if (write_success) {
191 return Status::OK;
192 } else {
193 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
194 }
195 }
196};
197
198void RunServer() {
199 std::ostringstream server_address;
Yang Gaodab70952015-01-23 16:06:36 -0800200 server_address << "0.0.0.0:" << FLAGS_port;
yangg06170572015-01-12 13:12:45 -0800201 TestServiceImpl service;
202
203 SimpleRequest request;
204 SimpleResponse response;
205
206 ServerBuilder builder;
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800207 builder.RegisterService(&service);
Yang Gaoa4002072015-04-09 23:25:21 -0700208 std::shared_ptr<ServerCredentials> creds =
209 grpc::testing::CreateInteropServerCredentials();
Nicolas Noblecfd60732015-03-18 16:27:43 -0700210 builder.AddListeningPort(server_address.str(), creds);
yangg06170572015-01-12 13:12:45 -0800211 std::unique_ptr<Server> server(builder.BuildAndStart());
212 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800213 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800214 sleep(5);
yangg06170572015-01-12 13:12:45 -0800215 }
216}
217
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800218static void sigint_handler(int x) { got_sigint = true; }
219
yangg06170572015-01-12 13:12:45 -0800220int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -0700221 grpc::testing::InitTest(&argc, &argv, true);
Craig Tiller2f3e2ec2015-02-20 13:07:50 -0800222 signal(SIGINT, sigint_handler);
yangg06170572015-01-12 13:12:45 -0800223
224 GPR_ASSERT(FLAGS_port != 0);
225 RunServer();
226
yangg06170572015-01-12 13:12:45 -0800227 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800228}