-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.py
146 lines (118 loc) · 4.36 KB
/
overlay.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
"""Modelling the overlay network"""
from enum import Enum
from matplotlib import pyplot as plt
import numpy as np
import networkx as nx
class BlockKind(Enum):
"""Types of overlay nodes"""
source = 1
sink = 2
intermediate = 3
class OverlayNetwork:
"""Model of the overlay network"""
def __init__(self):
self.graph = nx.DiGraph()
self._last_id = 0
self.sources = set()
self.intermediates = set()
self.sink = None
def blocks(self):
"""Returns all blocks of the overlay"""
return self.graph.nodes()
def links(self):
"""Returns all links of the overlay"""
return self.graph.edges()
def add_source(self, requirement=0, datarate=2.0, name=None):
"""Adds a new source node to the overlay and returns it"""
block = self._add_block(name, BlockKind.source, requirement, datarate)
self.sources.add(block)
return block
def add_intermediate(self, requirement=0, datarate=2.0, name=None):
"""Adds a new intermediate node to the overlay and returns it"""
block = self._add_block(
name, BlockKind.intermediate, requirement, datarate
)
self.intermediates.add(block)
return block
def set_sink(self, requirement=0, datarate=2.0, name=None):
"""Creates a new node, sets it as the sink node and returns it"""
block = self._add_block(name, BlockKind.sink, requirement, datarate)
self.sink = block
return block
def _add_block(self, name, kind: BlockKind, requirement, datarate):
"""Adds a block to the overlay network"""
if name is None:
name = self._generate_name()
self.graph.add_node(
name, kind=kind, requirement=requirement, datarate=datarate
)
return name
def requirement(self, block):
"""Returns the resource requirement of a given block"""
if block is None:
return 0
return self.graph.node[block]["requirement"]
def datarate(self, block):
"""Returns the datarate requirement a given block"""
if block is None:
return 0
return self.graph.node[block]["datarate"]
def _generate_name(self):
self._last_id += 1
return f"B{self._last_id}"
def add_link(self, source: str, sink: str):
"""Adds a link between two blocks in the overlay network"""
self.graph.add_edge(source, sink)
def _block_to_verbose_str(self, block):
requirement = round(self.requirement(block), 1)
datarate = round(self.datarate(block), 1)
return (
f'(name="{block}", requirement={requirement}, datarate={datarate})'
)
def __str__(self):
result = "overlay = OverlayNetwork()\n"
for source in self.sources:
s = self._block_to_verbose_str(source)
result += f"{source} = overlay.add_source{s}\n"
for intermediate in self.intermediates:
i = self._block_to_verbose_str(intermediate)
result += f"{intermediate} = overlay.add_intermediate{i}\n"
s = self._block_to_verbose_str(self.sink)
result += f"{self.sink} = overlay.set_sink{s}\n"
links = self.graph.edges()
for (u, v) in links:
result += f"overlay.add_link({u}, {v})\n"
return result
def draw_overlay(
overlay: OverlayNetwork,
sources_color="red",
sink_color="yellow",
intermediates_color="green",
):
"""Draws a given OverlayNetwork"""
shared_args = {
"G": overlay.graph,
"pos": nx.spring_layout(overlay.graph),
"node_size": 450,
"node_shape": "s",
}
nx.draw_networkx_nodes(
nodelist=list(overlay.sources), node_color=sources_color, **shared_args
)
nx.draw_networkx_nodes(
nodelist=list(overlay.intermediates),
node_color=intermediates_color,
**shared_args,
)
nx.draw_networkx_nodes(
nodelist=[overlay.sink], node_color=sink_color, **shared_args
)
nx.draw_networkx_labels(**shared_args)
nx.draw_networkx_edges(**shared_args)
# positions are arbitrary for an overlay
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
if __name__ == "__main__":
from generator import DefaultGenerator
draw_overlay(DefaultGenerator().random_overlay(2, rand=np.random))
plt.show()