A Discrete-Event Network Simulator
API
olsr.py
Go to the documentation of this file.
1 from gi.repository import Gdk, Gtk
2 
3 try:
4  from ns3.visualizer.base import InformationWindow
5 except ModuleNotFoundError:
6  from visualizer.base import InformationWindow
7 
8 
9 
11 
29  (
30  COLUMN_DESTINATION,
31  COLUMN_NEXT_HOP,
32  COLUMN_INTERFACE,
33  COLUMN_NUM_HOPS,
34  ) = range(4)
35 
36  def __init__(self, visualizer, node_index):
37  """!
38  Initializer
39  @param self this object
40  @param visualizer visualizer object
41  @param node_index the node index
42  """
43  InformationWindow.__init__(self)
44  self.winwin = Gtk.Dialog(
45  parent=visualizer.window,
46  flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
47  buttons=("_Close", Gtk.ResponseType.CLOSE),
48  )
49  self.winwin.set_default_size(Gdk.Screen.width() / 2, Gdk.Screen.height() / 2)
50  self.winwin.connect("response", self._response_cb_response_cb)
51  self.winwin.set_title("OLSR routing table for node %i" % node_index)
52  self.visualizervisualizer = visualizer
53  self.node_indexnode_index = node_index
54 
55  self.table_modeltable_model = Gtk.ListStore(str, str, str, int)
56 
57  treeview = Gtk.TreeView(self.table_modeltable_model)
58  treeview.show()
59  sw = Gtk.ScrolledWindow()
60  sw.set_properties(
61  hscrollbar_policy=Gtk.PolicyType.AUTOMATIC, vscrollbar_policy=Gtk.PolicyType.AUTOMATIC
62  )
63  sw.show()
64  sw.add(treeview)
65  self.winwin.vbox.add(sw)
66 
67  # Dest.
68  column = Gtk.TreeViewColumn(
69  "Destination", Gtk.CellRendererText(), text=self.COLUMN_DESTINATION
70  )
71  treeview.append_column(column)
72 
73  # Next hop
74  column = Gtk.TreeViewColumn("Next hop", Gtk.CellRendererText(), text=self.COLUMN_NEXT_HOP)
75  treeview.append_column(column)
76 
77  # Interface
78  column = Gtk.TreeViewColumn("Interface", Gtk.CellRendererText(), text=self.COLUMN_INTERFACE)
79  treeview.append_column(column)
80 
81  # Num. Hops
82  column = Gtk.TreeViewColumn("Num. Hops", Gtk.CellRendererText(), text=self.COLUMN_NUM_HOPS)
83  treeview.append_column(column)
84 
85  self.visualizervisualizer.add_information_window(self)
86  self.winwin.show()
87 
88  def _response_cb(self, win, response):
89  """!
90  Initializer
91  @param self this object
92  @param win the window
93  @param response the response
94  @return none
95  """
96  self.winwin.destroy()
97  self.visualizervisualizer.remove_information_window(self)
98 
99  def update(self):
100  """!
101  Update function
102  @param self this object
103  @return none
104  """
105  node = ns.NodeList.GetNode(self.node_indexnode_index)
106  ipv4 = node.GetObject(ns.Ipv4.GetTypeId())
107  if not ns.cppyy.gbl.hasOlsr(ns3_node):
108  return
109  olsr = ns.cppyy.gbl.getNodeOlsr(node)
110 
111  self.table_modeltable_model.clear()
112  for route in olsr.GetRoutingTableEntries():
113  tree_iter = self.table_modeltable_model.append()
114  netdevice = ipv4.GetNetDevice(route.interface)
115  if netdevice is None:
116  interface_name = "lo"
117  else:
118  interface_name = ns.Names.FindName(netdevice)
119  if not interface_name:
120  interface_name = "(interface %i)" % route.interface
121  self.table_modeltable_model.set(
122  tree_iter,
123  self.COLUMN_DESTINATION,
124  str(route.destAddr),
125  self.COLUMN_NEXT_HOP,
126  str(route.nextAddr),
127  self.COLUMN_INTERFACE,
128  interface_name,
129  self.COLUMN_NUM_HOPS,
130  route.distance,
131  )
132 
133 
134 def populate_node_menu(viz, node, menu):
135  ns3_node = ns.NodeList.GetNode(node.node_index)
136  if not ns.cppyy.gbl.hasOlsr(ns3_node):
137  print("No OLSR")
138  return
139 
140  menu_item = Gtk.MenuItem("Show OLSR Routing Table")
141  menu_item.show()
142 
143  def _show_ipv4_routing_table(dummy_menu_item):
144  ShowOlsrRoutingTable(viz, node.node_index)
145 
146  menu_item.connect("activate", _show_ipv4_routing_table)
147  menu.add(menu_item)
148 
149 
150 def register(viz):
151  viz.connect("populate-node-menu", populate_node_menu)
ShowOlsrRoutingTable class.
Definition: olsr.py:10
node_index
node index
Definition: olsr.py:53
def __init__(self, visualizer, node_index)
Initializer.
Definition: olsr.py:36
visualizer
visualizer
Definition: olsr.py:52
table_model
table model
Definition: olsr.py:55
def _response_cb(self, win, response)
Initializer.
Definition: olsr.py:88
def update(self)
Update function.
Definition: olsr.py:99
InformationWindow class.
Definition: base.py:28
def populate_node_menu(viz, node, menu)
Definition: olsr.py:134
def register(viz)
Definition: olsr.py:150