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