forked from protocollabs/dmpr-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmpr-simulator.py
executable file
·734 lines (567 loc) · 21.2 KB
/
dmpr-simulator.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time
import sys
import os
import json
import datetime
import argparse
import pprint
import socket
import struct
import functools
import uuid
import random
import math
import addict
import cairo
import shutil
import copy
import lzma
from PIL import Image
import core.dmpr
NO_ROUTER = 100
RTN_MSG_INTERVAL = 30
RTN_MSG_INTERVAL_JITTER = int(RTN_MSG_INTERVAL / 4)
RTN_MSG_HOLD_TIME = RTN_MSG_INTERVAL * 3 + 1
# two stiched images result in 1080p resoltion
SIMU_AREA_X = 960
SIMU_AREA_Y = 1080
DEFAULT_PACKET_TTL = 32
random.seed(1)
# statitics variables follows
NEIGHBOR_INFO_ACTIVE = 0
PATH_LOGS = "logs"
PATH_IMAGES_RANGE = "images-range"
PATH_IMAGES_TX = "images-tx"
PATH_IMAGES_MERGE = "images-merge"
class LoggerClone:
def calc_file_path(self, directory, id_):
try:
val = "{0:05}.log".format(int(id_))
except ValueError:
val = "{}.log".format(str(id_))
return os.path.join(directory, val)
def __init__(self, directory, id_):
file_path = self.calc_file_path(directory, id_)
self._log_fd = open(file_path, 'w')
def msg(self, msg, time=None):
msg = "{} {}\n".format(time, msg)
self._log_fd.write(msg)
debug = msg
info = msg
warning = msg
error = msg
critical = msg
class Router:
def __init__(self, id_, interfaces=None, mm=None, log_directory=None, msg_compress=True):
self.id = id_
self.log = LoggerClone(os.path.join(log_directory, "logs"), id_)
self._log_directory = log_directory
self._do_msg_compress = msg_compress
assert(mm)
self.mm = mm
assert(interfaces)
self.interfaces=interfaces
self.connections = dict()
self.interface_addr = dict()
self._gen_own_networks()
for interface in self.interfaces:
self.connections[interface['name']] = dict()
self.interface_addr[interface['name']] = dict()
self.interface_addr[interface['name']]['v4'] = self._rand_ip_addr("v4")
self.interface_addr[interface['name']]['v6'] = self._rand_ip_addr("v6")
self.transmission_within_second = False
self._setup_core()
def _setup_core(self):
self._core = core.dmpr.DMPR(log=self.log)
self._core.register_routing_table_update_cb(self.routing_table_update_cb, priv_data=None)
self._core.register_msg_tx_cb(self.msg_tx_cb, priv_data=None)
self._core.register_get_time_cb(self.get_time, priv_data=None)
conf = self._gen_configuration()
self._conf = conf
self._core.register_configuration(conf)
def _gen_own_networks(self):
self._own_networks_v4 = list()
for i in range(2):
self._own_networks_v4.append(self._rand_ip_prefix("v4"))
def get_router_by_interface_addr(self, addr):
for router in self.router:
for interface, v in router.interface_addr.keys():
if v['v4'] == addr:
return router
return None
def _generate_configuration(self):
c = dict()
c["id"] = self.id
c["rtn-msg-interval"] = "30"
c["rtn-msg-interval-jitter"] = "7"
c["rtn-msg-hold-time"] = "90"
c["mcast-v4-tx-addr"] = "224.0.1.1"
c["mcast-v6-tx-addr"] = "ff05:0:0:0:0:0:0:2"
c["proto-transport-enable"] = [ "v4" ]
c["interfaces"] = list()
for interface in self.interfaces:
entry = dict()
entry["name"] = interface["name"]
entry["addr-v4"] = self.interface_addr[interface['name']]['v4']
entry["addr-v6"] = self.interface_addr[interface['name']]['v6']
entry["link-characteristics"] = dict()
characteristics = ("bandwidth", "loss")
for characteristic in characteristics:
if characteristic in interface:
entry["link-characteristics"][characteristic] = interface[characteristic]
c["interfaces"].append(entry)
c["networks"] = list()
for ip in self._own_networks_v4:
entry = dict()
entry["proto"] = "v4"
entry["prefix"] = ip[0]
entry["prefix-len"] = ip[1]
c["networks"].append(entry)
return c
def _dump_config(self, config):
dir_ = os.path.join(self._log_directory, "configs")
if not os.path.exists(dir_):
os.makedirs(dir_)
fn = os.path.join(dir_, self.id)
with open(fn, 'w') as fd:
fd.write("\n" * 2)
fd.write(json.dumps(config, sort_keys=True,
indent=4, separators=(',', ': ')))
fd.write("\n" * 3)
def _gen_configuration(self):
conf = self._generate_configuration()
self._dump_config(conf)
return conf
def routing_table_update_cb(self, routing_table, priv_data=None):
""" this function is called when core stated
that the routing table should be updated
"""
self.log.info("routing table update")
self._routing_table = routing_table
def _ip_addr_to_prefix(self, ip_addr):
ip_tuple = ip_addr.split(".")
return "{}.{}.{}.0".format(ip_tuple[0], ip_tuple[1], ip_tuple[2])
def _route_lookup(self, packet):
tos = packet['tos'] # e.g. "lowest-lost"
dst_ip = packet['ip-dst']
dst_ip_normalized = self._ip_addr_to_prefix(dst)
if not tos in self._routing_table:
print("no policy routing table named: {}".format(tos))
print("ICMP, no route to host or take default path?")
return
specific_table = self._routing_table[tos]
for entry in specific_table:
if entry['proto'] != "v4":
print("not ipv4, skipping")
continue
if entry['prefix-len'] != "24":
raise Exception("prefixlen != 24, this is not allowed for the simulation")
if entry['prefix'] == dst_ip_normalized:
print("found, next hop")
return True, entry['next-hop'], entry['interface']
return False, None, None
def _data_packet_forward(self, packet):
""" this is a toy version of a forwarding algorithm.
A first match algorithm because we make sure that no
other network is available in a simulation with the
same root"""
ok, next_hop_ip, interface_name = self._route_lookup(packet)
if not ok:
print("route lookup failed, drop packet, no next hop")
return
r = self.get_router_by_interface_addr(next_hop_ip)
if not r:
print("fck")
return
r.data_packet_rx(packet, interface_name)
def _data_packet_update_ttl(self, packet):
if packet['ttl'] <= 0:
print("ttl is 0, drop packet")
return False
packet['ttl'] -= 1
return True
def _data_packet_reached_dst(self, packet):
dst_ip = packet['dst-ip']
dst_ip_prefix = self._ip_addr_to_prefix(dst_ip)
for addr in self._own_networks_v4:
if addr == dst_ip_prefix:
return True
return False
def data_packet_rx(self, packet, interface):
""" received from a neighbor """
ok = self._data_packet_update_ttl(packet)
if not ok:
return
if self._data_packet_reached_dst(packet):
print("packet reached destination")
return
self._data_packet_forward(packet)
def _msg_compress(self, msg):
msg_bin = msg.encode("ascii", "ignore")
msg_comp = lzma.compress(msg_bin)
return msg_comp
def _msg_decompress(self, msg):
msg_bin = lzma.decompress(msg)
msg_str = msg_bin.decode('ascii')
return msg_str
def msg_tx_cb(self, interface_name, proto, dst_mcast_addr, msg, priv_data=None):
#print(pprint.pformat(msg))
msg_json = json.dumps(msg)
print("message size: {} bytes (uncompressed)".format(len(msg_json)))
if self._do_msg_compress:
msg = self._msg_compress(msg_json)
print("message size: {} bytes (compressed)".format(len(msg)))
""" this function is called when core stated
that a routing message must be transmitted
"""
emsg = "msg transmission [interface:{}, proto:{}, addr:{}]"
self.log.info(emsg.format(interface_name, proto, dst_mcast_addr),
time=self.get_time())
# send message to all connected routers
for r_id, r_obj in self.connections[interface_name].items():
r_obj.msg_rx(interface_name, msg)
def msg_rx(self, interface_name, msg):
if self._do_msg_compress:
msg = self._msg_decompress(msg)
msg_dict = json.loads(msg)
self._core.msg_rx(interface_name, msg_dict)
def register_router(self, r):
self.r = r
def get_time(self, priv_data=None):
return self._time
def step(self, time):
self._time = time
self.mm.step()
self.connect()
self._core.tick()
def start(self, time):
self._time = time
self._core.start()
def stop(self):
self._core.stop()
def coordinates(self):
return self.mm.coordinates()
def connect_links(self, dist, other):
for interface in self.interfaces:
name = interface['name']
range = interface['range']
if dist <= range:
self.connections[name][other.id] = other
else:
if other.id in self.connections[name]:
del self.connections[name][other.id]
def connect(self):
for neighbor in self.r:
if self.id == neighbor.id:
continue
own_cor = self.coordinates()
other_cor = neighbor.coordinates()
dist = math.hypot(own_cor[1] - other_cor[1], own_cor[0] - other_cor[0])
self.connect_links(dist, neighbor)
def _rand_ip_prefix(self, type_):
if type_ == "v4":
addr = random.randint(0, 4000000000)
a = socket.inet_ntoa(struct.pack("!I", addr))
b = a.split(".")
c = "{}.{}.{}.0".format(b[0], b[1], b[2])
return c, 24
if type_ == "v6":
addr = ':'.join('{:x}'.format(random.randint(0, 2**16 - 1)) for i in range(4))
addr += "::"
return addr, 64
raise Exception("only IPv4/IPv6 supported")
def _rand_ip_addr(self, type_):
if type_ == "v4":
addr = random.randint(0, 4000000000)
a = socket.inet_ntoa(struct.pack("!I", addr))
b = a.split(".")
c = "{}.{}.{}.{}".format(b[0], b[1], b[2], b[3])
return c
if type_ == "v6":
return ':'.join('{:x}'.format(random.randint(0, 2**16 - 1)) for i in range(8))
raise Exception("only IPv4/IPv6 supported")
def _id_generator(self):
return str(uuid.uuid1())
def draw_router_loc(ld, area, r, img_idx):
color_interface_links = ((1.0, 0.15, 0.15, 1.0), (0.15, 1.0, 0.15, 1.0),
(0.45, 0.2, 0.15, 1.0), (0.85, 0.5, 0.45, 1.0))
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, area.x, area.y)
ctx = cairo.Context(surface)
ctx.rectangle(0, 0, area.x, area.y)
ctx.set_source_rgba(0.15, 0.15, 0.15, 1.0)
ctx.fill()
for i in range(len(r)):
router = r[i]
x, y = router.coordinates()
color = ((1.0, 1.0, 0.5, 0.05), (1.0, 0.0, 1.0, 0.05),
(0.5, 1.0, 0.0, 0.05), (1.0, 0.5, 1.0, 0.05))
ctx.set_line_width(0.1)
path_thinkness = 5.0
# iterate over links
interfaces_idx = 0
for i, t in enumerate(router.interfaces):
range_ = t['range']
interface_name = t['name']
ctx.set_source_rgba(*color[i])
ctx.move_to(x, y)
ctx.arc(x, y, range_, 0, 2 * math.pi)
ctx.fill()
# draw lines between links
ctx.set_line_width(path_thinkness)
for r_id, r_obj in router.connections[interface_name].items():
other_x, other_y = r_obj.coordinates()
ctx.move_to(x, y)
ctx.set_source_rgba(*color_interface_links[interfaces_idx])
ctx.line_to(other_x, other_y)
ctx.stroke()
path_thinkness -= 1.0
interfaces_idx += 1
for i in range(len(r)):
router = r[i]
x, y = router.coordinates()
# node middle point
ctx.set_line_width(0.0)
ctx.set_source_rgb(0.5, 1, 0.5)
ctx.move_to(x, y)
ctx.arc(x, y, 5, 0, 2 * math.pi)
ctx.fill()
# router id
ctx.set_font_size(10)
ctx.set_source_rgb(0.5, 1, 0.7)
ctx.move_to(x + 10, y + 10)
ctx.show_text(str(router.id))
full_path = os.path.join(ld, "images-range" , "{0:05}.png".format(img_idx))
surface.write_to_png(full_path)
def draw_router_transmission(ld, area, r, img_idx):
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, area.x, area.y)
ctx = cairo.Context(surface)
ctx.rectangle(0, 0, area.x, area.y)
ctx.set_source_rgba(0.15, 0.15, 0.15, 1.0)
ctx.fill()
# transmitting circles
for i in range(len(r)):
router = r[i]
x, y = router.coordinates()
if router.transmission_within_second:
ctx.set_source_rgba(.10, .10, .10, 1.0)
ctx.move_to(x, y)
ctx.arc(x, y, 50, 0, 2 * math.pi)
ctx.fill()
for i in range(len(r)):
router = r[i]
x, y = router.coordinates()
color = ((1.0, 1.0, 0.5, 0.05), (1.0, 0.0, 1.0, 0.05),
(.0, 0.0, 0.5, 0.05), (1.0, 0.5, 1.0, 0.05))
ctx.set_line_width(0.1)
path_thinkness = 6.0
# iterate over links
for i, t in enumerate(router.interfaces):
range_ = t['range']
interface_name = t['name']
# draw lines between links
ctx.set_line_width(path_thinkness)
for r_id, r_obj in router.connections[interface_name].items():
other_x, other_y = r_obj.coordinates()
ctx.move_to(x, y)
ctx.set_source_rgba(.0, .0, .0, .4)
ctx.line_to(other_x, other_y)
ctx.stroke()
path_thinkness -= 4.0
if path_thinkness < 2.0:
path_thinkness = 2.0
# draw dots over all
for i in range(len(r)):
router = r[i]
x, y = router.coordinates()
ctx.set_line_width(0.0)
ctx.set_source_rgb(0, 0, 0)
ctx.move_to(x, y)
ctx.arc(x, y, 5, 0, 2 * math.pi)
ctx.fill()
full_path = os.path.join(ld, "images-tx" , "{0:05}.png".format(img_idx))
surface.write_to_png(full_path)
def image_merge(ld, img_idx):
m_path = os.path.join(ld, "images-range-tx-merge", "{0:05}.png".format(img_idx))
r_path = os.path.join(ld, "images-range", "{0:05}.png".format(img_idx))
t_path = os.path.join(ld, "images-tx", "{0:05}.png".format(img_idx))
images = map(Image.open, [r_path, t_path])
new_im = Image.new('RGB', (1920, 1080))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save(m_path, "PNG")
def draw_images(ld, area, r, img_idx):
draw_router_loc(ld, area, r, img_idx)
draw_router_transmission(ld, area, r, img_idx)
image_merge(ld, img_idx)
def setup_img_folder(scenerio_name):
for path in ("images-range", "images-tx", "images-range-tx-merge"):
f_path = os.path.join("run-data", scenerio_name, path)
if os.path.exists(f_path):
shutil.rmtree(f_path)
os.makedirs(f_path)
def gen_data_packet(src_id, dst_id, tos='low-loss'):
packet = addict.Dict()
packet.src_id = src_id
packet.dst_id = dst_id
packet.ttl = DEFAULT_PACKET_TTL
packet.tos = tos
return packet
def setup_log_folder(scenario_name):
ld = os.path.join("run-data", scenario_name, "logs")
if os.path.exists(ld):
shutil.rmtree(ld)
os.makedirs(ld)
class MobilityArea(object):
def __init__(self, width, height):
self.x = width
self.y = height
class StaticMobilityModel(object):
def __init__(self, area, x, y):
self.area = area
self.x = x
self.y = y
assert(self.x >= 0 and self.x <= self.area.x)
assert(self.y >= 0 and self.y <= self.area.y)
def coordinates(self):
return self.x, self.y
def step(self):
# static, nothing
pass
class MobilityModel(object):
LEFT = 1
RIGHT = 2
UPWARDS = 1
DOWNWARDS = 2
def __init__(self, area):
self.area = area
self.direction_x = random.randint(0, 2)
self.direction_y = random.randint(0, 2)
self.velocity = random.randint(1, 1)
self.x = random.randint(0, self.area.x)
self.y = random.randint(0, self.area.y)
def _move_x(self, x):
if self.direction_x == MobilityModel.LEFT:
x -= self.velocity
if x <= 0:
self.direction_x = MobilityModel.RIGHT
x = 0
elif self.direction_x == MobilityModel.RIGHT:
x += self.velocity
if x >= self.area.x:
self.direction_x = MobilityModel.LEFT
x = self.area.x
else:
pass
return x
def _move_y(self, y):
if self.direction_y == MobilityModel.DOWNWARDS:
y += self.velocity
if y >= self.area.y:
self.direction_y = MobilityModel.UPWARDS
y = self.area.y
elif self.direction_y == MobilityModel.UPWARDS:
y -= self.velocity
if y <= 0:
self.direction_y = MobilityModel.DOWNWARDS
y = 0
else:
pass
return y
def move(self, x, y):
x = self._move_x(x)
y = self._move_y(y)
return x, y
def step(self):
self.x, self.y = self.move(self.x, self.y)
def coordinates(self):
return self.x, self.y
def two_router_static_in_range(scenario_name):
ld = os.path.join("run-data", scenario_name)
interfaces = [
{ "name" : "wifi0", "range" : 200, "bandwidth" : 8000, "loss" : 10},
{ "name" : "tetra0", "range" : 350, "bandwidth" : 1000, "loss" : 5}
]
area = MobilityArea(600, 500)
r = []
mm = StaticMobilityModel(area, 200, 250)
r.append(Router("1", interfaces=interfaces, mm=mm, log_directory=ld))
mm = StaticMobilityModel(area, 400, 250)
r.append(Router("2", interfaces=interfaces, mm=mm, log_directory=ld))
r[0].register_router(r)
r[1].register_router(r)
r[0].connect()
r[1].connect()
r[0].start(0)
r[1].start(0)
SIMU_TIME = 1000
for sec in range(SIMU_TIME):
sep = '=' * 50
print("\n{}\nsimulation time:{:6}/{}\n".format(sep, sec, SIMU_TIME))
for i in range(len(r)):
r[i].step(sec)
draw_images(ld, area, r, sec)
#src_id = random.randint(0, NO_ROUTER - 1)
#dst_id = random.randint(0, NO_ROUTER - 1)
#packet_low_loss = gen_data_packet(src_id, dst_id, tos='low-loss')
#packet_high_througput = gen_data_packet(src_id, dst_id, tos='high-throughput')
#for sec in range(SIMULATION_TIME_SEC):
# for i in range(NO_ROUTER):
# r[i].step()
# dist_update_all(r)
# draw_images(r, sec)
# # inject test data packet into network
# r[src_id].forward_data_packet(packet_low_loss)
# r[src_id].forward_data_packet(packet_high_througput)
def two_hundr_router_static_in_range(scenario_name):
ld = os.path.join("run-data", scenario_name)
interfaces = [
{ "name" : "wifi0", "range" : 200, "bandwidth" : 8000, "loss" : 10},
{ "name" : "tetra0", "range" : 350, "bandwidth" : 1000, "loss" : 5}
]
area = MobilityArea(600, 500)
r = []
no_routers = 20
for i in range(no_routers):
x = random.randint(200, 400)
y = random.randint(200, 300)
mm = StaticMobilityModel(area, x, y)
r.append(Router(str(i), interfaces=interfaces, mm=mm, log_directory=ld))
r[i].register_router(r)
r[i].connect()
r[i].start(0)
SIMU_TIME = 1000
for sec in range(SIMU_TIME):
sep = '=' * 50
print("\n{}\nsimulation time:{:6}/{}\n".format(sep, sec, SIMU_TIME))
for i in range(len(r)):
r[i].step(sec)
draw_images(ld, area, r, sec)
scenarios = [
[ "001-two-router-static-in-range", two_router_static_in_range ],
[ "002-20-router-static-in-range", two_hundr_router_static_in_range ]
]
def die():
print("scenario as argument required")
for scenario in scenarios:
print(" {}".format(scenario[0]))
sys.exit(1)
def main():
if len(sys.argv) > 1:
scenario_name = sys.argv[1]
else:
die()
for scenario in scenarios:
if scenario_name == scenario[0]:
setup_img_folder(scenario[0])
setup_log_folder(scenario[0])
scenario[1](scenario[0])
#cmd = "ffmpeg -framerate 10 -pattern_type glob -i 'images-merge/*.png' -c:v libx264 -pix_fmt yuv420p mdvrd.mp4"
#print("now execute \"{}\" to generate a video".format(cmd))
sys.exit(0)
die()
if __name__ == '__main__':
main()