forked from cn-uofbasel/ccn-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccn-lite-simu.c
756 lines (626 loc) · 19.7 KB
/
ccn-lite-simu.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/*
* @f ccn-lite-simu.c
* @b prog with multiple CCNL relays, running a standalone simulation
*
* Copyright (C) 2011-13, Christian Tschudin, University of Basel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* File history:
* 2011-11-22 created
* 2011-12 simulation scenario and logging support s.braun@stud.unibas.ch
* 2013-03-19 updated (ms): replacement code after renaming the field
* ccnl_relay_s.client to ccnl_relay_s.aux
*/
#define CCNL_SIMULATION
#define USE_DEBUG
#define USE_DEBUG_MALLOC
#define USE_FRAG
#define USE_SCHEDULER
#define USE_ETHERNET
#include "ccnl-includes.h"
#include "ccnx.h"
#include "ccnl.h"
#include "ccnl-core.h"
#include "ccnl-ext-debug.c"
#include "ccnl-ext.h"
#include "ccnl-platform.c"
int ccnl_app_RX(struct ccnl_relay_s *ccnl, struct ccnl_content_s *c);
void ccnl_print_stats(struct ccnl_relay_s *relay, int code);
enum {STAT_RCV_I, STAT_RCV_C, STAT_SND_I, STAT_SND_C, STAT_QLEN, STAT_EOP1};
#include "ccnl-core.c"
#include "ccnl-ext-mgmt.c"
#include "ccnl-ext-sched.c"
#include "ccnl-pdu.c"
#include "ccnl-ext-frag.c"
// ----------------------------------------------------------------------
#define SIMU_NUMBER_OF_CHUNKS 10
// #define SIMU_CHUNK_SIZE 4096
#define SIMU_CHUNK_SIZE 1600
// #define SIMU_CHUNK_SIZE 1340
static struct ccnl_relay_s* char2relay(char node);
static char relay2char(struct ccnl_relay_s *relay);
void ccnl_simu_fini(void *ptr, int aux);
void ccnl_simu_phase_two(void *ptr, void *dummy);
extern void ccnl_simu_client_RX(struct ccnl_relay_s *relay, char *name,
int seqn, char *data, int len);
extern void ccnl_simu_ll_TX(struct ccnl_relay_s *relay, unsigned char *dst,
unsigned char *src, unsigned char *data, int len);
extern void ccnl_simu_LOG(struct ccnl_relay_s *relay, char *s);
void ccnl_simu_client(char node); // sending side
void ccnl_simu_ethernet(void *dummy, void *dummy2); // implements the media
void ccnl_simu_client_endlog(struct ccnl_relay_s *relay);
// ----------------------------------------------------------------------
static struct ccnl_prefix_s*
ccnl_path_to_prefix(const char *path)
{
char *cp;
struct ccnl_prefix_s *pr = (struct ccnl_prefix_s*) ccnl_calloc(1,
sizeof(*pr));
DEBUGMSG(99, "ccnl_path_to_prefix <%s>\n", path);
if (!pr)
return NULL;
pr->comp = (unsigned char**) ccnl_malloc(CCNL_MAX_NAME_COMP *
sizeof(unsigned char**));
pr->complen = (int*) ccnl_malloc(CCNL_MAX_NAME_COMP * sizeof(int));
pr->path = (unsigned char*) ccnl_malloc(strlen(path)+1);
if (!pr->comp || !pr->complen || !pr->path) {
ccnl_free(pr->comp);
ccnl_free(pr->complen);
ccnl_free(pr->path);
ccnl_free(pr);
return NULL;
}
strcpy((char*) pr->path, path);
cp = (char*) pr->path;
for (path = strtok(cp, "/");
path && pr->compcnt < CCNL_MAX_NAME_COMP;
path = strtok(NULL, "/")) {
pr->comp[pr->compcnt] = (unsigned char*) path;
pr->complen[pr->compcnt] = strlen(path);
pr->compcnt++;
}
return pr;
}
// ----------------------------------------------------------------------
struct ccnl_relay_s relays[5];
#define relayChars "ABC12"
static struct ccnl_relay_s*
char2relay(char node)
{
unsigned int i;
for (i = 0; i < strlen(relayChars); i++) {
if (node == relayChars[i])
return relays + i;
}
fprintf(stderr, "unknown node %c\n", node);
return 0;
}
static char
relay2char(struct ccnl_relay_s *relay)
{
unsigned int i;
for (i = 0; i < strlen(relayChars); i++) {
if (relay == (relays + i))
return relayChars[i];
}
fprintf(stderr, "unknown node %p\n", (void *) relay);
return '?';
}
void
ccnl_simu_add2cache(char node, const char *name, int seqn, void *data, int len)
{
struct ccnl_relay_s *relay = char2relay(node);
struct ccnl_buf_s *bp;
struct ccnl_prefix_s *pp;
char *name2, tmp[10], *n;
char *namecomp[20];
unsigned char tmp2[8192];
int cnt, len2;
struct ccnl_content_s *c;
cnt = 0;
n = name2 = strdup(name);
name2 = strtok(name2, "/");
while (name2) {
namecomp[cnt++] = name2;
name2 = strtok(NULL, "/");
}
sprintf(tmp, ".%d", seqn);
namecomp[cnt++] = tmp;
namecomp[cnt] = 0;
len2 = mkContent(namecomp, (char*) data, len, tmp2);
free(n);
bp = ccnl_buf_new(tmp2, len2);
strcpy((char*) tmp2, name);
sprintf(tmp, "/.%d", seqn);
strcat((char*) tmp2, tmp);
pp = ccnl_path_to_prefix((const char*)tmp2);
c = ccnl_content_new(relay, &bp, &pp, NULL, 0, 0);
if (c)
ccnl_content_add2cache(relay, c);
}
// ----------------------------------------------------------------------
void
ccnl_client_TX(char node, char *name, int seqn, unsigned int nonce)
{
char *namecomp[20], *n;
char tmp[512], tmp2[10];
int len, cnt;
struct ccnl_relay_s *relay = char2relay(node);
if (!relay) return;
DEBUGMSG(10, "ccnl_client_tx node=%c: request %s #%d\n", node, name, seqn);
cnt = 0;
n = name = strdup(name);
name = strtok(name, "/");
while (name) {
namecomp[cnt++] = name;
name = strtok(NULL, "/");
}
sprintf(tmp2, ".%d", seqn);
namecomp[cnt++] = tmp2;
namecomp[cnt] = 0;
len = mkInterest(namecomp, &nonce, (unsigned char*) tmp);
free(n);
// inject it into the relay:
ccnl_core_RX(relay, -1, (unsigned char*)tmp, len, 0, 0);
}
// ----------------------------------------------------------------------
// simulated ethernet segment
struct ccnl_ethernet_s {
struct ccnl_ethernet_s *next;
unsigned char *dst, *src;
unsigned short protocol;
unsigned char *data;
int len;
};
struct ccnl_ethernet_s *etherqueue;
void
ccnl_simu_ethernet(void *dummy, void *dummy2)
{
if (etherqueue) {
int i, qlen;
struct ccnl_ethernet_s **pp, *p;
// pick the last element in the queue
for (qlen = 1, pp = ðerqueue; (*pp)->next; pp = &((*pp)->next))
qlen++;
p = *pp;
*pp = NULL;
for (i = 0; i < 5; i++) {
if (!memcmp(p->dst, &relays[i].ifs[0].addr.eth.sll_addr, ETH_ALEN)) {
break;
}
}
if (i < 5) {
sockunion sun;
sun.sa.sa_family = AF_PACKET;
memcpy(sun.eth.sll_addr, p->src, ETH_ALEN);
DEBUGMSG(10, "simu_ethernet: sending %d Bytes to %s, (qlen=%d)\n",
p->len, eth2ascii(p->dst), qlen);
ccnl_core_RX(relays + i, 0, (unsigned char*) p->data,
p->len, &sun.sa, sizeof(sun.eth));
} else {
DEBUGMSG(10, "simu_ethernet: dest %s not found\n",
eth2ascii(etherqueue->dst));
}
ccnl_free(p);
}
ccnl_set_timer(2000, ccnl_simu_ethernet, dummy, dummy2);
}
// ----------------------------------------------------------------------
// the two functions called by the CCNL's core and frag code:
int
ccnl_app_RX(struct ccnl_relay_s *ccnl, struct ccnl_content_s *c)
{
int i;
char tmp[200], tmp2[10];
struct ccnl_prefix_s *p = c->name;
tmp[0] = '\0';
for (i = 0; i < p->compcnt-1; i++) {
strcat((char*)tmp, "/");
tmp[strlen(tmp) + p->complen[i]] = '\0';
memcpy(tmp + strlen(tmp), p->comp[i], p->complen[i]);
}
memcpy(tmp2, p->comp[p->compcnt-1], p->complen[p->compcnt-1]);
tmp2[p->complen[p->compcnt-1]] = '\0';
DEBUGMSG(10, "app delivery at node %c, name=%s%s\n",
relay2char(ccnl), tmp, tmp2);
ccnl_simu_client_RX(ccnl, tmp, atoi(tmp2+1),
(char*) c->content, c->contentlen);
return 0;
}
void
ccnl_ll_TX(struct ccnl_relay_s *relay, struct ccnl_if_s *ifc,
sockunion *dst, struct ccnl_buf_s *buf)
{
struct ccnl_ethernet_s *p;
int cnt;
for (cnt = 0, p = etherqueue; p; p = p->next, cnt++);
DEBUGMSG(29, "eth(simu)_ll_TX to %s len=%d (qlen=%d) [0x%02x 0x%02x]\n",
ccnl_addr2ascii(dst), buf->datalen, cnt,
buf->data[0], buf->data[1]);
DEBUGMSG(29, " src=%s\n", ccnl_addr2ascii(&ifc->addr));
p = ccnl_calloc(1, sizeof(*p) + 2*ETH_ALEN + buf->datalen);
p->dst = (unsigned char*)p + sizeof(*p);
p->src = (unsigned char*)p + sizeof(*p) + ETH_ALEN;
p->data = (unsigned char*)p + sizeof(*p) + 2*ETH_ALEN;
p->len = buf->datalen;
memcpy(p->dst, dst->eth.sll_addr, ETH_ALEN);
p->protocol = dst->eth.sll_protocol;
memcpy(p->src, ifc->addr.eth.sll_addr, ETH_ALEN);
memcpy(p->data, buf->data, buf->datalen);
// prepend
p->next = etherqueue;
etherqueue = p;
}
void
ccnl_close_socket(int s)
{
close(s);
}
// ----------------------------------------------------------------------
void
ccnl_simu_node_log_start(struct ccnl_relay_s *relay, char node)
{
char name[100];
struct ccnl_stats_s *st = relay->stats;
if (!st)
return;
sprintf(name, "node-%c-sent.log", node);
st->ofp_s = fopen(name, "w");
if (st->ofp_s) {
fprintf(st->ofp_s,"#####################\n");
fprintf(st->ofp_s,"# sent_log Node %c\n", node);
fprintf(st->ofp_s,"#####################\n");
fprintf(st->ofp_s,"# time\t sent i\t\t sent c\t\t sent i+c\n");
fflush(st->ofp_s);
}
sprintf(name, "node-%c-rcvd.log", node);
st->ofp_r = fopen(name, "w");
if (st->ofp_r) {
fprintf(st->ofp_r,"#####################\n");
fprintf(st->ofp_r,"# rcvd_log Node %c\n", node);
fprintf(st->ofp_r,"#####################\n");
fprintf(st->ofp_r,"# time\t recv i\t\t recv c\t\t recv i+c\n");
fflush(st->ofp_r);
}
sprintf(name, "node-%c-qlen.log", node);
st->ofp_q = fopen(name, "w");
if (st->ofp_q) {
fprintf(st->ofp_q,"#####################\n");
fprintf(st->ofp_q,"# qlen_log Node %c (relay->ifs[0].qlen)\n", node);
fprintf(st->ofp_q,"#####################\n");
fprintf(st->ofp_q,"# time\t qlen\n");
fflush(st->ofp_q);
}
}
void
ccnl_print_stats(struct ccnl_relay_s *relay, int code)
{
//code 1 = recv interest
//code 2 = recv content
//code 3 = sent interest
//code 4 = sent content
//code 5 = queue length
//code 6 = end phaseOne
struct ccnl_stats_s *st = relay->stats;
double t = current_time();
double tmp;
if (!st || !st->ofp_r || !st->ofp_s || !st->ofp_q)
return;
switch (code) {
case STAT_RCV_I:
if (st->log_recv_t_i < st->log_recv_t_c) {
tmp = st->log_recv_t_c;
} else {
tmp = st->log_recv_t_i;
}
fprintf(st->ofp_r, "%.4g\t %f\t %c\t %f\n",
t, 1/(t - st->log_recv_t_i), '?', 1/(t-tmp));
fflush(st->ofp_r);
st->log_recv_t_i = t;
break;
case STAT_RCV_C:
if (st->log_recv_t_i < st->log_recv_t_c) {
tmp = st->log_recv_t_c;
} else {
tmp = st->log_recv_t_i;
}
fprintf(st->ofp_r, "%.4g\t %c\t %f\t %f\n",
t, '?', 1/(t - st->log_recv_t_c), 1/(t-tmp));
fflush(st->ofp_r);
st->log_recv_t_c = t;
case STAT_SND_I:
if (st->log_sent_t_i < st->log_sent_t_c) {
tmp = st->log_sent_t_c;
} else {
tmp = st->log_sent_t_i;
}
fprintf(st->ofp_s, "%.4g\t %f\t %c\t %f\n",
t, 1/(t - st->log_sent_t_i), '?', 1/(t-tmp));
fflush(st->ofp_s);
st->log_sent_t_i = t;
break;
case STAT_SND_C:
if (st->log_sent_t_i < st->log_sent_t_c) {
tmp = st->log_sent_t_c;
} else {
tmp = st->log_sent_t_i;
}
fprintf(st->ofp_s, "%.4g\t %c\t %f\t %f\n",
t, '?', 1/(t - st->log_sent_t_c), 1/(t-tmp));
fflush(st->ofp_s);
st->log_sent_t_c = t;
break;
case STAT_QLEN:
fprintf(st->ofp_q, "%.4g\t %i\n", t, relay->ifs[0].qlen);
fflush(st->ofp_q);
break;
case STAT_EOP1: // end of phase 1
fprintf(st->ofp_r, "%.4g\t %f\t %f\t %f\n", t, 0.0, 0.0, 0.0);
fflush(st->ofp_r);
break;
default:
break;
}
}
void ccnl_simu_cleanup(void *dummy, void *dummy2);
#include "ccnl-simu-client.c"
// ----------------------------------------------------------------------
static int inter_ccn_interval = 0; // in usec
static int inter_packet_interval = 0; // in usec
#ifdef USE_SCHEDULER
struct ccnl_sched_s*
ccnl_simu_defaultFaceScheduler(struct ccnl_relay_s *ccnl,
void(*cb)(void*,void*))
{
return ccnl_sched_pktrate_new(cb, ccnl, inter_ccn_interval);
}
#endif
void ccnl_ageing(void *relay, void *aux)
{
ccnl_do_ageing(relay, aux);
ccnl_set_timer(1000000, ccnl_ageing, relay, 0);
}
void
ccnl_simu_init_node(char node, const char *addr,
int max_cache_entries)
{
struct ccnl_relay_s *relay = char2relay(node);
struct ccnl_if_s *i;
DEBUGMSG(99, "ccnl_simu_init_node\n");
relay->id = relay - relays;
relay->max_cache_entries = node == 'C' ? -1 : max_cache_entries;
// add (fake) eth0 interface with index 0:
i = &relay->ifs[0];
i->addr.eth.sll_family = AF_PACKET;
memcpy(i->addr.eth.sll_addr, addr, ETH_ALEN);
// i->mtu = 1400;
i->mtu = 1200;
i->reflect = 1;
i->fwdalli = 1;
relay->ifcount++;
#ifdef USE_SCHEDULER
i->sched = ccnl_sched_pktrate_new(ccnl_interface_CTS, relay,
inter_packet_interval);
relay->defaultFaceScheduler = ccnl_simu_defaultFaceScheduler;
#endif
if (node == 'A' || node == 'B') {
struct ccnl_client_s *client = ccnl_calloc(1,
sizeof(struct ccnl_client_s));
client->lastseq = SIMU_NUMBER_OF_CHUNKS-1;
client->last_received = -1;
client->name = node == 'A' ?
"/ccnl/simu/movie1" : "/ccnl/simu/movie2";
relay->aux = (void *) client;
relay->stats = ccnl_calloc(1, sizeof(struct ccnl_stats_s));
}
ccnl_set_timer(1000000, ccnl_ageing, relay, 0);
ccnl_simu_node_log_start(relay, node);
}
void
ccnl_simu_add_fwd(char node, const char *name, char dstnode)
{
struct ccnl_relay_s *relay = char2relay(node), *dst = char2relay(dstnode);
struct ccnl_forward_s *fwd;
sockunion sun;
DEBUGMSG(99, "ccnl_simu_add_fwd\n");
sun.eth.sll_family = AF_PACKET;
memcpy(sun.eth.sll_addr, dst->ifs[0].addr.eth.sll_addr, ETH_ALEN);
fwd = (struct ccnl_forward_s *) ccnl_calloc(1, sizeof(*fwd));
fwd->prefix = ccnl_path_to_prefix(name);
fwd->face = ccnl_get_face_or_create(relay, 0, &sun.sa, sizeof(sun.eth));
#ifdef USE_FRAG
// fwd->face->frag = ccnl_frag_new(CCNL_FRAG_SEQUENCED2012, 1500);
fwd->face->frag = ccnl_frag_new(CCNL_FRAG_CCNx2013, 1200);
#endif
fwd->face->flags |= CCNL_FACE_FLAGS_STATIC;
fwd->next = relay->fib;
relay->fib = fwd;
}
int
ccnl_simu_init(int max_cache_entries)
{
static char dat[SIMU_CHUNK_SIZE];
static char init_was_visited;
int i;
if (init_was_visited)
return 0;
init_was_visited = 1;
DEBUGMSG(99, "ccnl_simu_init\n");
#ifdef USE_SCHEDULER
// initialize the scheduling subsystem
ccnl_sched_init();
#endif
// define each node's eth address:
ccnl_simu_init_node('A', "\x00\x00\x00\x00\x00\x0a",
max_cache_entries);
ccnl_simu_init_node('B', "\x00\x00\x00\x00\x00\x0b",
max_cache_entries);
ccnl_simu_init_node('C', "\x00\x00\x00\x00\x00\x0c",
max_cache_entries);
ccnl_simu_init_node('1', "\x00\x00\x00\x00\x00\x01",
max_cache_entries);
ccnl_simu_init_node('2', "\x00\x00\x00\x00\x00\x02",
max_cache_entries);
// install the system's forwarding pointers:
ccnl_simu_add_fwd('A', "/ccnl/simu", '2');
ccnl_simu_add_fwd('B', "/ccnl/simu", '2');
ccnl_simu_add_fwd('2', "/ccnl/simu", '1');
ccnl_simu_add_fwd('1', "/ccnl/simu", 'C');
/*
for (i = 0; i < 5; i++)
ccnl_dump(0, CCNL_RELAY, relays+i);
*/
// turn node 'C' into a repository for three movies
for (i = 0; i < SIMU_NUMBER_OF_CHUNKS; i++) {
ccnl_simu_add2cache('C', "/ccnl/simu/movie1", i, dat, sizeof(dat));
ccnl_simu_add2cache('C', "/ccnl/simu/movie2", i, dat, sizeof(dat));
ccnl_simu_add2cache('C', "/ccnl/simu/movie3", i, dat, sizeof(dat));
}
ccnl_set_timer(5000, ccnl_simu_ethernet, 0, 0);
// start clients:
ccnl_set_timer( 500000, ccnl_simu_client_start, char2relay('A'), 0);
ccnl_set_timer(1000000, ccnl_simu_client_start, char2relay('B'), 0);
phaseOne = 1;
printf("Press ENTER to start the simulation\n");
while (getchar() != '\n');
return 0;
}
void
ccnl_simu_phase_two(void *ptr, void *dummy)
{
phaseOne = 0;
ccnl_set_timer(0, ccnl_simu_client_start, char2relay('A'), 0);
ccnl_set_timer(500000, ccnl_simu_client_start, char2relay('B'), 0);
}
// ----------------------------------------------------------------------
void
simu_eventloop()
{
static struct timeval now;
long usec;
while (eventqueue) {
struct ccnl_timer_s *t = eventqueue;
// printf(" looping now %g\n", CCNL_NOW());
gettimeofday(&now, 0);
usec = timevaldelta(&(t->timeout), &now);
if (usec >= 0) {
// usleep(usec);
struct timespec ts;
ts.tv_sec = usec / 1000000;
ts.tv_nsec = 1000 * (usec % 1000000);
nanosleep(&ts, NULL);
}
t = eventqueue;
eventqueue = t->next;
if (t->fct) {
void (*fct)(char,int) = t->fct;
char c = t->node;
int i = t->intarg;
ccnl_free(t);
(fct)(c, i);
} else if (t->fct2) {
void (*fct2)(void*,void*) = t->fct2;
void *aux1 = t->aux1;
void *aux2 = t->aux2;
ccnl_free(t);
(fct2)(aux1, aux2);
}
}
DEBUGMSG(0, "simu event loop: no more events to handle\n");
}
#define end_log(CHANNEL) do { if (CHANNEL){ \
fprintf(CHANNEL, "#end\n"); \
fclose(CHANNEL); \
CHANNEL = NULL; \
} } while(0)
void
ccnl_simu_cleanup(void *dummy, void *dummy2)
{
printf("Simulation ended; press ENTER to terminate\n");
while (getchar() != '\n');
int i;
for (i = 0; i < 5; i++) {
struct ccnl_relay_s *relay = relays + i;
if (relay->aux) {
ccnl_free(relay->aux);
relay->aux = NULL;
}
if (relay->stats) {
end_log(relay->stats->ofp_s);
end_log(relay->stats->ofp_r);
end_log(relay->stats->ofp_q);
ccnl_free(relay->stats);
relay->stats = NULL;
}
ccnl_core_cleanup(relay);
}
while(eventqueue)
ccnl_rem_timer(eventqueue);
while(etherqueue) {
struct ccnl_ethernet_s *e = etherqueue->next;
ccnl_free(etherqueue);
etherqueue = e;
}
#ifdef USE_SCHEDULER
ccnl_sched_cleanup();
#endif
#ifdef USE_DEBUG_MALLOC
debug_memdump();
#endif
}
// ----------------------------------------------------------------------
int
main(int argc, char **argv)
{
int opt;
int max_cache_entries = CCNL_DEFAULT_MAX_CACHE_ENTRIES;
// srand(time(NULL));
srandom(time(NULL));
while ((opt = getopt(argc, argv, "hc:g:i:v:")) != -1) {
switch (opt) {
case 'c':
max_cache_entries = atoi(optarg);
break;
case 'g':
inter_packet_interval = atoi(optarg);
break;
case 'i':
inter_ccn_interval = atoi(optarg);
break;
case 'v':
debug_level = atoi(optarg);
break;
case 'h':
default:
fprintf(stderr, "usage: %s [-h] [-c MAX_CONTENT_ENTRIES] "
"[-g MIN_INTER_PACKET_INTERVAL] "
"[-i MIN_INTER_CCNMSG_INTERVAL] "
"[-v DEBUG_LEVEL]\n",
argv[0]);
exit(EXIT_FAILURE);
}
}
time(&relays[0].startup_time);
DEBUGMSG(1, "This is ccn-lite-simu, starting at %s",
ctime(&relays[0].startup_time) + 4);
DEBUGMSG(1, " ccnl-core: %s\n", CCNL_VERSION);
DEBUGMSG(1, " compile time: %s %s\n", __DATE__, __TIME__);
DEBUGMSG(1, " compile options: %s\n", compile_string());
ccnl_simu_init(max_cache_entries);
DEBUGMSG(1, "simulation starts\n");
simu_eventloop();
DEBUGMSG(1, "simulation ends\n");
return -1;
}
// eof