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.node1node1 = sta
30  self.devdev = dev
31  self.node2node2 = None # ap
32  self.canvas_itemcanvas_item = GooCanvas.CanvasGroup(parent=parent_canvas_item)
33  self.invisible_lineinvisible_line = GooCanvas.CanvasPolyline(parent=self.canvas_itemcanvas_item,
34  line_width=25.0,
35  visibility=GooCanvas.CanvasItemVisibility.HIDDEN)
36  self.visible_linevisible_line = GooCanvas.CanvasPolyline(parent=self.canvas_itemcanvas_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_itemcanvas_item.pyviz_object = self
44  self.canvas_itemcanvas_item.lower(None)
45  self.set_apset_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.node2node2:
54  return
55  if self.node2node2 is not None:
56  self.node2node2.remove_link(self)
57  self.node2node2 = ap
58  if self.node2node2 is None:
59  self.canvas_itemcanvas_item.set_property("visibility", GooCanvas.CanvasItemVisibility.HIDDEN)
60  else:
61  self.node2node2.add_link(self)
62  self.canvas_itemcanvas_item.set_property("visibility", GooCanvas.CanvasItemVisibility.VISIBLE)
63  self.update_pointsupdate_points()
64 
65  def update_points(self):
66  """! Update points function.
67  @param self The object pointer.
68  @return none
69  """
70  if self.node2node2 is None:
71  return
72  pos1_x, pos1_y = self.node1node1.get_position()
73  pos2_x, pos2_y = self.node2node2.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_linevisible_line.set_property("points", points)
78  self.invisible_lineinvisible_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_itemcanvas_item.destroy()
86  self.node1node1 = None
87  self.node2node2 = 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.node1node1.get_position()
96  pos2_x, pos2_y = self.node2node2.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.devdev.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.node1node1.node_index, self.node2node2.node_index, d,
105  mac.GetSsid(), mac.GetBssid()))
106 
107 
109 
113  def __init__(self, dummy_viz):
114  """! Initialize function.
115  @param self The object pointer.
116  @param dummy_viz A dummy visualizer
117  """
118  self.access_pointsaccess_points = {} # bssid -> node
119  self.stationsstations = [] # list of (sta_netdevice, viz_node, wifi_link)
120 
121  def scan_nodes(self, viz):
122  """! Scan nodes function.
123  @param self The object pointer.
124  @param viz The visualizer object
125  @return none
126  """
127  for (sta_netdevice, viz_node, wifi_link) in self.stationsstations:
128  wifi_link.destroy()
129 
130  self.access_pointsaccess_points = {}
131  self.stationsstations = []
132 
133  for node in viz.nodes.values():
134  ns3_node = ns.network.NodeList.GetNode(node.node_index)
135  for devI in range(ns3_node.GetNDevices()):
136  dev = ns3_node.GetDevice(devI)
137  if not isinstance(dev, ns.wifi.WifiNetDevice):
138  continue
139  wifi_mac = dev.GetMac()
140  if isinstance(wifi_mac, ns.wifi.StaWifiMac):
141  wifi_link = WifiLink(viz.links_group, node, dev)
142  self.stationsstations.append((dev, node, wifi_link))
143  elif isinstance(wifi_mac, ns.wifi.ApWifiMac):
144  bssid = ns.network.Mac48Address.ConvertFrom(dev.GetAddress())
145  self.access_pointsaccess_points[str(bssid)] = node
146  #print "APs: ", self.access_points
147  #print "STAs: ", self.stations
148 
150  """! Simulation Periodic Update function.
151  @param self The object pointer.
152  @param viz The visualizer object
153  @return none
154  """
155  for (sta_netdevice, viz_node, wifi_link) in self.stationsstations:
156  if not sta_netdevice.IsLinkUp():
157  wifi_link.set_ap(None)
158  continue
159  bssid = str(sta_netdevice.GetMac().GetBssid())
160  if bssid == '00:00:00:00:00:00':
161  wifi_link.set_ap(None)
162  continue
163  ap = self.access_pointsaccess_points[bssid]
164  wifi_link.set_ap(ap)
165 
166  def update_view(self, viz):
167  """! Update View function.
168  @param self The object pointer.
169  @param viz The visualizer object
170  @return none
171  """
172  for (dummy_sta_netdevice, dummy_viz_node, wifi_link) in self.stationsstations:
173  if wifi_link is not None:
174  wifi_link.update_points()
175 
176 
177 def register(viz):
178  link_monitor = WifiLinkMonitor(viz)
179  viz.connect("simulation-periodic-update", link_monitor.simulation_periodic_update)
180  viz.connect("update-view", link_monitor.update_view)
181  viz.connect("topology-scanned", link_monitor.scan_nodes)
def transform_distance_canvas_to_simulation(d)
Definition: base.py:91