]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
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 <thrift/c_glib/server/thrift_simple_server.h> | |
21 | #include <thrift/c_glib/transport/thrift_transport_factory.h> | |
22 | #include <thrift/c_glib/protocol/thrift_protocol_factory.h> | |
23 | #include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h> | |
24 | ||
25 | G_DEFINE_TYPE(ThriftSimpleServer, thrift_simple_server, THRIFT_TYPE_SERVER) | |
26 | ||
27 | gboolean | |
28 | thrift_simple_server_serve (ThriftServer *server, GError **error) | |
29 | { | |
30 | ThriftTransport *t = NULL; | |
31 | ThriftTransport *input_transport = NULL, *output_transport = NULL; | |
32 | ThriftProtocol *input_protocol = NULL, *output_protocol = NULL; | |
33 | ThriftSimpleServer *tss = THRIFT_SIMPLE_SERVER(server); | |
34 | GError *process_error = NULL; | |
35 | ||
36 | g_return_val_if_fail (THRIFT_IS_SIMPLE_SERVER (server), FALSE); | |
37 | ||
38 | if (thrift_server_transport_listen (server->server_transport, error)) { | |
39 | tss->running = TRUE; | |
40 | while (tss->running == TRUE) | |
41 | { | |
42 | t = thrift_server_transport_accept (server->server_transport, | |
43 | error); | |
44 | if (t != NULL && tss->running) { | |
45 | input_transport = | |
46 | THRIFT_TRANSPORT_FACTORY_GET_CLASS (server->input_transport_factory) | |
47 | ->get_transport (server->input_transport_factory, t); | |
48 | output_transport = | |
49 | THRIFT_TRANSPORT_FACTORY_GET_CLASS (server->output_transport_factory) | |
50 | ->get_transport (server->output_transport_factory, t); | |
51 | input_protocol = | |
52 | THRIFT_PROTOCOL_FACTORY_GET_CLASS (server->input_protocol_factory) | |
53 | ->get_protocol (server->input_protocol_factory, input_transport); | |
54 | output_protocol = | |
55 | THRIFT_PROTOCOL_FACTORY_GET_CLASS (server->output_protocol_factory) | |
56 | ->get_protocol (server->output_protocol_factory, output_transport); | |
57 | ||
58 | while (THRIFT_PROCESSOR_GET_CLASS (server->processor) | |
59 | ->process (server->processor, | |
60 | input_protocol, | |
61 | output_protocol, | |
62 | &process_error) && | |
63 | thrift_transport_peek (input_transport, &process_error)) | |
64 | { | |
65 | } | |
66 | ||
67 | if (process_error != NULL) | |
68 | { | |
69 | g_message ("thrift_simple_server_serve: %s", process_error->message); | |
70 | g_clear_error (&process_error); | |
71 | ||
72 | /* Note we do not propagate processing errors to the caller as they | |
73 | * normally are transient and not fatal to the server */ | |
74 | } | |
75 | ||
76 | /* TODO: handle exceptions */ | |
77 | THRIFT_TRANSPORT_GET_CLASS (input_transport)->close (input_transport, | |
78 | NULL); | |
79 | THRIFT_TRANSPORT_GET_CLASS (output_transport)->close (output_transport, | |
80 | NULL); | |
81 | } | |
82 | } | |
83 | ||
84 | /* attempt to shutdown */ | |
85 | THRIFT_SERVER_TRANSPORT_GET_CLASS (server->server_transport) | |
86 | ->close (server->server_transport, NULL); | |
87 | } | |
88 | ||
89 | /* Since this method is designed to run forever, it can only ever return on | |
90 | * error */ | |
91 | return FALSE; | |
92 | } | |
93 | ||
94 | void | |
95 | thrift_simple_server_stop (ThriftServer *server) | |
96 | { | |
97 | g_return_if_fail (THRIFT_IS_SIMPLE_SERVER (server)); | |
98 | (THRIFT_SIMPLE_SERVER (server))->running = FALSE; | |
99 | } | |
100 | ||
101 | static void | |
102 | thrift_simple_server_init (ThriftSimpleServer *tss) | |
103 | { | |
104 | ThriftServer *server = THRIFT_SERVER(tss); | |
105 | ||
106 | tss->running = FALSE; | |
107 | ||
108 | if (server->input_transport_factory == NULL) | |
109 | { | |
110 | server->input_transport_factory = | |
111 | g_object_new (THRIFT_TYPE_TRANSPORT_FACTORY, NULL); | |
112 | } | |
113 | if (server->output_transport_factory == NULL) | |
114 | { | |
115 | server->output_transport_factory = | |
116 | g_object_new (THRIFT_TYPE_TRANSPORT_FACTORY, NULL); | |
117 | } | |
118 | if (server->input_protocol_factory == NULL) | |
119 | { | |
120 | server->input_protocol_factory = | |
121 | g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY, NULL); | |
122 | } | |
123 | if (server->output_protocol_factory == NULL) | |
124 | { | |
125 | server->output_protocol_factory = | |
126 | g_object_new (THRIFT_TYPE_BINARY_PROTOCOL_FACTORY, NULL); | |
127 | } | |
128 | } | |
129 | ||
130 | /* initialize the class */ | |
131 | static void | |
132 | thrift_simple_server_class_init (ThriftSimpleServerClass *class) | |
133 | { | |
134 | ThriftServerClass *cls = THRIFT_SERVER_CLASS(class); | |
135 | ||
136 | cls->serve = thrift_simple_server_serve; | |
137 | cls->stop = thrift_simple_server_stop; | |
138 | } |