Package network_monitor_udp :: Module udpmonsource
[frames] | no frames]

Source Code for Module network_monitor_udp.udpmonsource

 1  #! /usr/bin/env python 
 2   
 3  from __future__ import with_statement 
 4   
 5  import time 
 6  import sys 
 7   
 8  import roslib; roslib.load_manifest('network_monitor_udp') 
 9  import rospy 
10   
11  from udpmoncli import MonitorSource 
12   
13           
14  if __name__ == "__main__": 
15      from optparse import OptionParser 
16      try: 
17          usage = "usage: %prog [options] <host> <port> <pkt_rate> <pkt_size> " 
18          parser = OptionParser(usage=usage) 
19          parser.add_option("-a", "--address", dest="src_addr", default="0.0.0.0", help="source interface address") 
20          parser.add_option("-s", "--source_id", dest="source_id", type="int", default=None, help="MonitorSource id (use for multiple sources)") 
21          parser.add_option("-m", "--max_return_time", dest="max_return_time", type="float", default=0.0, help="Maximum time for return path (in sec)") 
22          parser.add_option("-R", dest="ros_returnpath", action="store_true", default=False, help="Use ROS instead of UDP for return path") 
23          parser.add_option("-1", dest="oneway", action="store_true", default=False, help="Measure one-way travel time instead of roundtrip") 
24          (options, args) = parser.parse_args(sys.argv[1:]) 
25           
26          if len(args) != 4: 
27              parser.print_help() 
28              sys.exit(1) 
29   
30          host = args[0] 
31          port = int(args[1]) 
32          rate = float(args[2]) 
33          size = int(args[3]) 
34       
35          src_addr = options.src_addr 
36          source_id = options.source_id 
37          ros_returnpath = options.ros_returnpath 
38          oneway = options.oneway  
39          max_return_time = options.max_return_time 
40           
41          if ros_returnpath: 
42              if source_id is None: 
43                  source_id = 1 
44              rospy.init_node("udpmonsource", anonymous=True) 
45   
46          source = MonitorSource([.005, .01, .025, .05, .075, .1], (host, int(port)), rate, size, ros_returnpath = ros_returnpath, roundtrip=not oneway, source_id = source_id, sourceaddr = (src_addr, 0), max_return_time = max_return_time) 
47   
48          try: 
49              display_interval = 0.5 
50              start_time = time.time() 
51              next_time = start_time 
52              while True:  
53           
54                  next_time = next_time + display_interval 
55                  sleeptime = next_time - time.time() 
56                  if sleeptime > 0: 
57                      time.sleep(sleeptime) 
58                  if 0: 
59                      bins = source.get_bins() 
60                  else: 
61                      bins, average, average_restricted = source.get_smart_bins(display_interval) 
62                  print "%7.3f:"%(time.time() - start_time), 
63                  for i in range(0,len(bins)): 
64                      print "%3i"%(int(100*bins[i])), 
65                      if i == 2: 
66                          print "  /", 
67                  print "avg: %5.1f ms"%(1000*average), "avgr: %5.1f ms"%(1000*average_restricted), "loss: %6.2f %%"%(100 - 100 * sum(bins[0:-1])) 
68                  sys.stdout.flush() 
69                  if ros_returnpath and rospy.is_shutdown(): 
70                      break 
71          finally: 
72              if oneway: 
73                  print >> sys.stderr, "Oneway latency summary (packets):" 
74              else: 
75                  print >> sys.stderr, "Round trip latency summary (packets):" 
76              for i in range(0, len(source.latencybins)): 
77                  print >> sys.stderr, "%.1f ms: %i before %i after"%(source.latencybins[i] * 1000, sum(source.bins[0:i+1]), sum(source.bins[i+1:]) + source.lost) 
78              source.shutdown() 
79   
80      except KeyboardInterrupt: 
81          print >> sys.stderr, "Exiting on CTRL+C." 
82