Newer
Older
from argparse import ArgumentParser
from collections import defaultdict, OrderedDict
from itertools import combinations
import math
import random
import sys
import simpy
import yaml
import networkx as nx
from .communities import types as communities
from .routers import types as routers
from .traces import types as traces
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
class Network:
''''''
def __init__(self, env,
packets=None,
node_factory=None,
community=None,
trace=None):
''''''
self.env = env
# contact trace
if trace is None:
trace = traces['random']()
self.trace = trace
self.trace.start(env, self)
# community detection
self.community = community
if community is not None:
self.community.start(env, self)
# packet generation
'''
if packets is None:
packets = packets['uniform']()
self.packets = packets
self.packets_proc = self.packets.process(self.env, self)
'''
# create node network
if node_factory is None:
node_factory = NodeFactory(tick_rate=1, router=routers['direct'])
self.nodes = [
node_factory(env, self, nid)
for nid in range(self.trace.nodes)
]
self.links = [
(a, b)
for a, b in combinations(self.nodes, 2)
]
# set up networkx graph
self.graph = nx.Graph()
self.graph.add_nodes_from(self.nodes)
self.graph.add_edges_from([
(a, b, { 'state': False })
for a, b in self.links
])
# TODO: better packet generation
self.packets = []
for i in range(100):
source, dest = random.choice(self.links)
packet = Packet(i, source, dest, trace.duration, None)
self.packets.append(packet)
source.recv(packet)
def set_link(self, a, b, state):
if isinstance(a, int):
a = self.nodes[a]
if isinstance(b, int):
b = self.nodes[b]
edge = self[a][b]
if edge['state'] == state:
return
if state is None:
state = not edge['state']
edge['state'] = state
if self.community:
self.community.set_link(a, b, state, self.env.now)
def toggle_link(self, a, b):
self.set_link(a, b, None)
def send_link(self, a, b, packet):
''''''
if self[a][b]['state']:
# TODO: transfer delay
b.recv(packet)
else:
raise Exception('Nodes {} and {} not connected'.format(a, b))
def __getitem__(self, node):
''''''
return self.graph[node]
def NodeFactory(router, **kwargs):
nid = -1
def factory(env, network, nid):
nid += 1
return Node(env, network, nid, router=router, **kwargs)
return factory
class Node:
''''''
def __init__(self, env, network, nid,
tick_time=1, router=None):
''''''
self.env = env
self.network = network
self.id = nid
self.tick_time = tick_time
self.ticker = env.process(self.tick())
self.buffer = Buffer(self.env)
# bind router as a class method
if router is None:
router = routers['direct']
self.router = router.__get__(self, Node)
self.router_state = {}
def tick(self):
''''''
while True:
packets_to_delete = []
for packet in self.buffer:
if packet.ttl < self.env.now:
packets_to_delete.append(packet)
continue
if self.router(packet, self.router_state):
packets_to_delete.append(packet)
for packet in packets_to_delete:
self.buffer.remove(packet)
yield self.env.timeout(self.tick_time)
def send(self, to, packet):
# TODO: transfer delay
self.network.send_link(self, to, packet)
def recv(self, packet):
if packet.destination == self:
packet.recv()
else:
self.buffer.add(packet)
@property
def community(self):
return self.network.community[self]
@property
def links(self):
'''
Returns a list of connected links.
'''
links = {
met: data
for met, data in self.network[self].items()
if data['state']
}
return links
def __repr__(self):
return 'Node(id={})'.format(self.id)
class Buffer:
def __init__(self, env, capacity=0):
self.env = env
if capacity <= 0:
self.capacity = float('inf')
else:
self.capacity = capacity
self.buffer = OrderedDict()
self.used = 0
def add(self, packet):
if self.used < self.capacity:
self.used += 1
self.buffer[packet] = None
else:
raise Exception('buffer full')
def remove(self, packet):
self.used -= 1
del self.buffer[packet]
def __contains__(self, packet):
return packet in self.buffer
def __iter__(self):
return iter(self.buffer)
def __len__(self):
return len(self.buffer)
class Packet:
def __init__(self, id, source, destination, ttl, payload):
self.id = id
self.source = source
self.destination = destination
self.ttl = ttl
self.payload = payload
self.recieved = False
self.recieved_count = 0
def recv(self):
self.recieved = True
self.recieved_count += 1
def __str__(self):
return "Packet(id={}, source={}, destination={})".format(
self.id,
self.source,
self.destination
)