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