A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ipv4_routing_table.py
1 import gtk
2 
3 import ns.core
4 import ns.network
5 import ns.internet
6 
7 from visualizer.base import InformationWindow
8 
10  (
11  COLUMN_DESTINATION,
12  COLUMN_NEXT_HOP,
13  COLUMN_INTERFACE,
14  COLUMN_TYPE,
15  COLUMN_PRIO
16  ) = range(5)
17 
18  def __init__(self, visualizer, node_index):
19  InformationWindow.__init__(self)
20  self.win = gtk.Dialog(parent=visualizer.window,
21  flags=gtk.DIALOG_DESTROY_WITH_PARENT|gtk.DIALOG_NO_SEPARATOR,
22  buttons=(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE))
23  self.win.connect("response", self._response_cb)
24  self.win.set_title("IPv4 routing table for node %i" % node_index)
25  self.visualizer = visualizer
26  self.node_index = node_index
27 
28  self.table_model = gtk.ListStore(str, str, str, str, int)
29 
30  treeview = gtk.TreeView(self.table_model)
31  treeview.show()
32  sw = gtk.ScrolledWindow()
33  sw.set_properties(hscrollbar_policy=gtk.POLICY_AUTOMATIC,
34  vscrollbar_policy=gtk.POLICY_AUTOMATIC)
35  sw.show()
36  sw.add(treeview)
37  self.win.vbox.add(sw)
38  self.win.set_default_size(600, 300)
39 
40  # Dest.
41  column = gtk.TreeViewColumn('Destination', gtk.CellRendererText(),
42  text=self.COLUMN_DESTINATION)
43  treeview.append_column(column)
44 
45  # Next hop
46  column = gtk.TreeViewColumn('Next hop', gtk.CellRendererText(),
47  text=self.COLUMN_NEXT_HOP)
48  treeview.append_column(column)
49 
50  # Interface
51  column = gtk.TreeViewColumn('Interface', gtk.CellRendererText(),
52  text=self.COLUMN_INTERFACE)
53  treeview.append_column(column)
54 
55  # Type
56  column = gtk.TreeViewColumn('Type', gtk.CellRendererText(),
57  text=self.COLUMN_TYPE)
58  treeview.append_column(column)
59 
60  # Prio
61  column = gtk.TreeViewColumn('Prio', gtk.CellRendererText(),
62  text=self.COLUMN_PRIO)
63  treeview.append_column(column)
64 
65  self.visualizer.add_information_window(self)
66  self.win.show()
67 
68  def _response_cb(self, win, response):
69  self.win.destroy()
70  self.visualizer.remove_information_window(self)
71 
72  def update(self):
73  node = ns.network.NodeList.GetNode(self.node_index)
74  ipv4 = node.GetObject(ns.internet.Ipv4.GetTypeId())
75  routing = ipv4.GetRoutingProtocol()
76  if routing is None:
77  return
78 
79  routing_protocols = [] # list of (protocol, type_string, priority)
80 
81  if isinstance(routing, ns.internet.Ipv4StaticRouting):
82  ipv4_routing = routing_protocols.append((routing, "static", 0))
83  elif isinstance(routing, ns.internet.Ipv4ListRouting):
84  list_routing = routing
85  for rI in range(list_routing.GetNRoutingProtocols()):
86  routing, prio = list_routing.GetRoutingProtocol(rI)
87  if isinstance(routing, ns.internet.Ipv4StaticRouting):
88  routing_protocols.append((routing, "static", prio))
89  elif isinstance(routing, ns.internet.Ipv4GlobalRouting):
90  routing_protocols.append((routing, "global", prio))
91  if not routing_protocols:
92  return
93 
94  self.table_model.clear()
95  for route_proto, type_string, prio in routing_protocols:
96  for routeI in range(route_proto.GetNRoutes()):
97  route = route_proto.GetRoute(routeI)
98  tree_iter = self.table_model.append()
99  netdevice = ipv4.GetNetDevice(route.GetInterface())
100  if netdevice is None:
101  interface_name = 'lo'
102  else:
103  interface_name = ns.core.Names.FindName(netdevice)
104  if not interface_name:
105  interface_name = "(interface %i)" % route.GetInterface()
106  self.table_model.set(tree_iter,
107  self.COLUMN_DESTINATION, str(route.GetDest()),
108  self.COLUMN_NEXT_HOP, str(route.GetGateway()),
109  self.COLUMN_INTERFACE, interface_name,
110  self.COLUMN_TYPE, type_string,
111  self.COLUMN_PRIO, prio)
112 
113 
114 def populate_node_menu(viz, node, menu):
115  menu_item = gtk.MenuItem("Show IPv4 Routing Table")
116  menu_item.show()
117 
118  def _show_ipv4_routing_table(dummy_menu_item):
119  ShowIpv4RoutingTable(viz, node.node_index)
120 
121  menu_item.connect("activate", _show_ipv4_routing_table)
122  menu.add(menu_item)
123 
124 def register(viz):
125  viz.connect("populate-node-menu", populate_node_menu)