blob: bc84645a0405572d4a9a122a42c761095a614797 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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 <grpc/grpc.h>
Craig Tiller39484122015-01-21 13:37:23 -080035#include <grpc/grpc_http.h>
Craig Tiller8a84be32015-01-21 12:29:41 -080036#include <grpc/grpc_security.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037
nnoble0c475f02014-12-05 15:37:39 -080038#include <signal.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <time.h>
43
Craig Tiller485d7762015-01-23 12:54:05 -080044#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045#include "test/core/util/test_config.h"
46#include <grpc/support/alloc.h>
Craig Tiller8a84be32015-01-21 12:29:41 -080047#include <grpc/support/cmdline.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048#include <grpc/support/host_port.h>
49#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050#include <grpc/support/time.h>
51#include "test/core/util/port.h"
Craig Tiller8a84be32015-01-21 12:29:41 -080052#include "test/core/end2end/data/ssl_test_data.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
54static grpc_completion_queue *cq;
55static grpc_server *server;
nnoble0c475f02014-12-05 15:37:39 -080056static int got_sigint = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058typedef struct {
59 gpr_refcount pending_ops;
60 gpr_intmax bytes_read;
61} call_state;
62
Craig Tiller32946d32015-01-15 11:37:30 -080063static void request_call(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064 call_state *tag = gpr_malloc(sizeof(*tag));
65 gpr_ref_init(&tag->pending_ops, 2);
66 tag->bytes_read = 0;
Craig Tillerc4f0ebe2015-02-02 10:16:30 -080067 grpc_server_request_call_old(server, tag);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068}
69
70static void assert_read_ok(call_state *s, grpc_byte_buffer *b) {
71 grpc_byte_buffer_reader *bb_reader = NULL;
72 gpr_slice read_slice;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010073 unsigned i;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074
75 bb_reader = grpc_byte_buffer_reader_create(b);
76 while (grpc_byte_buffer_reader_next(bb_reader, &read_slice)) {
77 for (i = 0; i < GPR_SLICE_LENGTH(read_slice); i++) {
78 GPR_ASSERT(GPR_SLICE_START_PTR(read_slice)[i] == s->bytes_read % 256);
79 s->bytes_read++;
80 }
81 gpr_slice_unref(read_slice);
82 }
83 grpc_byte_buffer_reader_destroy(bb_reader);
84}
85
nnoble0c475f02014-12-05 15:37:39 -080086static void sigint_handler(int x) { got_sigint = 1; }
87
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088int main(int argc, char **argv) {
89 grpc_event *ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 call_state *s;
Craig Tiller8a84be32015-01-21 12:29:41 -080091 char *addr_buf = NULL;
92 gpr_cmdline *cl;
nnoble0c475f02014-12-05 15:37:39 -080093 int shutdown_started = 0;
94 int shutdown_finished = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Craig Tiller8a84be32015-01-21 12:29:41 -080096 int secure = 0;
97 char *addr = NULL;
98
99 char *fake_argv[1];
100
Craig Tiller39484122015-01-21 13:37:23 -0800101#define MAX_ARGS 4
102 grpc_arg arge[MAX_ARGS];
103 grpc_arg *e;
104 grpc_channel_args args = {0, NULL};
105
106 grpc_http_server_page home_page = {"/", "text/html",
Craig Tiller59963412015-01-21 14:04:01 -0800107 "<head>\n"
108 "<title>Echo Server</title>\n"
109 "</head>\n"
110 "<body>\n"
111 "Welcome to the world of the future!\n"
112 "</body>\n"};
Craig Tiller39484122015-01-21 13:37:23 -0800113
Craig Tiller8a84be32015-01-21 12:29:41 -0800114 GPR_ASSERT(argc >= 1);
115 fake_argv[0] = argv[0];
116 grpc_test_init(1, fake_argv);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117
118 grpc_init();
119 srand(clock());
Craig Tiller39484122015-01-21 13:37:23 -0800120 memset(arge, 0, sizeof(arge));
121 args.args = arge;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122
Craig Tiller8a84be32015-01-21 12:29:41 -0800123 cl = gpr_cmdline_create("echo server");
124 gpr_cmdline_add_string(cl, "bind", "Bind host:port", &addr);
125 gpr_cmdline_add_flag(cl, "secure", "Run with security?", &secure);
126 gpr_cmdline_parse(cl, argc, argv);
127 gpr_cmdline_destroy(cl);
128
Craig Tiller39484122015-01-21 13:37:23 -0800129 e = &arge[args.num_args++];
130 e->type = GRPC_ARG_POINTER;
131 e->key = GRPC_ARG_SERVE_OVER_HTTP;
132 e->value.pointer.p = &home_page;
133
Craig Tiller8a84be32015-01-21 12:29:41 -0800134 if (addr == NULL) {
135 gpr_join_host_port(&addr_buf, "::", grpc_pick_unused_port_or_die());
136 addr = addr_buf;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800137 }
138 gpr_log(GPR_INFO, "creating server on: %s", addr);
139
140 cq = grpc_completion_queue_create();
Craig Tiller8a84be32015-01-21 12:29:41 -0800141 if (secure) {
Craig Tiller0a207712015-01-23 08:12:38 -0800142 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {test_server1_key,
143 test_server1_cert};
144 grpc_server_credentials *ssl_creds =
145 grpc_ssl_server_credentials_create(NULL, &pem_key_cert_pair, 1);
Craig Tiller103481e2015-01-27 10:18:04 -0800146 server = grpc_secure_server_create(ssl_creds, cq, &args);
Craig Tiller8a84be32015-01-21 12:29:41 -0800147 GPR_ASSERT(grpc_server_add_secure_http2_port(server, addr));
148 grpc_server_credentials_release(ssl_creds);
149 } else {
Craig Tiller103481e2015-01-27 10:18:04 -0800150 server = grpc_server_create(cq, &args);
Craig Tiller8a84be32015-01-21 12:29:41 -0800151 GPR_ASSERT(grpc_server_add_http2_port(server, addr));
152 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800153 grpc_server_start(server);
154
Craig Tiller8a84be32015-01-21 12:29:41 -0800155 gpr_free(addr_buf);
156 addr = addr_buf = NULL;
157
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800158 request_call();
159
nnoble0c475f02014-12-05 15:37:39 -0800160 signal(SIGINT, sigint_handler);
161 while (!shutdown_finished) {
162 if (got_sigint && !shutdown_started) {
163 gpr_log(GPR_INFO, "Shutting down due to SIGINT");
164 grpc_server_shutdown(server);
165 grpc_completion_queue_shutdown(cq);
166 shutdown_started = 1;
167 }
Craig Tillerc1f11622015-02-25 09:09:59 -0800168 ev = grpc_completion_queue_next(
169 cq, gpr_time_add(gpr_now(), gpr_time_from_seconds(1)));
nnoble0c475f02014-12-05 15:37:39 -0800170 if (!ev) continue;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800171 s = ev->tag;
172 switch (ev->type) {
173 case GRPC_SERVER_RPC_NEW:
nnoble0c475f02014-12-05 15:37:39 -0800174 if (ev->call != NULL) {
175 /* initial ops are already started in request_call */
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800176 grpc_call_server_accept_old(ev->call, cq, s);
177 grpc_call_server_end_initial_metadata_old(ev->call,
178 GRPC_WRITE_BUFFER_HINT);
179 GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK);
nnoble0c475f02014-12-05 15:37:39 -0800180 request_call();
181 } else {
182 GPR_ASSERT(shutdown_started);
183 gpr_free(s);
184 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185 break;
186 case GRPC_WRITE_ACCEPTED:
187 GPR_ASSERT(ev->data.write_accepted == GRPC_OP_OK);
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800188 GPR_ASSERT(grpc_call_start_read_old(ev->call, s) == GRPC_CALL_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800189 break;
190 case GRPC_READ:
191 if (ev->data.read) {
192 assert_read_ok(ev->tag, ev->data.read);
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800193 GPR_ASSERT(grpc_call_start_write_old(ev->call, ev->data.read, s,
194 GRPC_WRITE_BUFFER_HINT) ==
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800195 GRPC_CALL_OK);
196 } else {
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800197 GPR_ASSERT(grpc_call_start_write_status_old(ev->call, GRPC_STATUS_OK,
198 NULL, s) == GRPC_CALL_OK);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 }
200 break;
201 case GRPC_FINISH_ACCEPTED:
202 case GRPC_FINISHED:
203 if (gpr_unref(&s->pending_ops)) {
204 grpc_call_destroy(ev->call);
205 gpr_free(s);
206 }
207 break;
nnoble0c475f02014-12-05 15:37:39 -0800208 case GRPC_QUEUE_SHUTDOWN:
209 GPR_ASSERT(shutdown_started);
210 shutdown_finished = 1;
211 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212 default:
nnoble0c475f02014-12-05 15:37:39 -0800213 GPR_ASSERT(0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800214 }
215 grpc_event_finish(ev);
216 }
217
nnoble0c475f02014-12-05 15:37:39 -0800218 grpc_server_destroy(server);
219 grpc_completion_queue_destroy(cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220 grpc_shutdown();
221
222 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800223}