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