A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
show_last_packets.py
Go to the documentation of this file.
1from gi.repository import GObject
2from gi.repository import Gtk
3
4from ns import ns
5
6try:
7 from nsnam.visualizer.base import InformationWindow
8except ModuleNotFoundError:
9 from visualizer.base import InformationWindow
10
11from kiwi.ui.objectlist import ObjectList, Column
12
13
15
39 class PacketList(Gtk.ScrolledWindow):
40 """
41 PacketList class
42 """
43
45 (
46 COLUMN_TIME,
47 COLUMN_INTERFACE,
48 COLUMN_SIZE,
49 COLUMN_CONTENTS,
50 ) = range(4)
51
52 def __init__(self):
53 """
54 Initializer
55 @param self this object
56 """
58 self.set_properties(hscrollbar_policy=Gtk.PolicyType.AUTOMATIC,
59 vscrollbar_policy=Gtk.PolicyType.AUTOMATIC)
60 self.table_model = Gtk.ListStore(*([str]*4))
61 treeview = Gtk.TreeView(self.table_model)
62 treeview.show()
63 self.add(treeview)
64
65 def add_column(descr, colid):
66 column = Gtk.TreeViewColumn(descr, Gtk.CellRendererText(), text=colid)
67 treeview.append_column(column)
68
69 add_column("Time", self.COLUMN_TIME)
70 add_column("Interface", self.COLUMN_INTERFACE)
71 add_column("Size", self.COLUMN_SIZE)
72 add_column("Contents", self.COLUMN_CONTENTS)
73
74 def update(self, node, packet_list):
75 """!
76 Update function
77 @param self this object
78 @param node the node
79 @param packet_list packet list
80 @return none
81 """
82 self.table_model.clear()
83 for sample in packet_list:
84 tree_iter = self.table_model.append()
85 if sample.device is None:
86 interface_name = "(unknown)"
87 else:
88 interface_name = ns.core.Names.FindName(sample.device)
89 if not interface_name:
90 interface_name = "(interface %i)" % sample.device.GetIfIndex()
91 self.table_model.set(tree_iter,
92 self.COLUMN_TIME, str(sample.time.GetSeconds()),
93 self.COLUMN_INTERFACE, interface_name,
94 self.COLUMN_SIZE, str(sample.packet.GetSize ()),
95 self.COLUMN_CONTENTS, str(sample.packet)
96 )
97
98
99 def __init__(self, visualizer, node_index):
100 """!
101 Initializer
102 @param self this object
103 @param visualizer the visualizer object
104 @param node_index the node index
105 """
106 InformationWindow.__init__(self)
107 self.win = Gtk.Dialog(parent=visualizer.window,
108 flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
109 buttons=("_Close", Gtk.ResponseType.CLOSE))
110 self.win.connect("response", self._response_cb)
111 self.win.set_title("Last packets for node %i" % node_index)
112 self.visualizer = visualizer
113 self.viz_node = visualizer.get_node(node_index)
114 self.node = ns.network.NodeList.GetNode(node_index)
115
116 def smart_expand(expander, vbox):
117 if expander.get_expanded():
118 vbox.set_child_packing(expander, expand=True, fill=True, padding=0, pack_type=Gtk.PACK_START)
119 else:
120 vbox.set_child_packing(expander, expand=False, fill=False, padding=0, pack_type=Gtk.PACK_START)
121
122 main_hbox = Gtk.HBox(False, 4)
123 main_hbox.show()
124 main_vbox = Gtk.VBox(False, 4)
125 main_vbox.show()
126 self.win.vbox.add(main_hbox)
127 main_hbox.add(main_vbox)
128
129 self.tx_list = self.PacketList()
130 self.tx_list.show()
131 group = Gtk.Expander("Last transmitted packets")
132 group.show()
133 group.add(self.tx_list)
134 main_vbox.pack_start(group, expand=False, fill=False)
135 group.connect_after("activate", smart_expand, main_vbox)
136
137 self.rx_list = self.PacketList()
138 self.rx_list.show()
139 group = Gtk.Expander("Last received packets")
140 group.show()
141 group.add(self.rx_list)
142 main_vbox.pack_start(group, expand=False, fill=False)
143 group.connect_after("activate", smart_expand, main_vbox)
144
145 self.drop_list = self.PacketList()
146 self.drop_list.show()
147 group = Gtk.Expander("Last dropped packets")
148 group.show()
149 group.add(self.drop_list)
150 main_vbox.pack_start(group, expand=False, fill=False)
151 group.connect_after("activate", smart_expand, main_vbox)
152
153
154 # Packet Filter
155
156 # - options
157 self.packet_capture_options = ns.visualizer.PyViz.PacketCaptureOptions()
158 self.packet_capture_options.numLastPackets = 100
159
160 packet_filter_vbox = Gtk.VBox(False, 4)
161 packet_filter_vbox.show()
162 main_hbox.add(packet_filter_vbox)
163
164 sel_buttons_box = Gtk.HButtonBox()
165 sel_buttons_box.show()
166 packet_filter_vbox.pack_start(sel_buttons_box, False, False, 4)
167 select_all_button = GObject.new(Gtk.Button, label="Sel. All", visible=True)
168 select_none_button = GObject.new(Gtk.Button, label="Sel. None", visible=True)
169 sel_buttons_box.add(select_all_button)
170 sel_buttons_box.add(select_none_button)
171
172 self.packet_filter_widget = ObjectList([
173 Column('selected', title="Sel.", data_type=bool, editable=True),
174 Column('name', title="Header"),
175 ], sortable=True)
176 self.packet_filter_widget.show()
177 packet_filter_vbox.pack_start(self.packet_filter_widget, True, True, 4)
178
179 class TypeIdConfig(object):
180 __slots__ = ['name', 'selected', 'typeid']
181
182 self.packet_filter_list = [] # list of TypeIdConfig instances
183
184 Header = ns.core.TypeId.LookupByName("ns3::Header")
185 Trailer = ns.core.TypeId.LookupByName("ns3::Trailer")
186 for typeid_i in range(ns.core.TypeId.GetRegisteredN()):
187 typeid = ns.core.TypeId.GetRegistered(typeid_i)
188 # check if this is a header or trailer subtype
189 typeid_tmp = typeid
190 type_is_good = False
191 while 1:
192 if typeid_tmp == Header or typeid_tmp == Trailer:
193 type_is_good = True
194 break
195 if typeid_tmp.HasParent():
196 typeid_tmp = typeid_tmp.GetParent()
197 else:
198 break
199 if not type_is_good:
200 continue
201 if typeid in [Header, Trailer]:
202 continue
203 c = TypeIdConfig()
204 c.selected = True
205 c.name = typeid.GetName()
206 c.typeid = typeid
207 self.packet_filter_list.append(c)
208 self.packet_filter_widget.add_list(self.packet_filter_list)
209
210 def update_capture_options():
211 if self.op_AND_button.props.active:
212 self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_AND
213 else:
214 self.packet_capture_options.mode = ns.visualizer.PyViz.PACKET_CAPTURE_FILTER_HEADERS_OR
215 self.packet_capture_options.numLastPackets = 100
216 self.packet_capture_options.headers = [c.typeid for c in self.packet_filter_list if c.selected]
217 self.visualizer.simulation.lock.acquire()
218 try:
219 self.visualizer.simulation.sim_helper.SetPacketCaptureOptions(
220 self.node.GetId(), self.packet_capture_options)
221 finally:
222 self.visualizer.simulation.lock.release()
223
224 def sel_all_cb(bt):
225 for c in self.packet_filter_list:
226 c.selected = True
227 self.packet_filter_widget.refresh()
228 update_capture_options()
229
230 def sel_none_cb(bt):
231 for c in self.packet_filter_list:
232 c.selected = False
233 self.packet_filter_widget.refresh()
234 update_capture_options()
235
236 select_all_button.connect("clicked", sel_all_cb)
237 select_none_button.connect("clicked", sel_none_cb)
238
239 op_buttons_box = Gtk.HButtonBox()
240 op_buttons_box.show()
241 packet_filter_vbox.pack_start(op_buttons_box, False, False, 4)
242 self.op_AND_button = GObject.new(Gtk.RadioButton, label="AND", visible=True)
243 self.op_OR_button = GObject.new(Gtk.RadioButton, label="OR", visible=True, group=self.op_AND_button)
244 op_buttons_box.add(self.op_AND_button)
245 op_buttons_box.add(self.op_OR_button)
246 self.op_OR_button.props.active = True
247
248 self.op_AND_button.connect("toggled", lambda b: update_capture_options())
249
250 def cell_edited(l, obj, attribute):
251 update_capture_options()
252 self.packet_filter_widget.connect("cell-edited", cell_edited)
253
254 update_capture_options()
255
256 self.visualizer.add_information_window(self)
257 self.win.set_default_size(600, 300)
258 self.win.show()
259
260 def _response_cb(self, win, response):
261 """!
262 Response callback function
263 @param self this object
264 @param win the window
265 @param response the response
266 @return none
267 """
268 self.win.destroy()
269 self.visualizer.remove_information_window(self)
270
271 def update(self):
272 """!
273 Update function
274 @param self this object
275 @return none
276 """
277 last_packets = self.visualizer.simulation.sim_helper.GetLastPackets(self.node.GetId())
278
279 self.tx_list.update(self.node, last_packets.lastTransmittedPackets)
280 self.rx_list.update(self.node, last_packets.lastReceivedPackets)
281 self.drop_list.update(self.node, last_packets.lastDroppedPackets)
282
283
284def populate_node_menu(viz, node, menu):
285 menu_item = Gtk.MenuItem("Show Last Packets")
286 menu_item.show()
287
288 def _show_it(dummy_menu_item):
289 ShowLastPackets(viz, node.node_index)
290
291 menu_item.connect("activate", _show_it)
292 menu.add(menu_item)
293
294def register(viz):
295 viz.connect("populate-node-menu", populate_node_menu)
def update(self, node, packet_list)
Update function.
def _response_cb(self, win, response)
Response callback function.
packet_capture_options
packet capture options
packet_filter_widget
packet filter widget
def update(self)
Update function.
def __init__(self, visualizer, node_index)
Initializer.
packet_filter_list
list of TypeIdConfig instances
InformationWindow class.
Definition: base.py:25
def populate_node_menu(viz, node, menu)