-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
144 lines (115 loc) · 4.92 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "db_lib/main.h"
#include "server_convert.h"
#include "gen-c_glib/query_svc.h"
#include <thrift/c_glib/server/thrift_simple_server.h>
#include <thrift/c_glib/transport/thrift_server_socket.h>
#include <thrift/c_glib/transport/thrift_buffered_transport_factory.h>
#include <thrift/c_glib/protocol/thrift_binary_protocol_factory.h>
#include <thrift/c_glib/protocol/thrift_protocol_factory.h>
#include <thrift/c_glib/transport/thrift_server_transport.h>
#include <thrift/c_glib/thrift.h>
#include <glib-object.h>
#include <glib.h>
#include <stdio.h>
#include <malloc.h>
G_BEGIN_DECLS
#define TYPE_EXAMPLE_QUERY_SVC_HANDLER \
(example_query_svc_handler_get_type ())
#define EXAMPLE_QUERY_SVC_HANDLER(obj) \
(G_TYPE_CHECK_INSTANCE_CAST ((obj), \
TYPE_EXAMPLE_QUERY_SVC_HANDLER, \
ExampleQuerySvcHandler))
#define EXAMPLE_QUERY_SVC_HANDLER_CLASS(c) \
(G_TYPE_CHECK_CLASS_CAST ((c), \
TYPE_EXAMPLE_QUERY_SVC_HANDLER, \
ExampleQuerySvcHandlerClass))
#define IS_EXAMPLE_QUERY_SVC_HANDLER(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
TYPE_EXAMPLE_QUERY_SVC_HANDLER))
#define IS_EXAMPLE_QUERY_SVC_HANDLER_CLASS(c) \
(G_TYPE_CHECK_CLASS_TYPE ((c), \
TYPE_EXAMPLE_QUERY_SVC_HANDLER))
#define EXAMPLE_QUERY_SVC_HANDLER_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS ((obj), \
TYPE_EXAMPLE_QUERY_SVC_HANDLER, \
ExampleQuerySvcHandlerClass))
struct _ExampleQuerySvcHandler {
querySvcHandler parent_instance;
};
typedef struct _ExampleQuerySvcHandler ExampleQuerySvcHandler;
struct _ExampleQuerySvcHandlerClass {
querySvcHandlerClass parent_class;
};
typedef struct _ExampleQuerySvcHandlerClass ExampleQuerySvcHandlerClass;
GType example_query_svc_handler_get_type(void);
G_END_DECLS
G_DEFINE_TYPE(ExampleQuerySvcHandler,
example_query_svc_handler,
TYPE_QUERY_SVC_HANDLER
)
static gboolean
example_query_svc_handler_execute(querySvcIf *iface,
I_QueryResult **_return,
const I_Query *query,
GError **error) {
THRIFT_UNUSED_VAR(iface);
THRIFT_UNUSED_VAR(error);
printf("Got query: %s\n", toString_I_QueryType(query->type));
Connection *connection = connect();
Query to_execute = convert_query(query);
QueryResult qr = execute_db_query(to_execute, connection);
*_return = convert_result(qr, to_execute.type);
close_connection(connection);
return TRUE;
}
static void
example_query_svc_handler_init(ExampleQuerySvcHandler *self) {
}
static void
example_query_svc_handler_class_init(ExampleQuerySvcHandlerClass *klass) {
querySvcHandlerClass *query_svc_handler_class =
QUERY_SVC_HANDLER_CLASS (klass);
query_svc_handler_class->execute = example_query_svc_handler_execute;
}
int
main(void) {
querySvcHandler *handler;
querySvcProcessor *processor;
ThriftServerTransport *server_transport;
ThriftTransportFactory *transport_factory;
ThriftProtocolFactory *protocol_factory;
ThriftServer *server;
GError *error = NULL;
#if (!GLIB_CHECK_VERSION(2, 36, 0))
g_type_init ();
#endif
handler = g_object_new(TYPE_EXAMPLE_QUERY_SVC_HANDLER,
NULL);
processor = g_object_new(TYPE_QUERY_SVC_PROCESSOR,
"handler", handler,
NULL);
server_transport = g_object_new(THRIFT_TYPE_SERVER_SOCKET,
"port", 9090,
NULL);
transport_factory = g_object_new(THRIFT_TYPE_BUFFERED_TRANSPORT_FACTORY,
NULL);
protocol_factory = g_object_new(THRIFT_TYPE_BINARY_PROTOCOL_FACTORY,
NULL);
server = g_object_new(THRIFT_TYPE_SIMPLE_SERVER,
"processor", processor,
"server_transport", server_transport,
"input_transport_factory", transport_factory,
"output_transport_factory", transport_factory,
"input_protocol_factory", protocol_factory,
"output_protocol_factory", protocol_factory,
NULL);
printf("SERVER IS RUNNING\n");
thrift_server_serve(server, &error);
g_object_unref(server);
g_object_unref(protocol_factory);
g_object_unref(transport_factory);
g_object_unref(server_transport);
g_object_unref(processor);
g_object_unref(handler);
return 0;
}