-
Notifications
You must be signed in to change notification settings - Fork 19
/
handle_consumer_ops.c
192 lines (161 loc) · 5.95 KB
/
handle_consumer_ops.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "common.h"
#include "http_server.h"
#include "handle_consumer_ops.h"
static int send_json_gen(yajl_gen json_gen, const OpReply * const op_reply)
{
const unsigned char *json_out_buf;
size_t json_out_len;
struct evbuffer *evb;
yajl_gen_map_close(json_gen);
yajl_gen_get_buf(json_gen, &json_out_buf, &json_out_len);
if ((evb = evbuffer_new()) == NULL) {
yajl_gen_free(json_gen);
return -1;
}
evbuffer_add(evb, json_out_buf, json_out_len);
evhttp_send_reply(op_reply->bare_op_reply.req, HTTP_OK, "OK", evb);
evbuffer_free(evb);
yajl_gen_free(json_gen);
return 0;
}
static int handle_consumer_op_error(OpReply * const op_reply)
{
ErrorOpReply * const error_op_reply = &op_reply->error_op_reply;
evhttp_send_error(error_op_reply->req, HTTP_NOTFOUND, "Not Found");
return 0;
}
static int handle_consumer_op_system_ping(OpReply * const op_reply)
{
SystemPingOpReply * const system_ping_op_reply =
&op_reply->system_ping_op_reply;
yajl_gen json_gen = system_ping_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_system_rewrite(OpReply * const op_reply)
{
SystemRewriteOpReply * const system_rewrite_op_reply =
&op_reply->system_rewrite_op_reply;
yajl_gen json_gen = system_rewrite_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_layers_create(OpReply * const op_reply)
{
LayersCreateOpReply * const layers_create_op_reply =
&op_reply->layers_create_op_reply;
yajl_gen json_gen = layers_create_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_layers_delete(OpReply * const op_reply)
{
LayersDeleteOpReply * const layers_delete_op_reply =
&op_reply->layers_delete_op_reply;
yajl_gen json_gen = layers_delete_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_layers_index(OpReply * const op_reply)
{
LayersIndexOpReply * const layers_index_op_reply =
&op_reply->layers_index_op_reply;
yajl_gen json_gen = layers_index_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_records_put(OpReply * const op_reply)
{
RecordsPutOpReply * const records_put_op_reply =
&op_reply->records_put_op_reply;
yajl_gen json_gen = records_put_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_records_get(OpReply * const op_reply)
{
RecordsGetOpReply * const records_get_op_reply =
&op_reply->records_get_op_reply;
yajl_gen json_gen = records_get_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_records_delete(OpReply * const op_reply)
{
RecordsDeleteOpReply * const records_delete_op_reply =
&op_reply->records_delete_op_reply;
yajl_gen json_gen = records_delete_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_search_nearby(OpReply * const op_reply)
{
SearchNearbyOpReply * const search_nearby_op_reply =
&op_reply->search_nearby_op_reply;
yajl_gen json_gen = search_nearby_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_search_in_rect(OpReply * const op_reply)
{
SearchInRectOpReply * const search_in_rect_op_reply =
&op_reply->search_in_rect_op_reply;
yajl_gen json_gen = search_in_rect_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
static int handle_consumer_op_search_in_keys(OpReply * const op_reply)
{
SearchInKeysOpReply * const search_in_keys_op_reply =
&op_reply->search_in_keys_op_reply;
yajl_gen json_gen = search_in_keys_op_reply->json_gen;
return send_json_gen(json_gen, op_reply);
}
void consumer_cb(struct bufferevent * const bev, void *context_)
{
OpReply *op_reply;
int ret = -1;
(void) context_;
while (evbuffer_get_length(bev->input) >= sizeof op_reply) {
assert(bufferevent_read(bev, &op_reply, sizeof op_reply)
== sizeof op_reply);
switch (op_reply->bare_op_reply.type) {
case OP_TYPE_ERROR:
ret = handle_consumer_op_error(op_reply);
break;
case OP_TYPE_SYSTEM_PING:
ret = handle_consumer_op_system_ping(op_reply);
break;
case OP_TYPE_SYSTEM_REWRITE:
ret = handle_consumer_op_system_rewrite(op_reply);
break;
case OP_TYPE_LAYERS_CREATE:
ret = handle_consumer_op_layers_create(op_reply);
break;
case OP_TYPE_LAYERS_DELETE:
ret = handle_consumer_op_layers_delete(op_reply);
break;
case OP_TYPE_LAYERS_INDEX:
ret = handle_consumer_op_layers_index(op_reply);
break;
case OP_TYPE_RECORDS_PUT:
ret = handle_consumer_op_records_put(op_reply);
break;
case OP_TYPE_RECORDS_GET:
ret = handle_consumer_op_records_get(op_reply);
break;
case OP_TYPE_RECORDS_DELETE:
ret = handle_consumer_op_records_delete(op_reply);
break;
case OP_TYPE_SEARCH_NEARBY:
ret = handle_consumer_op_search_nearby(op_reply);
break;
case OP_TYPE_SEARCH_IN_RECT:
ret = handle_consumer_op_search_in_rect(op_reply);
break;
case OP_TYPE_SEARCH_IN_KEYS:
ret = handle_consumer_op_search_in_keys(op_reply);
break;
default:
ret = -1;
}
if (ret != 0) {
evhttp_send_error(op_reply->bare_op_reply.req,
ret, "Error");
}
op_reply->bare_op_reply.type = OP_TYPE_NONE;
op_reply->bare_op_reply.req = NULL;
op_reply->bare_op_reply.op_tid = (OpTID) 0;
free(op_reply);
}
}