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