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 import goocanvas
5 from visualizer.base import Link, transform_distance_canvas_to_simulation
6 
7 ## WifiLink class
8 class WifiLink(Link):
9  ## @var node1
10  # sta
11  ## @var dev
12  # dev
13  ## @var node2
14  # ap
15  ## @var canvas_item
16  # parent_canvas_item
17  ## @var invisible_line
18  # invisible line
19  ## @var visible_line
20  # visible line
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  self.node1 = sta
29  self.dev = dev
30  self.node2 = None # ap
31  self.canvas_item = goocanvas.Group(parent=parent_canvas_item)
32  self.invisible_line = goocanvas.Polyline(parent=self.canvas_item,
33  line_width=25.0,
34  visibility=goocanvas.ITEM_HIDDEN)
35  self.visible_line = goocanvas.Polyline(parent=self.canvas_item,
36  line_width=1.0,
37  stroke_color_rgba=0xC00000FF,
38  line_dash=goocanvas.LineDash([2.0, 2.0 ]))
39  self.invisible_line.props.pointer_events = (goocanvas.EVENTS_STROKE_MASK
40  |goocanvas.EVENTS_FILL_MASK
41  |goocanvas.EVENTS_PAINTED_MASK)
42  self.canvas_item.set_data("pyviz-object", self)
43  self.canvas_item.lower(None)
44  self.set_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.node2:
53  return
54  if self.node2 is not None:
55  self.node2.remove_link(self)
56  self.node2 = ap
57  if self.node2 is None:
58  self.canvas_item.set_property("visibility", goocanvas.ITEM_HIDDEN)
59  else:
60  self.node2.add_link(self)
61  self.canvas_item.set_property("visibility", goocanvas.ITEM_VISIBLE)
62  self.update_points()
63 
64  def update_points(self):
65  """! Update points function.
66  @param self The object pointer.
67  @return none
68  """
69  if self.node2 is None:
70  return
71  pos1_x, pos1_y = self.node1.get_position()
72  pos2_x, pos2_y = self.node2.get_position()
73  points = goocanvas.Points([(pos1_x, pos1_y), (pos2_x, pos2_y)])
74  self.visible_line.set_property("points", points)
75  self.invisible_line.set_property("points", points)
76 
77  def destroy(self):
78  """! Destroy function.
79  @param self The object pointer.
80  @return none
81  """
82  self.canvas_item.destroy()
83  self.node1 = None
84  self.node2 = None
85 
86  def tooltip_query(self, tooltip):
87  """! Destroy function.
88  @param self The object pointer.
89  @param tooltip The tooltip.
90  @return tooltip
91  """
92  pos1_x, pos1_y = self.node1.get_position()
93  pos2_x, pos2_y = self.node2.get_position()
94  dx = pos2_x - pos1_x
95  dy = pos2_y - pos1_y
96  d = transform_distance_canvas_to_simulation(math.sqrt(dx*dx + dy*dy))
97  mac = self.dev.GetMac()
98  tooltip.set_text(("WiFi link between STA Node %i and AP Node %i; distance=%.2f m.\n"
99  "SSID: %s\n"
100  "BSSID: %s")
101  % (self.node1.node_index, self.node2.node_index, d,
102  mac.GetSsid(), mac.GetBssid()))
103 
104 ## WifiLinkMonitor class
105 class WifiLinkMonitor(object):
106  ## @var access_points
107  # bssid -> node
108  ## @var stations
109  # list of (sta_netdevice, viz_node, wifi_link)
110  def __init__(self, dummy_viz):
111  """! Initialize function.
112  @param self The object pointer.
113  @param dummy_viz A dummy visualizer
114  @return none
115  """
116  self.access_points = {} # bssid -> node
117  self.stations = [] # list of (sta_netdevice, viz_node, wifi_link)
118 
119  def scan_nodes(self, viz):
120  """! Scan nodes function.
121  @param self The object pointer.
122  @param viz The visualizer object
123  @return none
124  """
125  for (sta_netdevice, viz_node, wifi_link) in self.stations:
126  wifi_link.destroy()
127 
128  self.access_points = {}
129  self.stations = []
130 
131  for node in viz.nodes.itervalues():
132  ns3_node = ns.network.NodeList.GetNode(node.node_index)
133  for devI in range(ns3_node.GetNDevices()):
134  dev = ns3_node.GetDevice(devI)
135  if not isinstance(dev, ns.wifi.WifiNetDevice):
136  continue
137  wifi_mac = dev.GetMac()
138  if isinstance(wifi_mac, ns.wifi.StaWifiMac):
139  wifi_link = WifiLink(viz.links_group, node, dev)
140  self.stations.append((dev, node, wifi_link))
141  elif isinstance(wifi_mac, ns.wifi.ApWifiMac):
142  bssid = ns.network.Mac48Address.ConvertFrom(dev.GetAddress())
143  self.access_points[str(bssid)] = node
144  #print "APs: ", self.access_points
145  #print "STAs: ", self.stations
146 
148  """! Simulation Periodic Update function.
149  @param self The object pointer.
150  @param viz The visualizer object
151  @return none
152  """
153  for (sta_netdevice, viz_node, wifi_link) in self.stations:
154  if not sta_netdevice.IsLinkUp():
155  wifi_link.set_ap(None)
156  continue
157  bssid = str(sta_netdevice.GetMac().GetBssid())
158  if bssid == '00:00:00:00:00:00':
159  wifi_link.set_ap(None)
160  continue
161  ap = self.access_points[bssid]
162  wifi_link.set_ap(ap)
163 
164  def update_view(self, viz):
165  """! Update View function.
166  @param self The object pointer.
167  @param viz The visualizer object
168  @return none
169  """
170  for (dummy_sta_netdevice, dummy_viz_node, wifi_link) in self.stations:
171  if wifi_link is not None:
172  wifi_link.update_points()
173 
174 
175 def register(viz):
176  link_monitor = WifiLinkMonitor(viz)
177  viz.connect("simulation-periodic-update", link_monitor.simulation_periodic_update)
178  viz.connect("update-view", link_monitor.update_view)
179  viz.connect("topology-scanned", link_monitor.scan_nodes)
def transform_distance_canvas_to_simulation(d)
Definition: base.py:90