A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi_intrastructure_link.py
Go to the documentation of this file.
1import math
2
3try:
4 from ns import ns
5except ModuleNotFoundError:
6 raise SystemExit(
7 "Error: ns3 Python module not found;"
8 " Python bindings may not be enabled"
9 " or your PYTHONPATH might not be properly configured"
10 )
11from gi.repository import GooCanvas
12
13try:
14 from ns3.visualizer.base import Link, transform_distance_canvas_to_simulation
15except ModuleNotFoundError:
16 from visualizer.base import Link, transform_distance_canvas_to_simulation
17
18
19## WifiLink class
21 ## @var node1
22 # sta
23 ## @var dev
24 # dev
25 ## @var node2
26 # ap
27 ## @var canvas_item
28 # parent_canvas_item
29 ## @var invisible_line
30 # invisible line
31 ## @var visible_line
32 # visible line
33 def __init__(self, parent_canvas_item, sta, dev):
34 """! Initialize function.
35 @param self The object pointer.
36 @param parent_canvas_item: parent canvas
37 @param sta The STA node
38 @param dev The dev
39 """
40 super(WifiLink, self).__init__()
41 self.node1 = sta
42 self.dev = dev
43 self.node2 = None # ap
44 self.canvas_item = GooCanvas.CanvasGroup(parent=parent_canvas_item)
45 self.invisible_line = GooCanvas.CanvasPolyline(
46 parent=self.canvas_item,
47 line_width=25.0,
48 visibility=GooCanvas.CanvasItemVisibility.HIDDEN,
49 )
50 self.visible_line = GooCanvas.CanvasPolyline(
51 parent=self.canvas_item,
52 line_width=1.0,
53 stroke_color_rgba=0xC00000FF,
54 line_dash=GooCanvas.CanvasLineDash.newv([2.0, 2.0]),
55 )
56 # self.invisible_line.set_property("pointer-events", (GooCanvas.CanvasPointerEvents.STROKE_MASK
57 # |GooCanvas.CanvasPointerEvents.FILL_MASK
58 # |GooCanvas.CanvasPointerEvents.PAINTED_MASK))
59 self.canvas_item.pyviz_object = self
60 self.canvas_item.lower(None)
61 self.set_ap(None)
62
63 def set_ap(self, ap):
64 """! Set AP.
65 @param self The object pointer.
66 @param ap The AP node
67 @return none
68 """
69 if ap is self.node2:
70 return
71 if self.node2 is not None:
72 self.node2.remove_link(self)
73 self.node2 = ap
74 if self.node2 is None:
75 self.canvas_item.set_property("visibility", GooCanvas.CanvasItemVisibility.HIDDEN)
76 else:
77 self.node2.add_link(self)
78 self.canvas_item.set_property("visibility", GooCanvas.CanvasItemVisibility.VISIBLE)
79 self.update_points()
80
81 def update_points(self):
82 """! Update points function.
83 @param self The object pointer.
84 @return none
85 """
86 if self.node2 is None:
87 return
88 pos1_x, pos1_y = self.node1.get_position()
89 pos2_x, pos2_y = self.node2.get_position()
90 points = GooCanvas.CanvasPoints.new(2)
91 points.set_point(0, pos1_x, pos1_y)
92 points.set_point(1, pos2_x, pos2_y)
93 self.visible_line.set_property("points", points)
94 self.invisible_line.set_property("points", points)
95
96 def destroy(self):
97 """! Destroy function.
98 @param self The object pointer.
99 @return none
100 """
101 self.canvas_item.destroy()
102 self.node1 = None
103 self.node2 = None
104
105 def tooltip_query(self, tooltip):
106 """! Destroy function.
107 @param self The object pointer.
108 @param tooltip The tooltip.
109 @return tooltip
110 """
111 pos1_x, pos1_y = self.node1.get_position()
112 pos2_x, pos2_y = self.node2.get_position()
113 dx = pos2_x - pos1_x
114 dy = pos2_y - pos1_y
115 d = transform_distance_canvas_to_simulation(math.sqrt(dx * dx + dy * dy))
116 mac = self.dev.GetMac()
117 tooltip.set_text(
118 (
119 "WiFi link between STA Node %i and AP Node %i; distance=%.2f m.\n"
120 "SSID: %s\n"
121 "BSSID: %s"
122 )
123 % (self.node1.node_index, self.node2.node_index, d, mac.GetSsid(), mac.GetBssid())
124 )
125
126
127## WifiLinkMonitor class
129 ## @var access_points
130 # bssid -> node
131 ## @var stations
132 # list of (sta_netdevice, viz_node, wifi_link)
133 def __init__(self, dummy_viz):
134 """! Initialize function.
135 @param self The object pointer.
136 @param dummy_viz A dummy visualizer
137 """
138 self.access_points = {} # bssid -> node
139 self.stations = [] # list of (sta_netdevice, viz_node, wifi_link)
140
141 def scan_nodes(self, viz):
142 """! Scan nodes function.
143 @param self The object pointer.
144 @param viz The visualizer object
145 @return none
146 """
147 for sta_netdevice, viz_node, wifi_link in self.stations:
148 wifi_link.destroy()
149
150 self.access_points = {}
151 self.stations = []
152
153 for node in viz.nodes.values():
154 ns3_node = ns.NodeList.GetNode(node.node_index)
155 for devI in range(ns3_node.GetNDevices()):
156 dev = ns3_node.GetDevice(devI)
157 if not isinstance(dev, ns.WifiNetDevice):
158 continue
159 wifi_mac = dev.GetMac()
160 if isinstance(wifi_mac, ns.StaWifiMac):
161 wifi_link = WifiLink(viz.links_group, node, dev)
162 self.stations.append((dev, node, wifi_link))
163 elif isinstance(wifi_mac, ns.ApWifiMac):
164 bssid = ns.Mac48Address.ConvertFrom(dev.GetAddress())
165 self.access_points[str(bssid)] = node
166 # print "APs: ", self.access_points
167 # print "STAs: ", self.stations
168
170 """! Simulation Periodic Update function.
171 @param self The object pointer.
172 @param viz The visualizer object
173 @return none
174 """
175 for sta_netdevice, viz_node, wifi_link in self.stations:
176 if not sta_netdevice.IsLinkUp():
177 wifi_link.set_ap(None)
178 continue
179 bssid = str(sta_netdevice.GetMac().GetBssid())
180 if bssid == "00:00:00:00:00:00":
181 wifi_link.set_ap(None)
182 continue
183 ap = self.access_points[bssid]
184 wifi_link.set_ap(ap)
185
186 def update_view(self, viz):
187 """! Update View function.
188 @param self The object pointer.
189 @param viz The visualizer object
190 @return none
191 """
192 for dummy_sta_netdevice, dummy_viz_node, wifi_link in self.stations:
193 if wifi_link is not None:
194 wifi_link.update_points()
195
196
197def register(viz):
198 link_monitor = WifiLinkMonitor(viz)
199 viz.connect("simulation-periodic-update", link_monitor.simulation_periodic_update)
200 viz.connect("update-view", link_monitor.update_view)
201 viz.connect("topology-scanned", link_monitor.scan_nodes)