A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4_routing_table.py
Go to the documentation of this file.
1from gi.repository import Gtk
2
3try:
4 from ns3.visualizer.base import InformationWindow
5except ModuleNotFoundError:
6 from visualizer.base import InformationWindow
7
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 ## @var _response_cb
20 # _response_cb function
21 ## @var COLUMN_DESTINATION
22 # COLUMN_DESTINATION constant
23 ## @var COLUMN_NEXT_HOP
24 # COLUMN_NEXT_HOP constant
25 ## @var COLUMN_INTERFACE
26 # COLUMN_INTERFACE constant
27 ## @var COLUMN_TYPE
28 # COLUMN_TYPE constant
29 ## @var COLUMN_PRIO
30 # COLUMN_PRIO constant
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.win = Gtk.Dialog(
48 parent=visualizer.window,
49 flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
50 buttons=("_Close", Gtk.ResponseType.CLOSE),
51 )
52 self.win.connect("response", self._response_cb_response_cb)
53 self.win.set_title("IPv4 routing table for node %i" % node_index)
54 self.visualizer = visualizer
55 self.node_index = node_index
56
57 self.table_model = Gtk.ListStore(str, str, str, str, int)
58
59 treeview = Gtk.TreeView(self.table_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.win.vbox.add(sw)
68 self.win.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.visualizer.add_information_window(self)
93 self.win.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.win.destroy()
104 self.visualizer.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_index)
113 ipv4 = node.GetObject[ns.Ipv4]().__deref__()
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_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_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_model.set(
146 tree_iter,
148 str(route.GetDest()),
150 str(route.GetGateway()),
152 interface_name,
154 type_string,
156 prio,
157 )
158
159
160def 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
171def register(viz):
172 viz.connect("populate-node-menu", populate_node_menu)
_response_cb(self, win, response)
Response callback function.
COLUMN_DESTINATION
COLUMN_DESTINATION constant.
__init__(self, visualizer, node_index)
Initializer.
InformationWindow class.
Definition base.py:28
populate_node_menu(viz, node, menu)