A Discrete-Event Network Simulator
API
olsr.py
Go to the documentation of this file.
1 from gi.repository import Gtk
2 from gi.repository import Gdk
3 
4 import ns.core
5 import ns.network
6 import ns.internet
7 import ns.olsr
8 
9 from visualizer.base import InformationWindow
10 
11 
13 
21  (
22  COLUMN_DESTINATION,
23  COLUMN_NEXT_HOP,
24  COLUMN_INTERFACE,
25  COLUMN_NUM_HOPS,
26  ) = range(4)
27 
28  def __init__(self, visualizer, node_index):
29  """!
30  Initializer
31  @param self this object
32  @param visualizer visualizer object
33  @param node_index the node index
34  @return none
35  """
36  InformationWindow.__init__(self)
37  self.win = 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.win.set_default_size(Gdk.Screen.width()/2, Gdk.Screen.height()/2)
41  self.win.connect("response", self._response_cb)
42  self.win.set_title("OLSR routing table for node %i" % node_index)
43  self.visualizer = visualizer
44  self.node_index = node_index
45 
46  self.table_model = Gtk.ListStore(str, str, str, int)
47 
48  treeview = Gtk.TreeView(self.table_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.win.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.visualizer.add_information_window(self)
78  self.win.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.win.destroy()
89  self.visualizer.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_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_model.clear()
103  for route in olsr.GetRoutingTableEntries():
104  tree_iter = self.table_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_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)
def update(self)
Update function.
Definition: olsr.py:91
def register(viz)
Definition: olsr.py:135
table_model
table model
Definition: olsr.py:46
visualizer
visualizer
Definition: olsr.py:43
def __init__(self, visualizer, node_index)
Initializer.
Definition: olsr.py:28
node_index
node index
Definition: olsr.py:44
def _response_cb(self, win, response)
Initializer.
Definition: olsr.py:80
ShowOlsrRoutingTable class.
Definition: olsr.py:12
InformationWindow class.
Definition: base.py:35
def populate_node_menu(viz, node, menu)
Definition: olsr.py:119