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