-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraffic.py
90 lines (67 loc) · 1.77 KB
/
traffic.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
import random
class Traffic:
def __init__(self):
self.length = 0
__metaclass__ = TrafficLength
def __iter__(self):
self.n = 0
return self
def __next__(self):
raise StopIteration
def __len__(self):
return self.length
class TrafficLength(type):
def __len__(self):
return self.length
class RandomTraffic(Traffic):
def __init__(self, topo, packets = 1000, seed = 65137):
self.prgn = random.Random(seed)
self.edges = topo.edge_nodes()
self.length = packets
def next(self):
if self.n < self.length:
self.n += 1
return (
self.edges[self.prgn.randint(0, len(self.edges)-1)],
self.edges[self.prgn.randint(0, len(self.edges)-1)],
)
else:
raise StopIteration
class RandomGeneratedTraffic(Traffic):
def __init__(self, topo, packets = 1000, seed = 65137):
self.prgn = random.Random(seed)
self.edges = topo.edge_nodes()
self.length = packets
self.packets = []
for i in range(0, packets):
self.packets.append((
self.edges[self.prgn.randint(0, len(self.edges)-1)],
self.edges[self.prgn.randint(0, len(self.edges)-1)],
))
def next(self):
if self.n < len(self.packets):
ret = self.packets[self.n]
self.n += 1
return ret
else:
raise StopIteration
class RandomMappedTraffic(Traffic):
def __init__(self, topo, data = [], seed = 65137):
self.prng = random.Random(seed)
self.edges = topo.edge_nodes()
self.data = data
self.item2node = {}
for record in data:
for item in record:
if item not in self.item2node:
self.item2node[item] = self.edges[self.prng.randint(0, len(self.edges)-1)]
def next(self):
if self.n < len(self.data):
src_item, dst_item = self.data[self.n]
self.n += 1
return (
self.item2node[src_item],
self.item2node[dst_item],
)
else:
raise StopIteration