-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
656 lines (557 loc) · 24.1 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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* INCLUDES */
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <msgpack.h>
#include <fcntl.h> //open
#include <pthread.h>
#include <unistd.h> //close
#include <signal.h> // for SIGINT
#include <stdarg.h> // for VA_ARGS
#include "session.h"
#include "sheet.h"
#include "rpsc.h"
/* DEFINES */
#define PORT 1234
#define MAXCLIENTS 3
#define DEBUG 0
#define printd(x, ...) sc_server_debug(x, ##__VA_ARGS__)
/* STRUCTS */
/* Used as argument to thread_start() */
struct thread_info {
pthread_t thread_id; /* ID returned by pthread_create() */
int msocket; /* socket descriptor */
int exit; /* set it to 1 to exit thread */
};
// this is a dirty struct to handle a message
// will be removed later on when code generator is ready
typedef struct {
int * id;
char * method;
char * name; // sheet_name
int * row;
int * col;
int * to_row;
int * to_col;
short * bye;
double * val; // ent value
char * label; // ent label
short * flag; // ent flag
short * formula; // ent formula
} msg;
/* PROTOTYPES */
void * handle_new_connection(void * arg);
int process_msg(int msocket, msgpack_object o, msgpack_sbuffer * sbuf);
void initialize_msg(msg * m);
void free_msg(msg * m);
void decompress_msg(msgpack_object o, msg * m);
void debug_msg(msg * m);
void sc_server_debug(char * s, ...);
/* GLOBAL VARIABLES */
msgpack_packer pk;
msgpack_zone mempool;
int got_SIGINT = 0;
struct sockaddr_in address;
int server_fd;
/************************************************/
void SIGINT_handler(int dummy) {
got_SIGINT = 1;
printd("%s", "Got SIGINT. Shutting down server\n");
}
int main(void) {
signal(SIGINT, SIGINT_handler);
int opt = 1;
int addrlen = sizeof(address);
int intLanzados = 0;
int stat, msocket, i;
struct thread_info thLanzados[MAXCLIENTS];
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
printd("socket failed");
return -1;
}
// Forcefully attaching socket to the port PORT
// prevents “address already in use”.
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
printd("setsockopt");
return -1;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
if (bind(server_fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
printd("bind failed");
return -1;
}
/* deserialize the buffer into msgpack_object instance. */
/* deserialized object is valid during the msgpack_zone instance alive. */
msgpack_zone_init(&mempool, 1024);
// loop
while (! got_SIGINT) {
// check number of clients
if (listen(server_fd, MAXCLIENTS) < 0) {
printd("listen failure");
return -1;
}
/*if(fcntl(server_fd, F_SETFL, O_NONBLOCK) < 0) {
printd("!! cannot set socket in non blocking mode");
return -1;
}*/
if ((msocket = accept(server_fd, (struct sockaddr *) &address, (socklen_t*) &addrlen))<0) {
continue;
//return -1;
}
printd("new connection - address:%d port:%d\n", address.sin_addr, address.sin_port);
/* Put the socket in non-blocking mode:
if(fcntl(msocket, F_SETFL, fcntl(msocket, F_GETFL) | O_NONBLOCK) < 0) {
printd("cannot set socket in non blocking mode");
return -1;
}*/
if (msocket < 0) {
printd("ERROR on accept\n");
continue;
}
thLanzados[intLanzados].msocket = msocket;
// The pthread_create() call stores the thread ID into corresponding element of thread_info
int stat = pthread_create(&thLanzados[intLanzados].thread_id, NULL, &handle_new_connection, &thLanzados[intLanzados]);
// Error creating the thread
if ( 0 != stat ) return -1;
else intLanzados++;
}
// wait for threads to finish
printd("waiting for threads to finish");
for (i=0; i<intLanzados; i++) {
stat = pthread_join(thLanzados[i].thread_id, NULL);
if (0 != stat) {
printd("error waiting threads to finish\n");
return -1;
}
}
msgpack_zone_destroy(&mempool);
close(server_fd);
return 0;
}
void sc_server_debug(char * s, ...) {
if (! DEBUG) return;
char t[256];
va_list args;
va_start(args, s);
vsprintf (t, s, args);
printf("%s", t);
va_end(args);
}
void * handle_new_connection(void * arg) {
struct thread_info * ti = arg;
int tid = ti->thread_id;
int msocket = ti->msocket;
char buffer[1024] = {0};
printd("new thread: %d\n", msocket);
int valread;
msgpack_sbuffer sbuf;
msgpack_sbuffer_init(&sbuf); /* msgpack::sbuffer */
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); /* initialize packer */
// FIXME is this used correctly here?
slab_allocator_init();
ExpressionInit();
init_lib();
// send server welcome
char * helo = "ROMAN server v0.01";
send(msocket, helo, strlen(helo), 0 );
int res = 0;
while (res == 0) {
valread = read(msocket, buffer, 1024);
if (valread > 0) {
msgpack_object deserialized;
//FIXME sizeof(..) may differ on sender and receiver machine
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &deserialized);
res = process_msg(msocket, deserialized, &sbuf);
//if (valread == 4) printd("msg from %d %d: %s %d\n", s.sin_addr, s.sin_port, buffer, sizeof(buffer));
}
}
close(msocket);
printd("closed socket: %d\n", msocket);
msgpack_sbuffer_destroy(&sbuf);
return NULL;
}
/* process a message
return 0 when message was handled
return 1 on exit (close session)
*/
int process_msg(int msocket, msgpack_object o, msgpack_sbuffer * sbuf) {
printd("\nprocess msg from: %d - type:%d\n\\\t->", msocket, o.type);
//msgpack_object_print(stdout, o);
printd("\n\n");
// HANDLE HELO and create a new session
if (o.type == MSGPACK_OBJECT_STR && ! strcmp(o.via.str.ptr, "HELO")) {
printd("got: %s\n", o);
// create new session
int id_session = create_session();
printd("created session #: %d\n", id_session);
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_int(&pk, id_session);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
// handle a message that comes in a map
} else if (o.type == MSGPACK_OBJECT_MAP) {
int size = o.via.map.size;
msgpack_object_kv * p = o.via.map.ptr;
msgpack_object_kv * pend = o.via.map.ptr + o.via.map.size;
msg m;
initialize_msg(&m);
decompress_msg(o, &m);
debug_msg(&m);
if (! strncmp(m.method, "create_sheet", 12)) {
struct roman * cur_sesn = get_session(*(m.id));
struct Sheet * sh = new_sheet(cur_sesn, m.name);
/* TODO: should return -1 if session not found */
//growtbl(sh, GROWNEW, 0, 0); /* ONLY if OLD is defined */
cur_sesn->cur_sh = sh;
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3); /* return an OK msg to client */
msgpack_pack_short(&pk, 0);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
} else if (! strncmp(m.method, "delete_sheet", 12)) {
struct roman * cur_sesn = get_session (*(m.id));
struct Sheet * sh = Search_sheet(cur_sesn, m.name);
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
delete_sheet(cur_sesn, sh);
// return an OK msg to client
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
} else if (! strncmp(m.method, "bye", 3)) {
int res = close_session (*(m.id));
// return an OK msg to client
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, res);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
return 1;
} else if (! strncmp(m.method, "set_val", 7)) {
printd("\nin server set_val\n");
struct roman * cur_sesn = get_session (*(m.id));
struct Ent * t = lookat(cur_sesn->cur_sh, *(m.row), *(m.col)); // USES CURRENT SHEET
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
t->flag |= VAL;
t->val = *(m.val);
// return an OK msg to client
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server set_val\n");
} else if (! strncmp(m.method, "get_val", 7)) {
printd("\nin server get_val\n");
struct roman * cur_sesn = get_session (*(m.id));
struct Ent * e1 = lookat(cur_sesn->cur_sh, *(m.row), *(m.col));
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
printd("e1->val: %f", e1->val);
msgpack_pack_map(&pk, 2);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "val", 3);
msgpack_pack_float(&pk, e1->val);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server get_val\n");
} else if (! strncmp(m.method, "set_label", 9)) {
printd("\nin server set_label\n");
struct roman * cur_sesn = get_session (*(m.id));
struct Ent * t = lookat(cur_sesn->cur_sh, *(m.row), *(m.col)); // USES CURRENT SHEET
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
t->flag |= RP_LABEL;
printd("label:%s", m.label);
t->label = malloc(sizeof(char) * strlen(m.label) + 1);
strcpy(t->label, m.label);
// return an OK msg to client
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server set_label\n");
} else if (! strncmp(m.method, "get_label", 9)) {
printd("\nin server get_val\n");
struct roman * cur_sesn = get_session (*(m.id));
struct Ent * e1 = lookat(cur_sesn->cur_sh, *(m.row), *(m.col));
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
msgpack_pack_map(&pk, 2);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
msgpack_pack_str(&pk, 5);
msgpack_pack_str_body(&pk, "label", 5);
msgpack_pack_str(&pk, strlen(e1->label));
msgpack_pack_str_body(&pk, e1->label, strlen(e1->label));
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server get_label\n");
} else if (! strncmp(m.method, "get_cells", 9)) {
printd("\nin server get_cells\n");
struct roman * cur_sesn = get_session (*(m.id));
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
/* count the number of ents to be returned */
int ri = *(m.row);
int ci = *(m.col);
int rf = *(m.to_row);
int cf = *(m.to_col);
int r,c;
register struct Ent ** pp;
struct Ent * e1;
int count = 0; // we need to count the cells we are going to send
for (r=ri; r<rf; r++) {
for (c=ci; c<cf; c++) {
pp = ATBL(cur_sesn->cur_sh, cur_sesn->cur_sh->tbl, r, c);
if ((pp !=0 ) && (*pp != 0)) count++;
}
}
msgpack_pack_str(&pk, 5);
msgpack_pack_str_body(&pk, "count", 5);
msgpack_pack_short(&pk, count);
msgpack_pack_str(&pk, 5);
msgpack_pack_str_body(&pk, "cells", 5);
msgpack_pack_map(&pk, count);
int attr_count; /* we need to send the number of (key,values) in the first place. row and col are already counted*/
for (r=ri; r<rf; r++) {
for (c=ci; c<cf; c++) {
pp = ATBL(cur_sesn->cur_sh, cur_sesn->cur_sh->tbl, r, c);
if ((pp !=0 ) && (*pp != 0)) {
e1 = *pp;
attr_count = 2;
if (e1->val) attr_count++;
if (e1->label) attr_count++;
if (e1->formula) attr_count++;
if (e1->flag) attr_count++;
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "cell", 4);
msgpack_pack_map(&pk, attr_count);
// only pack whats is needed
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "row", 3);
msgpack_pack_float(&pk, e1->row);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "col", 3);
msgpack_pack_float(&pk, e1->col);
if (e1->val) {
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "val", 3);
msgpack_pack_float(&pk, e1->val);
}
if (e1->label) {
msgpack_pack_str(&pk, 5);
msgpack_pack_str_body(&pk, "label", 5);
msgpack_pack_str(&pk, e1->label != NULL ? strlen(e1->label) : 0);
msgpack_pack_str_body(&pk, e1->label != NULL ? e1->label : "", e1->label != NULL ? strlen(e1->label) : 0);
}
if (e1->formula) {
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "formula", 7);
msgpack_pack_str(&pk, e1->formula != NULL ? strlen(e1->formula) : 0);
msgpack_pack_str_body(&pk, e1->formula != NULL ? e1->formula : "", e1->formula != NULL ? strlen(e1->formula) : 0);
}
if (e1->flag) {
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "flag", 4);
msgpack_pack_short(&pk, e1->flag);
}
}
}
}
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server get_cells\n");
} else if (! strncmp(m.method, "get_cell", 8)) {
printd("\nin server get_cell\n");
struct roman * cur_sesn = get_session (*(m.id));
/* TODO: should return -1 if session not found
should return -2 if sheet not found */
int attr_count; /* we need to send the number of (key,values) in the first place. row and col are already counted*/
int row = *(m.row);
int col = *(m.col);
register struct Ent ** pp;
struct Ent * e1;
pp = ATBL(cur_sesn->cur_sh, cur_sesn->cur_sh->tbl, row, col);
if ((pp !=0 ) && (*pp != 0)) {
msgpack_pack_map(&pk, 2);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, 0);
e1 = *pp;
attr_count = 2;
if (e1->val) attr_count++;
if (e1->label) attr_count++;
if (e1->formula) attr_count++;
if (e1->flag) attr_count++;
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "cell", 4);
msgpack_pack_map(&pk, attr_count);
// only pack whats is needed
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "row", 3);
msgpack_pack_int(&pk, e1->row);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "col", 3);
msgpack_pack_int(&pk, e1->col);
if (e1->val) {
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "val", 3);
msgpack_pack_float(&pk, e1->val);
}
if (e1->label) {
msgpack_pack_str(&pk, 5);
msgpack_pack_str_body(&pk, "label", 5);
msgpack_pack_str(&pk, e1->label != NULL ? strlen(e1->label) : 0);
msgpack_pack_str_body(&pk, e1->label != NULL ? e1->label : "", e1->label != NULL ? strlen(e1->label) : 0);
}
if (e1->formula) {
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "formula", 7);
msgpack_pack_str(&pk, e1->formula != NULL ? strlen(e1->formula) : 0);
msgpack_pack_str_body(&pk, e1->formula != NULL ? e1->formula : "", e1->formula != NULL ? strlen(e1->formula) : 0);
}
if (e1->flag) {
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "flag", 4);
msgpack_pack_short(&pk, e1->flag);
}
} else {
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "ret", 3);
msgpack_pack_short(&pk, -1);
}
send(msocket, sbuf->data, sbuf->size, 0 );
msgpack_sbuffer_clear(sbuf);
printd("\nfin server get_cell\n");
} else if (! strncmp("recalc", m.method, 6)) {
// TODO
}
free_msg(&m);
}
return 0;
}
// decompress message
// will be removed later when the code generator is ready
void decompress_msg(msgpack_object o, msg * m) {
int size = o.via.map.size;
msgpack_object_kv * p = o.via.map.ptr;
msgpack_object_kv * pend = o.via.map.ptr + o.via.map.size;
while (p < pend) {
if (p->key.type == MSGPACK_OBJECT_STR) {
printd("key: %.*s\n", p->key.via.str.size, p->key.via.str.ptr);
if (m->id == NULL && ! strncmp(p->key.via.str.ptr, "id", p->key.via.str.size)) {
m->id = (int *) malloc(sizeof(int));
*(m->id) = p->val.via.u64;
} else if (m->row == NULL && ! strncmp(p->key.via.str.ptr, "row", p->key.via.str.size)) {
m->row = (int *) malloc(sizeof(int));
*(m->row) = p->val.via.u64;
} else if (m->col == NULL && ! strncmp(p->key.via.str.ptr, "col", p->key.via.str.size)) {
m->col = (int *) malloc(sizeof(int));
*(m->col) = p->val.via.u64;
} else if (m->to_row == NULL && ! strncmp(p->key.via.str.ptr, "to_row", p->key.via.str.size)) {
m->to_row = (int *) malloc(sizeof(int));
*(m->to_row) = p->val.via.u64;
} else if (m->to_col == NULL && ! strncmp(p->key.via.str.ptr, "to_col", p->key.via.str.size)) {
m->to_col = (int *) malloc(sizeof(int));
*(m->to_col) = p->val.via.u64;
} else if (m->bye == NULL && ! strncmp(p->key.via.str.ptr, "bye", p->key.via.str.size)) {
m->bye = (short *) malloc(sizeof(short));
*(m->bye) = p->val.via.u64;
} else if (m->val == NULL && ! strncmp(p->key.via.str.ptr, "val", p->key.via.str.size)) {
m->val = (double *) malloc(sizeof(double));
*(m->val) = p->val.via.f64;
} else if (m->label == NULL && ! strncmp(p->key.via.str.ptr, "label", p->key.via.str.size)) {
m->label = (char *) malloc(sizeof(char) * (p->val.via.str.size + 1));
sprintf(m->label, "%.*s", p->val.via.str.size, p->val.via.str.ptr);
} else if (m->method == NULL && ! strncmp(p->key.via.str.ptr, "method", p->key.via.str.size)) {
m->method = (char *) malloc(sizeof(char) * (p->val.via.str.size + 1));
sprintf(m->method, "%.*s", p->val.via.str.size, p->val.via.str.ptr);
} else if (m->name == NULL && ! strncmp(p->key.via.str.ptr, "name", p->key.via.str.size)) {
m->name = (char *) malloc(sizeof(char) * (p->val.via.str.size + 1));
sprintf(m->name, "%.*s", p->val.via.str.size, p->val.via.str.ptr);
}
} else if (p->key.type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
//printd("key: %d\n", (int) p->key.via.u64);
}
if (p->val.type == MSGPACK_OBJECT_STR) {
//printd("val: %.*s\n", p->val.via.str.size, p->val.via.str.ptr);
} else if (p->val.type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
//printd("val: %d\n", (int) p->val.via.u64);
} else if (p->val.type == MSGPACK_OBJECT_MAP) {
//printd("val: ");
//msgpack_object_print(stdout, p->val);
decompress_msg(p->val, m);
//printd("\n");
}
p++;
}
}
void initialize_msg(msg * m) {
m->method = NULL;
m->name = NULL;
m->id = NULL;
m->row = NULL;
m->col = NULL;
m->to_row = NULL;
m->to_col = NULL;
m->val = NULL;
m->label = NULL;
m->formula = NULL;
m->flag = NULL;
m->bye = NULL;
return;
}
void free_msg(msg * m) {
if (m->method != NULL) free(m->method);
if (m->name != NULL) free(m->name);
if (m->id != NULL) free(m->id);
if (m->row != NULL) free(m->row);
if (m->col != NULL) free(m->col);
if (m->to_row != NULL) free(m->to_row);
if (m->to_col != NULL) free(m->to_col);
if (m->val != NULL) free(m->val);
if (m->label != NULL) free(m->label);
if (m->formula != NULL) free(m->formula);
if (m->flag != NULL) free(m->flag);
if (m->bye != NULL) free(m->bye);
return;
}
void debug_msg(msg * m) {
printd("debug:\n");
if (m->id != NULL) printd("m.id: %d\n", *(m->id));
if (m->row != NULL) printd("m.row: %d\n", *(m->row));
if (m->col != NULL) printd("m.col: %d\n", *(m->col));
if (m->to_row != NULL) printd("m.to_row: %d\n", *(m->to_row));
if (m->to_col != NULL) printd("m.to_col: %d\n", *(m->to_col));
if (m->method != NULL) printd("m.method: %s\n", (m->method));
if (m->name != NULL) printd("m.name: %s\n", (m->name));
if (m->val != NULL) printd("m.val: %f\n", *(m->val));
if (m->label != NULL) printd("m.label: %s\n", (m->label));
if (m->flag != NULL) printd("m.flag: %d\n", *(m->flag));
}