Browse files

modify inner api and add yard-doc

  • Loading branch information...
1 parent f2b60bf commit 3beb20aafc1f339f7fb78210baeb9c69bee3a333 @kachick committed Aug 8, 2012
Showing with 183 additions and 117 deletions.
  1. +1 −1 README.rdoc
  2. +2 −81 lib/mswindows.rb
  3. +75 −0 lib/mswindows/commandprompt.rb
  4. +66 −0 lib/mswindows/ping.rb
  5. +39 −35 lib/wrapingr.rb
View
2 README.rdoc
@@ -26,7 +26,7 @@ Wrapper for "ping -r" on Windows (Record Route)
== Installation
1. gem install striuct
-2. put the "bin/wrapingr.rb" anywhere
+2. put this project-dir anywhere
== License
View
83 lib/mswindows.rb
@@ -1,81 +1,2 @@
-# Copyright (C) 2012 Kenichi Kamiya
-
-require 'striuct'
-
-module MSWindows
-
- module CommandPrompt
- module_function
-
- def change_width!(size)
- `mode con cols=#{size}`
- end
-
- def change_charset!(type)
- num = {ascii: 437, shiftjis: 932}[type] || raise(ArgumentError)
-
- `chcp #{num}`
- end
-
- end
-
- module Ping
-
- class RecordRoute < Striuct
-
- class << self
-
- def parse(str)
- define {|pr|
- source = str.dup
- source.slice!(/^Pinging.+:\n/)
- pr.summary = source.slice!(/^((?:Reply|Request|Destination).+)\n/, 1)
- source.slice!(/\n+/)
- source.slice!(/^Ping statistics.*?\n/)
-
- 1.upto 9 do |i|
- pr[:"route#{i}"] = (
- if source.slice!(/^\s+(?:Route:)?\s*(\S+)\s*(?:->)?\s*$/)
- $1
- else
- nil
- end
- )
- end
-
- unless source.empty?
- pr.rest = source.gsub("\n", '\n')
- end
- }
- end
-
- end
-
- member :summary, String
- member :route1, OR(nil, String)
- member :route2, OR(nil, String)
- member :route3, OR(nil, String)
- member :route4, OR(nil, String)
- member :route5, OR(nil, String)
- member :route6, OR(nil, String)
- member :route7, OR(nil, String)
- member :route8, OR(nil, String)
- member :route9, OR(nil, String)
- member :rest, OR(nil, String)
- close_member
-
- def hops
- 1.upto(9).map{|n|self[:"route#{n}"]}
- end
-
- alias_method :route, :hops
-
- def pass?
- !!route1
- end
-
- end
-
- end
-
-end
+require_relative 'mswindows/commandprompt'
+require_relative 'mswindows/ping'
View
75 lib/mswindows/commandprompt.rb
@@ -0,0 +1,75 @@
+# Copyright (C) 2012 Kenichi Kamiya
+
+module MSWindows
+
+ class CommandPrompt
+
+ CHAR_SETS = {
+ ascii: 437,
+ shiftjis: 932
+ }.freeze
+
+ class << self
+
+ # @return [CommandPrompt]
+ def open
+ instance = new
+ first_charset = instance.charset
+
+ begin
+ instance.charset = :ascii
+ first_widh = instance.width
+ yield instance
+ ensure
+ instance.width = first_widh
+ instance.charset = first_charset
+ instance
+ end
+ end
+
+ end
+
+ # @return [Integer]
+ def width
+ raise 'Invalid charset' unless charset == :ascii
+ Integer `mode con /status`.slice(/\bColumns:\s*(\d+)/, 1)
+ end
+
+ # @param [Integer] size
+ # @return [Integer] size
+ def width=(size)
+ `mode con cols=#{size}`
+ size
+ end
+
+ # @return [Integer]
+ def codepage
+ Integer `mode con cp /status`.slice(/:\s*(\d+)/, 1)
+ end
+
+ # @param [Integer] integer
+ # @return [Integer] integer
+ def codepage=(integer)
+ `chcp #{integer}`
+ integer
+ end
+
+ # @return [Symbol]
+ def charset
+ charset_for codepage
+ end
+
+ # @param [Symbol] type
+ def charset=(type)
+ self.codepage = CHAR_SETS.fetch(type)
+ end
+
+ # @param [Integer] cp
+ # @return [Symbol]
+ def charset_for(cp)
+ CHAR_SETS.key(cp) || raise(ArgumentError)
+ end
+
+ end
+
+end
View
66 lib/mswindows/ping.rb
@@ -0,0 +1,66 @@
+# Copyright (C) 2012 Kenichi Kamiya
+
+require 'striuct'
+
+module MSWindows
+
+ module Ping
+
+ class RecordRoute < Striuct
+
+ class << self
+
+ def parse(str)
+ define {|pr|
+ source = str.dup
+ source.slice!(/^Pinging.+:\n/)
+ pr.summary = source.slice!(/^((?:Reply|Request|Destination).+)\n/, 1)
+ source.slice!(/\n+/)
+ source.slice!(/^Ping statistics.*?\n/)
+
+ 1.upto 9 do |i|
+ pr[:"route#{i}"] = (
+ if source.slice!(/^\s+(?:Route:)?\s*(\S+)\s*(?:->)?\s*$/)
+ $1
+ else
+ nil
+ end
+ )
+ end
+
+ unless source.empty?
+ pr.rest = source.gsub("\n", '\n')
+ end
+ }
+ end
+
+ end
+
+ member :summary, String
+ member :route1, OR(nil, String)
+ member :route2, OR(nil, String)
+ member :route3, OR(nil, String)
+ member :route4, OR(nil, String)
+ member :route5, OR(nil, String)
+ member :route6, OR(nil, String)
+ member :route7, OR(nil, String)
+ member :route8, OR(nil, String)
+ member :route9, OR(nil, String)
+ member :rest, OR(nil, String)
+ close_member
+
+ def hops
+ 1.upto(9).map{|n|self[:"route#{n}"]}
+ end
+
+ alias_method :route, :hops
+
+ def pass?
+ !!route1
+ end
+
+ end
+
+ end
+
+end
View
74 lib/wrapingr.rb
@@ -14,55 +14,59 @@ module Wrapingr
VERSION = '0.2.1'.freeze
+ # @param [String] dest_addr
+ # @param [Numeric] interval
def run(dest_addr, interval)
$stderr.puts 'Stop with [ctrl + C]'
path_prefix = "#{$PROGRAM_NAME}.#{Time.now.strftime '%Y-%m-%d_%H-%M-%S'}_#{dest_addr}"
- width = 120
- CommandPrompt.change_width! width
- CommandPrompt.change_charset! :ascii
+
+ CommandPrompt.open do |cmd|
+ cmd.width = 120
- command = "ping -r 9 -w 100 -n 1 -l 1400 -4 #{dest_addr}"
- separator = '-' * (width - 2)
- last, last_passed, route_changed = nil, nil, false
+ command = "ping -r 9 -w 100 -n 1 -l 1400 -4 #{dest_addr}"
+ separator = '-' * (cmd.width - 2)
+ last, last_passed, route_changed = nil, nil, false
- File.open "#{path_prefix}.log", 'w' do |log|
- announce = "# Command: #{command}\n# Interval: #{interval} sec", separator
- log.puts announce
- puts announce
+ File.open "#{path_prefix}.log", 'w' do |log|
+ announce = "# Command: #{command}\n# Interval: #{interval} sec", separator
+ log.puts announce
+ puts announce
- CSV.open "#{path_prefix}.csv", 'w', CSV_OPTIONS do |csv|
+ CSV.open "#{path_prefix}.csv", 'w', CSV_OPTIONS do |csv|
- loop do
- time, cmd_output = Time.now, `#{command}`
- ftime = ftime time
- last = result = MSWindows::Ping::RecordRoute.parse cmd_output
+ loop do
+ time, cmd_output = Time.now, `#{command}`
+ ftime = ftime time
+ last = result = Ping::RecordRoute.parse cmd_output
- if result.pass?
- if last_passed
- route_changed = (result.route != last_passed.route)
+ if result.pass?
+ if last_passed
+ route_changed = (result.route != last_passed.route)
+ end
+
+ last_passed = result
+ else
+ route_changed = false
end
- last_passed = result
- else
- route_changed = false
- end
-
- header = "# #{ftime} #{result.pass? ? (route_changed ? 'Change!' : 'OK') : 'NG'}"
- log.puts header, cmd_output, separator
-
- route_summaries = result.route.each_with_index.map{|addr, i|"#{i + 1}: #{addr.to_s.ljust(15)}"}
- puts(
- header,
- route_summaries[0..3].join(' -> '),
- route_summaries[4..9].join(' -> ')
- )
+ header = "# #{ftime} #{result.pass? ? (route_changed ? 'Change!' : 'OK') : 'NG'}"
+ log.puts header, cmd_output, separator
+
+ route_summaries = result.route.each_with_index.map{|addr, i|"#{i + 1}: #{addr.to_s.ljust(15)}"}
+ puts(
+ header,
+ route_summaries[0..3].join(' -> '),
+ route_summaries[4..9].join(' -> ')
+ )
- csv << [ftime, result.pass?, route_changed, *result.route, result.summary, result.rest]
-
- sleep interval
+ csv << [ftime, result.pass?, route_changed, *result.route, result.summary, result.rest]
+
+ sleep interval
+ end
end
end
+
end
end

0 comments on commit 3beb20a

Please sign in to comment.