PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Etc/scripts/measure-handoff.rb

https://github.com/revenant/ipv6suite
Ruby | 181 lines | 106 code | 32 blank | 43 comment | 12 complexity | eff3928dc7221ef61963d8e4104d0c4b MD5 | raw file
  1. #! /usr/bin/env ruby
  2. #
  3. # Author: Johnny Lai
  4. # Copyright (c) 2004 Johnny Lai
  5. #
  6. # =DESCRIPTION
  7. # Sends UDP packets with the timestamp as a payload at user specified intervals
  8. #
  9. # =REVISION HISTORY
  10. # INI 2004-09-17 Changes
  11. #
  12. require 'optparse'
  13. require 'pp'
  14. require "socket"
  15. require "timeout"
  16. #
  17. # Send or receive UDP packets
  18. #
  19. class UDPTimestamp
  20. VERSION = "$Revision: 1.2 $"
  21. REVISION_DATE = "$Date: 2005/04/27 08:19:33 $"
  22. AUTHOR = "Johnny Lai"
  23. #
  24. # Returns a version string similar to:
  25. # <app_name>: Version: 1.2 Created on: 2002/05/08 by Jim Freeze
  26. # The version number is maintained by CVS.
  27. # The date is the last checkin date of this file.
  28. #
  29. def version
  30. "Version: #{VERSION.split[1]} Created on: " +
  31. "#{REVISION_DATE.split[1]} by #{AUTHOR}"
  32. end
  33. #
  34. # Intialise and parse command line options
  35. #
  36. def initialize
  37. @debug = false
  38. @verbose = false
  39. @quit = false
  40. @interval = 0.01
  41. @payload = nil
  42. @port = 8080
  43. @use_payload = false
  44. @receive = false
  45. get_options
  46. rescue => err
  47. STDERR.puts err
  48. STDERR.puts "\n"
  49. STDERR.puts usage
  50. exit 1
  51. end
  52. #
  53. # Returns usage string
  54. #
  55. def usage
  56. ARGV.options
  57. end
  58. #
  59. # Processes command line arguments
  60. #
  61. def get_options
  62. ARGV.options { |opt|
  63. opt.banner = "Usage: ruby #{__FILE__} [options] ipv6addr port"
  64. opt.on("--help", "What you see right now"){ puts opt; exit 0}
  65. opt.on("--doc=DIRECTORY", String, "Output rdoc (Ruby HTML documentation) into directory"){|dir|
  66. system("rdoc -o #{dir} #{__FILE__}")
  67. }
  68. opt.on("--verbose", "-v", "print space separated timestamps to STDOUT"){|@verbose|}
  69. opt.on("--interval=FLOAT", "-i", Float, "approximate interval to send packets at will sleep for this many seconds before sending. Not very accurate when doing less than 0.01. Will divide interval by 1000 when < 0.01 (inteval of 0.001 effective to reduce send interval to less than 0.01 but suddenly jumps up to 0.00005 and 100% cpu time) Probably need proper scheduling besides sleep to get better accuracy"){|@interval|
  70. puts "using interval of #{@interval}" if not @verbose
  71. }
  72. opt.on("--data=STRING", "-d", String, "payload to send (by default sends timestamp",
  73. "like this #{Time.now().to_f})"){|@payload|
  74. @use_payload = true
  75. puts "using payload of #{@payload}" if not @verbose
  76. }
  77. opt.on("-r", "Receive mode "){|@receive|}
  78. opt.on_tail("default port is #{@port}")
  79. opt.parse!
  80. } or exit(1);
  81. if @quit
  82. pp self
  83. (print ARGV.options; exit)
  84. end
  85. raise "Missing host from the commandline." if ARGV.size < 1
  86. @host = ARGV.shift
  87. @port = ARGV.shift if not ARGV.empty?
  88. end
  89. #
  90. # Launches the application
  91. # UDPTimestamp.new.run
  92. #
  93. def run
  94. if not @receive then
  95. sendUDPStream
  96. else
  97. receiveUDPStream
  98. end
  99. end
  100. def receiveUDPStream
  101. BasicSocket.do_not_reverse_lookup = true
  102. server = UDPSocket.open(Socket::AF_INET6)
  103. server.bind(@host, @port)
  104. while true
  105. d = server.recvfrom(64)
  106. p d[0]
  107. end
  108. end
  109. def sendUDPStream
  110. u = UDPSocket.open(Socket::AF_INET6)
  111. sleeps = @interval
  112. sleeps = @interval/1000 if @interval < 0.01
  113. seq = 0
  114. while true
  115. t = Time.now
  116. ##{t.sec}.#{t.usec}
  117. @payload = "#{t.to_f}" if not @use_payload
  118. @payload = "#{@payload} #{seq}"
  119. seq=seq+1
  120. u.send(@payload, 0, @host, @port)
  121. print @payload, " " if @verbose
  122. #For more than 10ms accurracy need to sleep less
  123. sleep(sleeps)
  124. end
  125. rescue NameError => err
  126. STDERR.puts err
  127. STDERR.puts "\n"
  128. STDERR.puts "Most likely IPv6 support was not enabled for ruby during compilation"
  129. exit 1
  130. end#run
  131. # 2001:388:608c:fc:20e:cff:fe08:5e74 #supersupreme
  132. # 2001:388:608c:fc:20e:cff:fe08:3d6e #margarita
  133. end#UDPTimestamp
  134. #main
  135. app = UDPTimestamp.new.run
  136. ##Unit test for this class/module
  137. require 'test/unit'
  138. class TC_UDPTimestamp < Test::Unit::TestCase
  139. def test_quoteString
  140. #assert_equal("\"quotedString\"",
  141. # quoteString("quotedString"),
  142. # "The quotes should surround the quotedString")
  143. end
  144. end
  145. if $0 != __FILE__ then
  146. ##Fix Ruby debugger to allow debugging of test code
  147. require 'test/unit/ui/console/testrunner'
  148. Test::Unit::UI::Console::TestRunner.run(TC_UDPTimestamp)
  149. end