-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.c
636 lines (534 loc) · 18.7 KB
/
app.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
/*
* Copyright (C) 2016 Wentao Shang
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup net_ndn
* @{
*
* @file
*
* @author Wentao Shang <[email protected]>
*/
#include "app.h"
#include "encoding/name.h"
#include "encoding/interest.h"
#include "encoding/data.h"
#include "msg-type.h"
#include "ndn.h"
#include <debug.h>
#include <msg.h>
#include <thread.h>
#include <utlist.h>
#include <net/gnrc/netapi.h>
#include <net/gnrc/netreg.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
ndn_app_t* ndn_app_create(void)
{
if (ndn_pid == KERNEL_PID_UNDEF) {
DEBUG("ndn_app: ndn thread not initialized (pid=%"
PRIkernel_pid ")\n", thread_getpid());
return NULL;
}
ndn_app_t *handle = (ndn_app_t*)malloc(sizeof(ndn_app_t));
if (handle == NULL) {
DEBUG("ndn_app: cannot alloacte memory for app handle (pid=%"
PRIkernel_pid ")\n", thread_getpid());
return NULL;
}
handle->id = thread_getpid(); // set to caller pid
handle->_scb_table = NULL;
handle->_ccb_table = NULL;
handle->_pcb_table = NULL;
// add face id to face table
msg_t add_face, reply;
add_face.type = NDN_APP_MSG_TYPE_ADD_FACE;
add_face.content.value = (uint32_t)handle->id;
reply.content.value = 1;
msg_send_receive(&add_face, &reply, ndn_pid);
if (reply.content.value != 0) {
DEBUG("ndn_app: cannot add app face (pid=%"
PRIkernel_pid ")\n", handle->id);
free(handle);
return NULL;
}
// init msg queue to receive message
msg_init_queue(handle->_msg_queue, NDN_APP_MSG_QUEUE_SIZE);
return handle;
}
static int _notify_consumer_timeout(ndn_app_t* handle, ndn_block_t* pi)
{
ndn_block_t pn;
if (ndn_interest_get_name(pi, &pn) != 0) {
DEBUG("ndn_app: cannot parse name from pending interest (pid=%"
PRIkernel_pid ")\n", handle->id);
return NDN_APP_ERROR;
}
_consumer_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_ccb_table, entry, tmp) {
ndn_block_t n;
int r = ndn_interest_get_name(&entry->pi->block, &n);
assert(r == 0);
if (0 != memcmp(pn.buf, n.buf, pn.len < n.len ? pn.len : n.len)) {
// not the same interest name
//TODO: check selectors
continue;
}
// raise timeout callback
r = NDN_APP_CONTINUE;
if (entry->on_timeout != NULL) {
DEBUG("ndn_app: call consumer timeout cb (pid=%"
PRIkernel_pid ")\n", handle->id);
r = entry->on_timeout(&entry->pi->block);
}
DL_DELETE(handle->_ccb_table, entry);
ndn_shared_block_release(entry->pi);
free(entry);
// stop the app now if the callback returns error or stop
if (r != NDN_APP_CONTINUE) return r;
// otherwise continue
}
return NDN_APP_CONTINUE;
}
static int _notify_producer_interest(ndn_app_t* handle, ndn_block_t* interest)
{
ndn_block_t name;
if (ndn_interest_get_name(interest, &name) != 0) {
DEBUG("ndn_app: cannot parse name from received interest (pid=%"
PRIkernel_pid ")\n", handle->id);
return NDN_APP_ERROR;
}
_producer_cb_entry_t *entry;
DL_FOREACH(handle->_pcb_table, entry) {
if (-2 != ndn_name_compare_block(&entry->prefix->block, &name)) {
continue;
}
// raise interest callback
int r = NDN_APP_CONTINUE;
if (entry->on_interest != NULL) {
DEBUG("ndn_app: call producer interest cb (pid=%"
PRIkernel_pid ")\n", handle->id);
r = entry->on_interest(interest);
}
// stop the app now if the callback returns error or stop
if (r != NDN_APP_CONTINUE) return r;
// otherwise continue
}
return NDN_APP_CONTINUE;
}
static int _notify_consumer_data(ndn_app_t* handle, ndn_block_t* data)
{
ndn_block_t name;
if (ndn_data_get_name(data, &name) != 0) {
DEBUG("ndn_app: cannot parse name from received data (pid=%"
PRIkernel_pid ")\n", handle->id);
return NDN_APP_ERROR;
}
_consumer_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_ccb_table, entry, tmp) {
ndn_block_t n;
int r = ndn_interest_get_name(&entry->pi->block, &n);
assert(r == 0);
// prefix matching
r = ndn_name_compare_block(&n, &name);
if (r != -2 && r != 0) {
continue;
}
// raise data callback
r = NDN_APP_CONTINUE;
if (entry->on_data != NULL) {
DEBUG("ndn_app: call consumer data cb (pid=%"
PRIkernel_pid ")\n", handle->id);
r = entry->on_data(&entry->pi->block, data);
}
DL_DELETE(handle->_ccb_table, entry);
ndn_shared_block_release(entry->pi);
free(entry);
// stop the app now if the callback returns error or stop
if (r != NDN_APP_CONTINUE) return r;
// otherwise continue
}
return NDN_APP_CONTINUE;
}
static int _sched_call_cb(ndn_app_t* handle, msg_t* msg)
{
int r = NDN_APP_ERROR;
_sched_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_scb_table, entry, tmp) {
if (&entry->timer_msg == msg) {
DEBUG("ndn_app: call scheduled callback (pid=%"
PRIkernel_pid ")\n", handle->id);
DL_DELETE(handle->_scb_table, entry);
r = entry->cb(entry->context);
free(entry);
break;
}
}
return r;
}
int ndn_app_run(ndn_app_t* handle)
{
if (handle == NULL) return NDN_APP_ERROR;
int ret = NDN_APP_CONTINUE;
ndn_shared_block_t* ptr;
msg_t msg, reply;
reply.type = GNRC_NETAPI_MSG_TYPE_ACK;
reply.content.value = (uint32_t)(-ENOTSUP);
while (1) {
msg_receive(&msg);
switch (msg.type) {
case NDN_APP_MSG_TYPE_TERMINATE:
DEBUG("ndn_app: TERMINATE msg received from thread %"
PRIkernel_pid " (pid=%" PRIkernel_pid ")\n",
msg.sender_pid, handle->id);
return NDN_APP_STOP;
case MSG_XTIMER:
DEBUG("ndn_app: XTIMER msg received from thread %"
PRIkernel_pid " (pid=%" PRIkernel_pid ")\n",
msg.sender_pid, handle->id);
ret = _sched_call_cb(handle, (msg_t*)msg.content.ptr);
break;
case NDN_APP_MSG_TYPE_TIMEOUT:
DEBUG("ndn_app: TIMEOUT msg received from thread %"
PRIkernel_pid " (pid=%" PRIkernel_pid ")\n",
msg.sender_pid, handle->id);
ptr = (ndn_shared_block_t*)msg.content.ptr;
ret = _notify_consumer_timeout(handle, &ptr->block);
ndn_shared_block_release(ptr);
break;
case NDN_APP_MSG_TYPE_INTEREST:
DEBUG("ndn_app: INTEREST msg received from thread %"
PRIkernel_pid " (pid=%" PRIkernel_pid ")\n",
msg.sender_pid, handle->id);
ptr = (ndn_shared_block_t*)msg.content.ptr;
ret = _notify_producer_interest(handle, &ptr->block);
ndn_shared_block_release(ptr);
break;
case NDN_APP_MSG_TYPE_DATA:
DEBUG("ndn_app: DATA msg received from thread %"
PRIkernel_pid " (pid=%" PRIkernel_pid ")\n",
msg.sender_pid, handle->id);
ptr = (ndn_shared_block_t*)msg.content.ptr;
ret = _notify_consumer_data(handle, &ptr->block);
ndn_shared_block_release(ptr);
break;
case GNRC_NETAPI_MSG_TYPE_GET:
case GNRC_NETAPI_MSG_TYPE_SET:
msg_reply(&msg, &reply);
break;
default:
DEBUG("ndn_app: unknown msg type %u (pid=%" PRIkernel_pid ")\n",
msg.type, handle->id);
break;
}
if (ret != NDN_APP_CONTINUE) {
DEBUG("ndn_app: stop app because callback returned"
" %s (pid=%" PRIkernel_pid ")\n",
ret == NDN_APP_STOP ? "STOP" : "ERROR",
handle->id);
return ret;
}
}
return ret;
}
static inline void _release_sched_cb_table(ndn_app_t* handle)
{
_sched_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_scb_table, entry, tmp) {
DEBUG("ndn_app: remove scheduler cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
DL_DELETE(handle->_scb_table, entry);
xtimer_remove(&entry->timer);
free(entry);
}
}
static inline void _release_consumer_cb_table(ndn_app_t* handle)
{
_consumer_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_ccb_table, entry, tmp) {
DEBUG("ndn_app: remove consumer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
DL_DELETE(handle->_ccb_table, entry);
ndn_shared_block_release(entry->pi);
free(entry);
}
}
static inline void _release_producer_cb_table(ndn_app_t* handle)
{
_producer_cb_entry_t *entry, *tmp;
DL_FOREACH_SAFE(handle->_pcb_table, entry, tmp) {
DEBUG("ndn_app: remove producer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
DL_DELETE(handle->_pcb_table, entry);
ndn_shared_block_release(entry->prefix);
free(entry);
}
}
void ndn_app_destroy(ndn_app_t* handle)
{
_release_sched_cb_table(handle);
_release_consumer_cb_table(handle);
_release_producer_cb_table(handle);
// remove face id to face table
msg_t add_face, reply;
add_face.type = NDN_APP_MSG_TYPE_REMOVE_FACE;
add_face.content.value = (uint32_t)handle->id;
reply.content.value = 1;
msg_send_receive(&add_face, &reply, ndn_pid);
if (reply.content.value != 0) {
DEBUG("ndn_app: error removing app face (pid=%"
PRIkernel_pid ")\n", handle->id);
// ignore the error anyway...
}
//TODO: clear msg queue
free(handle);
}
static _sched_cb_entry_t*
_add_sched_cb_entry(ndn_app_t* handle, ndn_app_sched_cb_t cb, void* context)
{
_sched_cb_entry_t* entry =
(_sched_cb_entry_t*)malloc(sizeof(_sched_cb_entry_t));
if (entry == NULL) {
DEBUG("ndn_app: cannot allocate memory for sched cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return NULL;
}
entry->cb = cb;
entry->context = context;
DL_PREPEND(handle->_scb_table, entry);
DEBUG("ndn_app: add sched cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return entry;
}
int ndn_app_schedule(ndn_app_t* handle, ndn_app_sched_cb_t cb, void* context,
uint32_t timeout)
{
if (handle == NULL) return -1;
_sched_cb_entry_t *entry =
_add_sched_cb_entry(handle, cb, context);
if (entry == NULL) return -1;
// initialize the timer
entry->timer.target = entry->timer.long_target = 0;
// initialize the msg struct
entry->timer_msg.type = MSG_XTIMER;
entry->timer_msg.content.ptr = (char*)(&entry->timer_msg);
// set a timer to send a message to the app thread
xtimer_set_msg(&entry->timer, timeout, &entry->timer_msg, handle->id);
return 0;
}
static _consumer_cb_entry_t*
_add_consumer_cb_entry(ndn_app_t* handle, ndn_shared_block_t* si,
ndn_app_data_cb_t on_data,
ndn_app_timeout_cb_t on_timeout)
{
_consumer_cb_entry_t *entry =
(_consumer_cb_entry_t*)malloc(sizeof(_consumer_cb_entry_t));
if (entry == NULL) {
DEBUG("ndn_app: cannot allocate memory for consumer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return NULL;
}
entry->on_data = on_data;
entry->on_timeout = on_timeout;
entry->pi = ndn_shared_block_copy(si);
DL_PREPEND(handle->_ccb_table, entry);
DEBUG("ndn_app: add consumer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return entry;
}
int ndn_app_express_interest(ndn_app_t* handle, ndn_block_t* name,
void* selectors, uint32_t lifetime,
ndn_app_data_cb_t on_data,
ndn_app_timeout_cb_t on_timeout)
{
if (handle == NULL) return -1;
// create encoded TLV block
ndn_shared_block_t* si = ndn_interest_create(name, selectors, lifetime);
if (si == NULL) {
DEBUG("ndn_app: cannot create interest block (pid=%"
PRIkernel_pid ")\n", handle->id);
return -1;
}
// add entry to consumer callback table
_consumer_cb_entry_t *entry
= _add_consumer_cb_entry(handle, si, on_data, on_timeout);
if (entry == NULL) {
ndn_shared_block_release(si);
return -1;
}
// send interest to NDN thread
msg_t send;
send.type = NDN_APP_MSG_TYPE_INTEREST;
send.content.ptr = (void*)si;
if (msg_try_send(&send, ndn_pid) < 1) {
DEBUG("ndn_app: cannot send interest to NDN thread (pid=%"
PRIkernel_pid ")\n", handle->id);
ndn_shared_block_release(si);
// remove consumer cb entry
DL_DELETE(handle->_ccb_table, entry);
ndn_shared_block_release(entry->pi);
free(entry);
return -1;
}
// NDN thread will own the shared block ptr
return 0;
}
int ndn_app_express_interest2(ndn_app_t* handle, ndn_name_t* name,
void* selectors, uint32_t lifetime,
ndn_app_data_cb_t on_data,
ndn_app_timeout_cb_t on_timeout)
{
if (handle == NULL) return -1;
// create encoded TLV block
ndn_shared_block_t* si = ndn_interest_create2(name, selectors, lifetime);
if (si == NULL) {
DEBUG("ndn_app: cannot create interest block (pid=%"
PRIkernel_pid ")\n", handle->id);
return -1;
}
// add entry to consumer callback table
_consumer_cb_entry_t *entry
= _add_consumer_cb_entry(handle, si, on_data, on_timeout);
if (entry == NULL) {
ndn_shared_block_release(si);
return -1;
}
// send interest to NDN thread
msg_t send;
send.type = NDN_APP_MSG_TYPE_INTEREST;
send.content.ptr = (void*)si;
if (msg_try_send(&send, ndn_pid) < 1) {
DEBUG("ndn_app: cannot send interest to NDN thread (pid=%"
PRIkernel_pid ")\n", handle->id);
ndn_shared_block_release(si);
// remove consumer cb entry
DL_DELETE(handle->_ccb_table, entry);
ndn_shared_block_release(entry->pi);
free(entry);
return -1;
}
// NDN thread will own the shared block ptr
return 0;
}
static _producer_cb_entry_t*
_add_producer_cb_entry(ndn_app_t* handle, ndn_shared_block_t* n,
ndn_app_interest_cb_t on_interest)
{
_producer_cb_entry_t *entry =
(_producer_cb_entry_t*)malloc(sizeof(_producer_cb_entry_t));
if (entry == NULL) {
DEBUG("ndn_app: cannot allocate memory for producer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return NULL;
}
entry->prefix = ndn_shared_block_copy(n);
entry->on_interest = on_interest;
DL_PREPEND(handle->_pcb_table, entry);
DEBUG("ndn_app: add producer cb entry (pid=%"
PRIkernel_pid ")\n", handle->id);
return entry;
}
int ndn_app_register_prefix(ndn_app_t* handle, ndn_shared_block_t* name,
ndn_app_interest_cb_t on_interest)
{
if (handle == NULL) {
ndn_shared_block_release(name);
return -1;
}
_producer_cb_entry_t* entry =
_add_producer_cb_entry(handle, name, on_interest);
if (entry == NULL) {
DEBUG("ndn_app: failed to add producer cb entry (pid=%"
PRIkernel_pid ")", handle->id);
ndn_shared_block_release(name);
return -1;
}
// notify ndn thread to add fib entry
msg_t add_fib, reply;
add_fib.type = NDN_APP_MSG_TYPE_ADD_FIB;
// once received, this pointer will be released by the ndn thread
add_fib.content.ptr = (void*)name;
reply.content.value = 1;
msg_send_receive(&add_fib, &reply, ndn_pid);
if (reply.content.value != 0) {
DEBUG("ndn_app: cannot add fib entry (pid=%"
PRIkernel_pid ")\n", handle->id);
DL_DELETE(handle->_pcb_table, entry);
ndn_shared_block_release(entry->prefix);
free(entry);
return -1;
}
return 0;
}
int ndn_app_register_prefix2(ndn_app_t* handle, ndn_name_t* name,
ndn_app_interest_cb_t on_interest)
{
if (handle == NULL) return -1;
ndn_block_t n;
n.len = ndn_name_total_length(name);
if (n.len <= 0) return -1;
n.buf = (const uint8_t*)malloc(n.len);
if (ndn_name_wire_encode(name, (uint8_t*)n.buf, n.len) <= 0) return -1;
ndn_shared_block_t* sn = ndn_shared_block_create_by_move(&n);
if (sn == NULL) {
DEBUG("ndn_app: cannot create shared block for prefix (pid=%"
PRIkernel_pid ")", handle->id);
free((void*)n.buf);
return -1;
}
return ndn_app_register_prefix(handle, sn, on_interest);
}
int ndn_app_put_data(ndn_app_t* handle, ndn_shared_block_t* sd)
{
if (handle == NULL || sd == NULL) return -1;
// send data to NDN thread
msg_t send;
send.type = NDN_APP_MSG_TYPE_DATA;
send.content.ptr = (void*)sd;
if (msg_try_send(&send, ndn_pid) < 1) {
DEBUG("ndn_app: cannot send data to NDN thread (pid=%"
PRIkernel_pid ")\n", handle->id);
ndn_shared_block_release(sd);
return -1;
}
// NDN thread will own the shared block ptr
return 0;
}
int ndn_app_add_strategy(ndn_shared_block_t* prefix,
ndn_forwarding_strategy_t* strategy)
{
struct _ndn_app_add_strategy_param param;
param.prefix = prefix; // receiver of this message takes the ownership
param.strategy = strategy;
msg_t op, reply;
op.type = NDN_APP_MSG_TYPE_ADD_STRATEGY;
op.content.ptr = (void*)(¶m);
reply.content.value = 1;
msg_send_receive(&op, &reply, ndn_pid);
if (reply.content.value != 0) {
DEBUG("ndn_app: cannot add forwarding strategy\n");
return -1;
}
return 0;
}
void ndn_app_send_msg_to_app(kernel_pid_t id, ndn_shared_block_t* block,
int msg_type)
{
msg_t m;
m.type = msg_type;
m.content.ptr = (void*)block;
if (msg_try_send(&m, id) < 1) {
DEBUG("ndn: cannot send msg to pid %"
PRIkernel_pid "\n", id);
// release the shared ptr here
ndn_shared_block_release(block);
}
DEBUG("ndn: msg sent to pid %" PRIkernel_pid "\n", id);
}
/** @} */