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