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