Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: third_party/grpc/test/cpp/interop/server_main.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * 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 <signal.h>
35 #include <unistd.h>
36
37 #include <fstream>
38 #include <memory>
39 #include <sstream>
40 #include <thread>
41
42 #include <gflags/gflags.h>
43 #include <grpc/grpc.h>
44 #include <grpc/support/log.h>
45 #include <grpc/support/useful.h>
46 #include <grpc++/server.h>
47 #include <grpc++/server_builder.h>
48 #include <grpc++/server_context.h>
49 #include <grpc++/security/server_credentials.h>
50
51 #include "test/cpp/interop/server_helper.h"
52 #include "test/cpp/util/test_config.h"
53 #include "src/proto/grpc/testing/test.grpc.pb.h"
54 #include "src/proto/grpc/testing/empty.grpc.pb.h"
55 #include "src/proto/grpc/testing/messages.grpc.pb.h"
56
57 DEFINE_bool(use_tls, false, "Whether to use tls.");
58 DEFINE_int32(port, 0, "Server port.");
59
60 using grpc::Server;
61 using grpc::ServerBuilder;
62 using grpc::ServerContext;
63 using grpc::ServerCredentials;
64 using grpc::ServerReader;
65 using grpc::ServerReaderWriter;
66 using grpc::ServerWriter;
67 using grpc::SslServerCredentialsOptions;
68 using grpc::testing::InteropServerContextInspector;
69 using grpc::testing::Payload;
70 using grpc::testing::PayloadType;
71 using grpc::testing::SimpleRequest;
72 using grpc::testing::SimpleResponse;
73 using grpc::testing::StreamingInputCallRequest;
74 using grpc::testing::StreamingInputCallResponse;
75 using grpc::testing::StreamingOutputCallRequest;
76 using grpc::testing::StreamingOutputCallResponse;
77 using grpc::testing::TestService;
78 using grpc::Status;
79
80 static bool got_sigint = false;
81 static const char* kRandomFile = "test/cpp/interop/rnd.dat";
82
83 const char kEchoInitialMetadataKey[] = "x-grpc-test-echo-initial";
84 const char kEchoTrailingBinMetadataKey[] = "x-grpc-test-echo-trailing-bin";
85 const char kEchoUserAgentKey[] = "x-grpc-test-echo-useragent";
86
87 void MaybeEchoMetadata(ServerContext* context) {
88 const auto& client_metadata = context->client_metadata();
89 GPR_ASSERT(client_metadata.count(kEchoInitialMetadataKey) <= 1);
90 GPR_ASSERT(client_metadata.count(kEchoTrailingBinMetadataKey) <= 1);
91
92 auto iter = client_metadata.find(kEchoInitialMetadataKey);
93 if (iter != client_metadata.end()) {
94 context->AddInitialMetadata(kEchoInitialMetadataKey, iter->second.data());
95 }
96 iter = client_metadata.find(kEchoTrailingBinMetadataKey);
97 if (iter != client_metadata.end()) {
98 context->AddTrailingMetadata(
99 kEchoTrailingBinMetadataKey,
100 grpc::string(iter->second.begin(), iter->second.end()));
101 }
102 // Check if client sent a magic key in the header that makes us echo
103 // back the user-agent (for testing purpose)
104 iter = client_metadata.find(kEchoUserAgentKey);
105 if (iter != client_metadata.end()) {
106 iter = client_metadata.find("user-agent");
107 if (iter != client_metadata.end()) {
108 context->AddInitialMetadata(kEchoUserAgentKey, iter->second.data());
109 }
110 }
111 }
112
113 bool SetPayload(PayloadType type, int size, Payload* payload) {
114 PayloadType response_type;
115 if (type == PayloadType::RANDOM) {
116 response_type =
117 rand() & 0x1 ? PayloadType::COMPRESSABLE : PayloadType::UNCOMPRESSABLE;
118 } else {
119 response_type = type;
120 }
121 payload->set_type(response_type);
122 switch (response_type) {
123 case PayloadType::COMPRESSABLE: {
124 std::unique_ptr<char[]> body(new char[size]());
125 payload->set_body(body.get(), size);
126 } break;
127 case PayloadType::UNCOMPRESSABLE: {
128 std::unique_ptr<char[]> body(new char[size]());
129 std::ifstream rnd_file(kRandomFile);
130 GPR_ASSERT(rnd_file.good());
131 rnd_file.read(body.get(), size);
132 GPR_ASSERT(!rnd_file.eof()); // Requested more rnd bytes than available
133 payload->set_body(body.get(), size);
134 } break;
135 default:
136 GPR_ASSERT(false);
137 }
138 return true;
139 }
140
141 template <typename RequestType>
142 void SetResponseCompression(ServerContext* context,
143 const RequestType& request) {
144 switch (request.response_compression()) {
145 case grpc::testing::NONE:
146 context->set_compression_algorithm(GRPC_COMPRESS_NONE);
147 break;
148 case grpc::testing::GZIP:
149 context->set_compression_algorithm(GRPC_COMPRESS_GZIP);
150 break;
151 case grpc::testing::DEFLATE:
152 context->set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
153 break;
154 default:
155 abort();
156 }
157 }
158
159 class TestServiceImpl : public TestService::Service {
160 public:
161 Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
162 grpc::testing::Empty* response) {
163 MaybeEchoMetadata(context);
164 return Status::OK;
165 }
166
167 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
168 SimpleResponse* response) {
169 MaybeEchoMetadata(context);
170 SetResponseCompression(context, *request);
171 if (request->response_size() > 0) {
172 if (!SetPayload(request->response_type(), request->response_size(),
173 response->mutable_payload())) {
174 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
175 }
176 }
177
178 if (request->has_response_status()) {
179 return Status(
180 static_cast<grpc::StatusCode>(request->response_status().code()),
181 request->response_status().message());
182 }
183
184 return Status::OK;
185 }
186
187 Status StreamingOutputCall(
188 ServerContext* context, const StreamingOutputCallRequest* request,
189 ServerWriter<StreamingOutputCallResponse>* writer) {
190 SetResponseCompression(context, *request);
191 StreamingOutputCallResponse response;
192 bool write_success = true;
193 for (int i = 0; write_success && i < request->response_parameters_size();
194 i++) {
195 if (!SetPayload(request->response_type(),
196 request->response_parameters(i).size(),
197 response.mutable_payload())) {
198 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
199 }
200 write_success = writer->Write(response);
201 }
202 if (write_success) {
203 return Status::OK;
204 } else {
205 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
206 }
207 }
208
209 Status StreamingInputCall(ServerContext* context,
210 ServerReader<StreamingInputCallRequest>* reader,
211 StreamingInputCallResponse* response) {
212 StreamingInputCallRequest request;
213 int aggregated_payload_size = 0;
214 while (reader->Read(&request)) {
215 if (request.has_payload()) {
216 aggregated_payload_size += request.payload().body().size();
217 }
218 }
219 response->set_aggregated_payload_size(aggregated_payload_size);
220 return Status::OK;
221 }
222
223 Status FullDuplexCall(
224 ServerContext* context,
225 ServerReaderWriter<StreamingOutputCallResponse,
226 StreamingOutputCallRequest>* stream) {
227 MaybeEchoMetadata(context);
228 StreamingOutputCallRequest request;
229 StreamingOutputCallResponse response;
230 bool write_success = true;
231 while (write_success && stream->Read(&request)) {
232 SetResponseCompression(context, request);
233 if (request.response_parameters_size() != 0) {
234 response.mutable_payload()->set_type(request.payload().type());
235 response.mutable_payload()->set_body(
236 grpc::string(request.response_parameters(0).size(), '\0'));
237 write_success = stream->Write(response);
238 }
239 }
240 if (write_success) {
241 return Status::OK;
242 } else {
243 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
244 }
245 }
246
247 Status HalfDuplexCall(
248 ServerContext* context,
249 ServerReaderWriter<StreamingOutputCallResponse,
250 StreamingOutputCallRequest>* stream) {
251 std::vector<StreamingOutputCallRequest> requests;
252 StreamingOutputCallRequest request;
253 while (stream->Read(&request)) {
254 requests.push_back(request);
255 }
256
257 StreamingOutputCallResponse response;
258 bool write_success = true;
259 for (unsigned int i = 0; write_success && i < requests.size(); i++) {
260 response.mutable_payload()->set_type(requests[i].payload().type());
261 if (requests[i].response_parameters_size() == 0) {
262 return Status(grpc::StatusCode::INTERNAL,
263 "Request does not have response parameters.");
264 }
265 response.mutable_payload()->set_body(
266 grpc::string(requests[i].response_parameters(0).size(), '\0'));
267 write_success = stream->Write(response);
268 }
269 if (write_success) {
270 return Status::OK;
271 } else {
272 return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
273 }
274 }
275 };
276
277 void RunServer() {
278 std::ostringstream server_address;
279 server_address << "0.0.0.0:" << FLAGS_port;
280 TestServiceImpl service;
281
282 SimpleRequest request;
283 SimpleResponse response;
284
285 ServerBuilder builder;
286 builder.RegisterService(&service);
287 std::shared_ptr<ServerCredentials> creds =
288 grpc::testing::CreateInteropServerCredentials();
289 builder.AddListeningPort(server_address.str(), creds);
290 std::unique_ptr<Server> server(builder.BuildAndStart());
291 gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
292 while (!got_sigint) {
293 sleep(5);
294 }
295 }
296
297 static void sigint_handler(int x) { got_sigint = true; }
298
299 int main(int argc, char** argv) {
300 grpc::testing::InitTest(&argc, &argv, true);
301 signal(SIGINT, sigint_handler);
302
303 GPR_ASSERT(FLAGS_port != 0);
304 RunServer();
305
306 return 0;
307 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/interop/server_helper.cc ('k') | third_party/grpc/test/cpp/interop/stress_interop_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698