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