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