-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.c
498 lines (427 loc) · 11.5 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (C) 2004 Steve Harris
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* $Id$
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <float.h>
#include <sys/types.h>
#include <unistd.h>
#define geterror() errno
#include "lop_types_internal.h"
#include "lop_internal.h"
#include "lop/lop_throw.h"
#include "lop/lop_lowlevel.h"
#include "lop/lop_endian.h"
static void dispatch_method(lop_server s, const char *path,
lop_message msg);
static void dispatch_queued(lop_server s);
static void queue_data(lop_server s, lop_timetag ts, const char *path,
lop_message msg);
static int lop_can_coerce(char a, char b);
static int lop_can_coerce_spec(const char *a, const char *b);
typedef struct {
lop_timetag ts;
char *path;
lop_message msg;
void *next;
} queued_msg_list;
lop_server lop_server_new(lop_err_handler err_h, lop_send_handler send_h, void *send_h_arg)
{
lop_server s;
s = calloc(1, sizeof(struct _lop_server));
if (!s) return 0;
s->err_h = err_h;
s->send_h = send_h;
s->send_h_arg = send_h_arg;
return s;
}
void lop_server_free(lop_server s)
{
lop_method it;
lop_method next;
#warning free s->queued ?
for (it = s->first; it; it = next) {
next = it->next;
free((char *)it->path);
free((char *)it->typespec);
free(it);
}
free(s);
}
int lop_server_dispatch_data(lop_server s, void *data, size_t size)
{
int result;
char *path;
ssize_t len;
dispatch_queued(s);
if (size == 0)
return 0;
result = 0;
path = data;
len = lop_validate_string(data, size);
if (len < 0) {
lop_throw(s, -len, "Invalid message path", NULL);
return len;
}
if (!strcmp(data, "#bundle")) {
char *pos;
int remain;
uint32_t elem_len;
lop_timetag ts, now;
ssize_t bundle_result = lop_validate_bundle(data, size);
if (bundle_result < 0) {
lop_throw(s, -bundle_result, "Invalid bundle", NULL);
return bundle_result;
}
pos = (char *)data + len;
remain = size - len;
lop_timetag_now(&now);
ts.sec = lop_otoh32(*((uint32_t *)pos));
pos += 4;
ts.frac = lop_otoh32(*((uint32_t *)pos));
pos += 4;
remain -= 8;
while (remain >= 4) {
lop_message msg;
elem_len = lop_otoh32(*((uint32_t *)pos));
pos += 4;
remain -= 4;
msg = lop_message_deserialise(pos, elem_len, &result);
if (!msg) {
lop_throw(s, result, "Invalid bundle element received", path);
return -result;
}
// set timetag from bundle
msg->ts = ts;
// test for immediate dispatch
if ((ts.sec == LOP_TT_IMMEDIATE.sec
&& ts.frac == LOP_TT_IMMEDIATE.frac) ||
lop_timetag_diff(ts, now) <= 0.0) {
dispatch_method(s, pos, msg);
lop_message_free(msg);
} else {
queue_data(s, ts, pos, msg);
}
pos += elem_len;
remain -= elem_len;
}
} else {
lop_message msg = lop_message_deserialise(data, size, &result);
if (NULL == msg) {
lop_throw(s, result, "Invalid message received", path);
return -result;
}
dispatch_method(s, data, msg);
lop_message_free(msg);
}
return size;
}
/* returns the time in seconds until the next scheduled event */
double lop_server_next_event_delay(lop_server s)
{
if (s->queued) {
lop_timetag now;
double delay;
lop_timetag_now(&now);
delay = lop_timetag_diff(((queued_msg_list *)s->queued)->ts, now);
delay = delay > 100.0 ? 100.0 : delay;
delay = delay < 0.0 ? 0.0 : delay;
return delay;
}
return 100.0;
}
static void lop_send_message(lop_server s, const char *path, lop_message msg)
{
const size_t data_len = lop_message_length(msg, path);
char *data = lop_message_serialise(msg, path, NULL, NULL);
s->send_h(data, data_len, s->send_h_arg);
free(data);
}
static void dispatch_method(lop_server s, const char *path,
lop_message msg)
{
char *types = msg->types + 1;
int argc = strlen(types);
lop_arg **argv = msg->argv;
lop_method it;
int ret = 1;
int pattern = strpbrk(path, " #*,?[]{}") != NULL;
const char *pptr;
for (it = s->first; it; it = it->next) {
/* If paths match or handler is wildcard */
if (!it->path || !strcmp(path, it->path) ||
(pattern && lop_pattern_match(it->path, path))) {
/* If types match or handler is wildcard */
if (!it->typespec || !strcmp(types, it->typespec)) {
/* Send wildcard path to generic handler, expanded path
to others.
*/
pptr = path;
if (it->path) pptr = it->path;
ret = it->handler(pptr, types, argv, argc, msg,
it->user_data);
} else if (lop_can_coerce_spec(types, it->typespec)) {
int i;
int opsize = 0;
char *ptr = msg->data;
char *data_co, *data_co_ptr;
argv = calloc(argc, sizeof(lop_arg *));
for (i=0; i<argc; i++) {
opsize += lop_arg_size(it->typespec[i], ptr);
ptr += lop_arg_size(types[i], ptr);
}
data_co = malloc(opsize);
data_co_ptr = data_co;
ptr = msg->data;
for (i=0; i<argc; i++) {
argv[i] = (lop_arg *)data_co_ptr;
lop_coerce(it->typespec[i], (lop_arg *)data_co_ptr,
types[i], (lop_arg *)ptr);
data_co_ptr += lop_arg_size(it->typespec[i], data_co_ptr);
ptr += lop_arg_size(types[i], ptr);
}
/* Send wildcard path to generic handler, expanded path
to others.
*/
pptr = path;
if (it->path) pptr = it->path;
ret = it->handler(pptr, it->typespec, argv, argc, msg,
it->user_data);
free(argv);
free(data_co);
argv = NULL;
}
if (ret == 0 && !pattern) {
break;
}
}
}
/* If we find no matching methods, check for protocol level stuff */
if (ret == 1) {
char *pos = strrchr(path, '/');
/* if its a method enumeration call */
if (pos && *(pos+1) == '\0') {
lop_message reply = lop_message_new();
int len = strlen(path);
lop_strlist *sl = NULL, *slit, *slnew, *slend;
if (!strcmp(types, "i")) {
lop_message_add_int32(reply, argv[0]->i);
}
lop_message_add_string(reply, path);
for (it = s->first; it; it = it->next) {
/* If paths match */
if (it->path && !strncmp(path, it->path, len)) {
char *tmp;
char *sec;
tmp = malloc(strlen(it->path + len) + 1);
strcpy(tmp, it->path + len);
sec = index(tmp, '/');
if (sec) *sec = '\0';
slend = sl;
for (slit = sl; slit; slend = slit, slit = slit->next) {
if (!strcmp(slit->str, tmp)) {
free(tmp);
tmp = NULL;
break;
}
}
if (tmp) {
slnew = calloc(1, sizeof(lop_strlist));
slnew->str = tmp;
slnew->next = NULL;
if (!slend) {
sl = slnew;
} else {
slend->next = slnew;
}
}
}
}
slit = sl;
while(slit) {
lop_message_add_string(reply, slit->str);
slnew = slit;
slit = slit->next;
free(slnew->str);
free(slnew);
}
lop_send_message(s, "#reply", reply);
lop_message_free(reply);
}
}
}
int lop_server_events_pending(lop_server s)
{
return s->queued != 0;
}
static void queue_data(lop_server s, lop_timetag ts, const char *path,
lop_message msg)
{
/* insert blob into future dispatch queue */
queued_msg_list *it = s->queued;
queued_msg_list *prev = NULL;
queued_msg_list *ins = calloc(1, sizeof(queued_msg_list));
ins->ts = ts;
ins->path = strdup(path);
ins->msg = msg;
while (it) {
if (lop_timetag_diff(it->ts, ts) > 0.0) {
if (prev) {
prev->next = ins;
} else {
s->queued = ins;
ins->next = NULL;
}
ins->next = it;
return;
}
prev = it;
it = it->next;
}
/* fell through, so this event is last */
if (prev) {
prev->next = ins;
} else {
s->queued = ins;
}
ins->next = NULL;
}
static void dispatch_queued(lop_server s)
{
queued_msg_list *head = s->queued;
queued_msg_list *tailhead;
lop_timetag disp_time;
lop_timetag_now(&disp_time);
while (head && lop_timetag_diff(head->ts, disp_time) < FLT_EPSILON) {
char *path;
lop_message msg;
tailhead = head->next;
path = ((queued_msg_list *)s->queued)->path;
msg = ((queued_msg_list *)s->queued)->msg;
dispatch_method(s, path, msg);
free(path);
lop_message_free(msg);
free((queued_msg_list *)s->queued);
s->queued = tailhead;
head = tailhead;
}
}
lop_method lop_server_add_method(lop_server s, const char *path,
const char *typespec, lop_method_handler h,
void *user_data)
{
lop_method m = calloc(1, sizeof(struct _lop_method));
lop_method it;
if (path && strpbrk(path, " #*,?[]{}")) {
return NULL;
}
if (path) {
m->path = strdup(path);
} else {
m->path = NULL;
}
if (typespec) {
m->typespec = strdup(typespec);
} else {
m->typespec = NULL;
}
m->handler = h;
m->user_data = user_data;
m->next = NULL;
/* append the new method to the list */
if (!s->first) {
s->first = m;
} else {
/* get to the last member of the list */
for (it=s->first; it->next; it=it->next);
it->next = m;
}
return m;
}
void lop_server_del_method(lop_server s, const char *path,
const char *typespec)
{
lop_method it, prev, next;
int pattern = 0;
if (!s->first) return;
if (path) pattern = strpbrk(path, " #*,?[]{}") != NULL;
it = s->first;
prev = it;
while (it) {
/* incase we free it */
next = it->next;
/* If paths match or handler is wildcard */
if ((it->path == path) ||
(path && it->path && !strcmp(path, it->path)) ||
(pattern && it->path && lop_pattern_match(it->path, path))) {
/* If types match or handler is wildcard */
if ((it->typespec == typespec) ||
(typespec && it->typespec && !strcmp(typespec, it->typespec))
) {
/* Take care when removing the head. */
if (it == s->first) {
s->first = it->next;
} else {
prev->next = it->next;
}
next = it->next;
free((void *)it->path);
free((void *)it->typespec);
free(it);
it = prev;
}
}
prev = it;
if (it) it = next;
}
}
void lop_server_pp(lop_server s)
{
lop_method it;
printf("Methods\n");
for (it = s->first; it; it = it->next) {
printf("\n");
lop_method_pp_prefix(it, " ");
}
}
static int lop_can_coerce_spec(const char *a, const char *b)
{
unsigned int i;
if (strlen(a) != strlen(b)) {
return 0;
}
for (i=0; a[i]; i++) {
if (!lop_can_coerce(a[i], b[i])) {
return 0;
}
}
return 1;
}
static int lop_can_coerce(char a, char b)
{
return ((a == b) ||
(lop_is_numerical_type(a) && lop_is_numerical_type(b)) ||
(lop_is_string_type(a) && lop_is_string_type (b)));
}
void lop_throw(lop_server s, int errnum, const char *message, const char *path)
{
if (s->err_h) {
(*s->err_h)(errnum, message, path);
}
}
/* vi:set ts=8 sts=4 sw=4: */