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