blob: 7f41d3f894a9d1a3367003adc4b2bef045c8b6d1 [file] [log] [blame]
Roger Meier15df0762014-09-29 20:50:56 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <glib-object.h>
21#include <signal.h>
22#include <stdio.h>
23#include <string.h>
24
25#include <thrift/c_glib/thrift.h>
26#include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h>
Chandler May6dde90b2016-01-10 06:01:10 +000027#include <thrift/c_glib/protocol/thrift_compact_protocol_factory.h>
Roger Meier15df0762014-09-29 20:50:56 +020028#include <thrift/c_glib/server/thrift_server.h>
29#include <thrift/c_glib/server/thrift_simple_server.h>
30#include <thrift/c_glib/transport/thrift_buffered_transport.h>
31#include <thrift/c_glib/transport/thrift_buffered_transport_factory.h>
32#include <thrift/c_glib/transport/thrift_framed_transport.h>
33#include <thrift/c_glib/transport/thrift_framed_transport_factory.h>
34#include <thrift/c_glib/transport/thrift_server_socket.h>
35#include <thrift/c_glib/transport/thrift_server_transport.h>
36#include <thrift/c_glib/transport/thrift_transport.h>
37#include <thrift/c_glib/transport/thrift_transport_factory.h>
38
39#include "../gen-c_glib/t_test_thrift_test.h"
40
41#include "thrift_test_handler.h"
42
43/* Our server object, declared globally so it is accessible within the SIGINT
44 signal handler */
45ThriftServer *server = NULL;
46
47/* A flag that indicates whether the server was interrupted with SIGINT
48 (i.e. Ctrl-C) so we can tell whether its termination was abnormal */
49gboolean sigint_received = FALSE;
50
51/* Handle SIGINT ("Ctrl-C") signals by gracefully stopping the server */
52static void
53sigint_handler (int signal_number)
54{
55 THRIFT_UNUSED_VAR (signal_number);
56
57 /* Take note we were called */
58 sigint_received = TRUE;
59
60 /* Shut down the server gracefully */
61 if (server != NULL)
62 thrift_server_stop (server);
63}
64
65int
66main (int argc, char **argv)
67{
68 static gint port = 9090;
69 static gchar *server_type_option = NULL;
70 static gchar *transport_option = NULL;
71 static gchar *protocol_option = NULL;
Chandler May6dde90b2016-01-10 06:01:10 +000072 static gint string_limit = 0;
73 static gint container_limit = 0;
Roger Meier15df0762014-09-29 20:50:56 +020074
75 static
76 GOptionEntry option_entries[] = {
77 { "port", 0, 0, G_OPTION_ARG_INT, &port,
78 "Port number to connect (=9090)", NULL },
79 { "server-type", 0, 0, G_OPTION_ARG_STRING, &server_type_option,
80 "Type of server: simple (=simple)", NULL },
81 { "transport", 0, 0, G_OPTION_ARG_STRING, &transport_option,
82 "Transport: buffered, framed (=buffered)", NULL },
83 { "protocol", 0, 0, G_OPTION_ARG_STRING, &protocol_option,
Chandler May6dde90b2016-01-10 06:01:10 +000084 "Protocol: binary, compact (=binary)", NULL },
85 { "string-limit", 0, 0, G_OPTION_ARG_INT, &string_limit,
86 "Max string length (=none)", NULL },
87 { "container-limit", 0, 0, G_OPTION_ARG_INT, &container_limit,
88 "Max container length (=none)", NULL },
Roger Meier15df0762014-09-29 20:50:56 +020089 { NULL }
90 };
91
92 gchar *server_name = "simple";
93 gchar *transport_name = "buffered";
94 GType transport_factory_type = THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY;
95 gchar *protocol_name = "binary";
96 GType protocol_factory_type = THRIFT_TYPE_BINARY_PROTOCOL_FACTORY;
97
98 TTestThriftTestHandler *handler;
99 ThriftProcessor *processor;
100 ThriftServerTransport *server_transport;
101 ThriftTransportFactory *transport_factory;
102 ThriftProtocolFactory *protocol_factory;
103
104 struct sigaction sigint_action;
105
106 GOptionContext *option_context;
107 gboolean options_valid = TRUE;
108
109 GError *error = NULL;
110
111#if (!GLIB_CHECK_VERSION (2, 36, 0))
112 g_type_init ();
113#endif
114
115 /* Configure and parse our command-line options */
116 option_context = g_option_context_new (NULL);
117 g_option_context_add_main_entries (option_context,
118 option_entries,
119 NULL);
120 if (g_option_context_parse (option_context,
121 &argc,
122 &argv,
123 &error) == FALSE) {
124 fprintf (stderr, "%s\n", error->message);
125 return 255;
126 }
127 g_option_context_free (option_context);
128
129 /* Validate the parsed options */
130 if (server_type_option != NULL &&
131 strncmp (server_type_option, "simple", 7) != 0) {
132 fprintf (stderr, "Unknown server type %s\n", protocol_option);
133 options_valid = FALSE;
134 }
135
Chandler May6dde90b2016-01-10 06:01:10 +0000136 if (protocol_option != NULL) {
137 if (strncmp (protocol_option, "compact", 8) == 0) {
138 protocol_factory_type = THRIFT_TYPE_COMPACT_PROTOCOL_FACTORY;
139 protocol_name = "compact";
140 }
141 else if (strncmp (protocol_option, "binary", 7) != 0) {
142 fprintf (stderr, "Unknown protocol type %s\n", protocol_option);
143 options_valid = FALSE;
144 }
Roger Meier15df0762014-09-29 20:50:56 +0200145 }
146
147 if (transport_option != NULL) {
148 if (strncmp (transport_option, "framed", 7) == 0) {
149 transport_factory_type = THRIFT_TYPE_FRAMED_TRANSPORT_FACTORY;
150 transport_name = "framed";
151 }
152 else if (strncmp (transport_option, "buffered", 9) != 0) {
153 fprintf (stderr, "Unknown transport type %s\n", transport_option);
154 options_valid = FALSE;
155 }
156 }
157
158 if (!options_valid)
159 return 254;
160
161 /* Establish all our connection objects */
162 handler = g_object_new (TYPE_THRIFT_TEST_HANDLER,
163 NULL);
164 processor = g_object_new (T_TEST_TYPE_THRIFT_TEST_PROCESSOR,
165 "handler", handler,
166 NULL);
167 server_transport = g_object_new (THRIFT_TYPE_SERVER_SOCKET,
168 "port", port,
169 NULL);
170 transport_factory = g_object_new (transport_factory_type,
171 NULL);
Chandler May6dde90b2016-01-10 06:01:10 +0000172
173 if (strncmp (protocol_name, "compact", 8) == 0) {
174 protocol_factory = g_object_new (protocol_factory_type,
175 "string_limit", string_limit,
176 "container_limit", container_limit,
177 NULL);
178 } else {
179 protocol_factory = g_object_new (protocol_factory_type,
180 NULL);
181 }
Roger Meier15df0762014-09-29 20:50:56 +0200182
183 server = g_object_new (THRIFT_TYPE_SIMPLE_SERVER,
184 "processor", processor,
185 "server_transport", server_transport,
186 "input_transport_factory", transport_factory,
187 "output_transport_factory", transport_factory,
188 "input_protocol_factory", protocol_factory,
189 "output_protocol_factory", protocol_factory,
190 NULL);
191
192 /* Install our SIGINT handler, which handles Ctrl-C being pressed by stopping
193 the server gracefully */
194 memset (&sigint_action, 0, sizeof (sigint_action));
195 sigint_action.sa_handler = sigint_handler;
196 sigint_action.sa_flags = SA_RESETHAND;
197 sigaction (SIGINT, &sigint_action, NULL);
198
199 printf ("Starting \"%s\" server (%s/%s) listen on: %d\n",
200 server_name,
201 transport_name,
202 protocol_name,
203 port);
204 fflush (stdout);
205
206 /* Serve clients until SIGINT is received (Ctrl-C is pressed) */
207 thrift_server_serve (server, &error);
208
209 /* If the server stopped for any reason other than being interrupted by the
210 user, report the error */
211 if (!sigint_received) {
212 g_message ("thrift_server_serve: %s",
213 error != NULL ? error->message : "(null)");
214 g_clear_error (&error);
215 }
216
217 puts ("done.");
218
219 g_object_unref (server);
220 g_object_unref (protocol_factory);
221 g_object_unref (transport_factory);
222 g_object_unref (server_transport);
223 g_object_unref (processor);
224 g_object_unref (handler);
225
226 return 0;
227}