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