Debian Codegraph @

test/core/fling/server.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <signal.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <time.h>
23
24 #include <grpc/byte_buffer.h>
25 #include <grpc/grpc.h>
26 #include <grpc/grpc_security.h>
27 #include <grpc/slice.h>
28 #include <grpc/status.h>
29 #include <grpc/support/sync.h>
30 #ifndef _WIN32
31 /* This is for _exit() below, which is temporary. */
32 #include <unistd.h>
33 #endif
34
35 #include <string>
36
37 #include <grpc/support/log.h>
38 #include <grpc/support/time.h>
39
40 #include "src/core/lib/gprpp/host_port.h"
41 #include "test/core/end2end/data/ssl_test_data.h"
42 #include "test/core/util/cmdline.h"
43 #include "test/core/util/grpc_profiler.h"
44 #include "test/core/util/port.h"
45 #include "test/core/util/test_config.h"
46
47 static grpc_completion_queue* cq;
48 static grpc_server* server;
49 static grpc_call* call;
50 static grpc_call_details call_details;
51 static grpc_metadata_array request_metadata_recv;
52 static grpc_metadata_array initial_metadata_send;
53 static grpc_byte_buffer* payload_buffer = nullptr;
54 /* Used to drain the terminal read in unary calls. */
55 static grpc_byte_buffer* terminal_buffer = nullptr;
56
57 static grpc_op read_op;
58 static grpc_op metadata_send_op;
59 static grpc_op write_op;
60 static grpc_op status_op[2];
61 static int was_cancelled = 2;
62 static grpc_op unary_ops[6];
63 static int got_sigint = 0;
64
65 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
66
67 typedef enum {
68 FLING_SERVER_NEW_REQUEST = 1,
69 FLING_SERVER_READ_FOR_UNARY,
70 FLING_SERVER_BATCH_OPS_FOR_UNARY,
71 FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING,
72 FLING_SERVER_READ_FOR_STREAMING,
73 FLING_SERVER_WRITE_FOR_STREAMING,
74 FLING_SERVER_SEND_STATUS_FOR_STREAMING
75 } fling_server_tags;
76
77 typedef struct {
78 gpr_refcount pending_ops;
79 uint32_t flags;
80 } call_state;
81
82 static void request_call(void) {
83 grpc_metadata_array_init(&request_metadata_recv);
84 GPR_ASSERT(GRPC_CALL_OK ==
85 grpc_server_request_call(server, &call, &call_details,
86 &request_metadata_recv, cq, cq,
87 tag(FLING_SERVER_NEW_REQUEST)));
88 }
89
90 static void handle_unary_method(void) {
91 grpc_op* op;
92 grpc_call_error error;
93
94 grpc_metadata_array_init(&initial_metadata_send);
95
96 op = unary_ops;
97 op->op = GRPC_OP_SEND_INITIAL_METADATA;
98 op->data.send_initial_metadata.count = 0;
99 op++;
100 op->op = GRPC_OP_RECV_MESSAGE;
101 op->data.recv_message.recv_message = &terminal_buffer;
102 op++;
103 op->op = GRPC_OP_SEND_MESSAGE;
104 if (payload_buffer == nullptr) {
105 gpr_log(GPR_INFO, "NULL payload buffer !!!");
106 }
107 op->data.send_message.send_message = payload_buffer;
108 op++;
109 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
110 op->data.send_status_from_server.status = GRPC_STATUS_OK;
111 op->data.send_status_from_server.trailing_metadata_count = 0;
112 op->data.send_status_from_server.status_details = nullptr;
113 op++;
114 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
115 op->data.recv_close_on_server.cancelled = &was_cancelled;
116 op++;
117
118 error = grpc_call_start_batch(call, unary_ops,
119 static_cast<size_t>(op - unary_ops),
120 tag(FLING_SERVER_BATCH_OPS_FOR_UNARY), nullptr);
121 GPR_ASSERT(GRPC_CALL_OK == error);
122 }
123
124 static void send_initial_metadata(void) {
125 grpc_call_error error;
126 void* tagarg = tag(FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING);
127 grpc_metadata_array_init(&initial_metadata_send);
128 metadata_send_op.op = GRPC_OP_SEND_INITIAL_METADATA;
129 metadata_send_op.data.send_initial_metadata.count = 0;
130 error = grpc_call_start_batch(call, &metadata_send_op, 1, tagarg, nullptr);
131
132 GPR_ASSERT(GRPC_CALL_OK == error);
133 }
134
135 static void start_read_op(int t) {
136 grpc_call_error error;
137 /* Starting read at server */
138 read_op.op = GRPC_OP_RECV_MESSAGE;
139 read_op.data.recv_message.recv_message = &payload_buffer;
140 error = grpc_call_start_batch(call, &read_op, 1, tag(t), nullptr);
141 GPR_ASSERT(GRPC_CALL_OK == error);
142 }
143
144 static void start_write_op(void) {
145 grpc_call_error error;
146 void* tagarg = tag(FLING_SERVER_WRITE_FOR_STREAMING);
147 /* Starting write at server */
148 write_op.op = GRPC_OP_SEND_MESSAGE;
149 if (payload_buffer == nullptr) {
150 gpr_log(GPR_INFO, "NULL payload buffer !!!");
151 }
152 write_op.data.send_message.send_message = payload_buffer;
153 error = grpc_call_start_batch(call, &write_op, 1, tagarg, nullptr);
154 GPR_ASSERT(GRPC_CALL_OK == error);
155 }
156
157 static void start_send_status(void) {
158 grpc_call_error error;
159 void* tagarg = tag(FLING_SERVER_SEND_STATUS_FOR_STREAMING);
160 status_op[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER;
161 status_op[0].data.send_status_from_server.status = GRPC_STATUS_OK;
162 status_op[0].data.send_status_from_server.trailing_metadata_count = 0;
163 status_op[0].data.send_status_from_server.status_details = nullptr;
164 status_op[1].op = GRPC_OP_RECV_CLOSE_ON_SERVER;
165 status_op[1].data.recv_close_on_server.cancelled = &was_cancelled;
166
167 error = grpc_call_start_batch(call, status_op, 2, tagarg, nullptr);
168 GPR_ASSERT(GRPC_CALL_OK == error);
169 }
170
171 /* We have some sort of deadlock, so let's not exit gracefully for now.
172 When that is resolved, please remove the #include <unistd.h> above. */
173 static void sigint_handler(int /*x*/) { _exit(0); }
174
175 int main(int argc, char** argv) {
176 grpc_event ev;
177 call_state* s;
178 std::string addr_buf;
179 gpr_cmdline* cl;
180 grpc_completion_queue* shutdown_cq;
181 int shutdown_started = 0;
182 int shutdown_finished = 0;
183
184 int secure = 0;
185 const char* addr = nullptr;
186
187 char* fake_argv[1];
188
189 GPR_ASSERT(argc >= 1);
190 argc = 1;
191 fake_argv[0] = argv[0];
192 grpc_test_init(&argc, fake_argv);
193
194 grpc_init();
195 srand(static_cast<unsigned>(clock()));
196
197 cl = gpr_cmdline_create("fling server");
198 gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
199 gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
200 gpr_cmdline_parse(cl, argc, argv);
201 gpr_cmdline_destroy(cl);
202
203 if (addr == nullptr) {
204 addr_buf = grpc_core::JoinHostPort("::", grpc_pick_unused_port_or_die());
205 addr = addr_buf.c_str();
206 }
207 gpr_log(GPR_INFO, "creating server on: %s", addr);
208
209 cq = grpc_completion_queue_create_for_next(nullptr);
210 grpc_server_credentials* creds;
211 if (secure) {
212 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
213 test_server1_cert};
214 creds = grpc_ssl_server_credentials_create(nullptr, &pem_key_cert_pair, 1,
215 0, nullptr);
216 } else {
217 creds = grpc_insecure_server_credentials_create();
218 }
219 server = grpc_server_create(nullptr, nullptr);
220 GPR_ASSERT(grpc_server_add_http2_port(server, addr, creds));
221 grpc_server_credentials_release(creds);
222 grpc_server_register_completion_queue(server, cq, nullptr);
223 grpc_server_start(server);
224
225 addr = nullptr;
226 addr_buf.clear();
227
228 grpc_call_details_init(&call_details);
229
230 request_call();
231
232 grpc_profiler_start("server.prof");
233 signal(SIGINT, sigint_handler);
234 while (!shutdown_finished) {
235 if (got_sigint && !shutdown_started) {
236 gpr_log(GPR_INFO, "Shutting down due to SIGINT");
237
238 shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
239 grpc_server_shutdown_and_notify(server, shutdown_cq, tag(1000));
240
241 GPR_ASSERT(grpc_completion_queue_pluck(
242 shutdown_cq, tag(1000),
243 grpc_timeout_seconds_to_deadline(5), nullptr)
244 .type == GRPC_OP_COMPLETE);
245 grpc_completion_queue_destroy(shutdown_cq);
246
247 grpc_completion_queue_shutdown(cq);
248 shutdown_started = 1;
249 }
250 ev = grpc_completion_queue_next(
251 cq,
252 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
253 gpr_time_from_micros(1000000, GPR_TIMESPAN)),
254 nullptr);
255 s = static_cast<call_state*>(ev.tag);
256 switch (ev.type) {
257 case GRPC_OP_COMPLETE:
258 switch (reinterpret_cast<intptr_t>(s)) {
259 case FLING_SERVER_NEW_REQUEST:
260 if (call != nullptr) {
261 if (0 == grpc_slice_str_cmp(call_details.method,
262 "/Reflector/reflectStream")) {
263 /* Received streaming call. Send metadata here. */
264 start_read_op(FLING_SERVER_READ_FOR_STREAMING);
265 send_initial_metadata();
266 } else {
267 /* Received unary call. Can do all ops in one batch. */
268 start_read_op(FLING_SERVER_READ_FOR_UNARY);
269 }
270 } else {
271 GPR_ASSERT(shutdown_started);
272 }
273 /* request_call();
274 */
275 break;
276 case FLING_SERVER_READ_FOR_STREAMING:
277 if (payload_buffer != nullptr) {
278 /* Received payload from client. */
279 start_write_op();
280 } else {
281 /* Received end of stream from client. */
282 start_send_status();
283 }
284 break;
285 case FLING_SERVER_WRITE_FOR_STREAMING:
286 /* Write completed at server */
287 grpc_byte_buffer_destroy(payload_buffer);
288 payload_buffer = nullptr;
289 start_read_op(FLING_SERVER_READ_FOR_STREAMING);
290 break;
291 case FLING_SERVER_SEND_INIT_METADATA_FOR_STREAMING:
292 /* Metadata send completed at server */
293 break;
294 case FLING_SERVER_SEND_STATUS_FOR_STREAMING:
295 /* Send status and close completed at server */
296 grpc_call_unref(call);
297 if (!shutdown_started) request_call();
298 break;
299 case FLING_SERVER_READ_FOR_UNARY:
300 /* Finished payload read for unary. Start all reamaining
301 * unary ops in a batch.
302 */
303 handle_unary_method();
304 break;
305 case FLING_SERVER_BATCH_OPS_FOR_UNARY:
306 /* Finished unary call. */
307 grpc_byte_buffer_destroy(payload_buffer);
308 payload_buffer = nullptr;
309 grpc_call_unref(call);
310 if (!shutdown_started) request_call();
311 break;
312 }
313 break;
314 case GRPC_QUEUE_SHUTDOWN:
315 GPR_ASSERT(shutdown_started);
316 shutdown_finished = 1;
317 break;
318 case GRPC_QUEUE_TIMEOUT:
319 break;
320 }
321 }
322 grpc_profiler_stop();
323 grpc_call_details_destroy(&call_details);
324
325 grpc_server_destroy(server);
326 grpc_completion_queue_destroy(cq);
327 grpc_shutdown();
328 return 0;
329 }