Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
modify inner api and add yard-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Aug 8, 2012
1 parent f2b60bf commit 3beb20a
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -26,7 +26,7 @@ Wrapper for "ping -r" on Windows (Record Route)
== Installation == Installation


1. gem install striuct 1. gem install striuct
2. put the "bin/wrapingr.rb" anywhere 2. put this project-dir anywhere


== License == License


Expand Down
83 changes: 2 additions & 81 deletions lib/mswindows.rb
@@ -1,81 +1,2 @@
# Copyright (C) 2012 Kenichi Kamiya require_relative 'mswindows/commandprompt'

require_relative 'mswindows/ping'
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
75 changes: 75 additions & 0 deletions 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
66 changes: 66 additions & 0 deletions 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
74 changes: 39 additions & 35 deletions lib/wrapingr.rb
Expand Up @@ -14,55 +14,59 @@ module Wrapingr


VERSION = '0.2.1'.freeze VERSION = '0.2.1'.freeze


# @param [String] dest_addr
# @param [Numeric] interval
def run(dest_addr, interval) def run(dest_addr, interval)
$stderr.puts 'Stop with [ctrl + C]' $stderr.puts 'Stop with [ctrl + C]'


path_prefix = "#{$PROGRAM_NAME}.#{Time.now.strftime '%Y-%m-%d_%H-%M-%S'}_#{dest_addr}" path_prefix = "#{$PROGRAM_NAME}.#{Time.now.strftime '%Y-%m-%d_%H-%M-%S'}_#{dest_addr}"
width = 120
CommandPrompt.change_width! width CommandPrompt.open do |cmd|
CommandPrompt.change_charset! :ascii cmd.width = 120


command = "ping -r 9 -w 100 -n 1 -l 1400 -4 #{dest_addr}" command = "ping -r 9 -w 100 -n 1 -l 1400 -4 #{dest_addr}"
separator = '-' * (width - 2) separator = '-' * (cmd.width - 2)
last, last_passed, route_changed = nil, nil, false last, last_passed, route_changed = nil, nil, false


File.open "#{path_prefix}.log", 'w' do |log| File.open "#{path_prefix}.log", 'w' do |log|
announce = "# Command: #{command}\n# Interval: #{interval} sec", separator announce = "# Command: #{command}\n# Interval: #{interval} sec", separator
log.puts announce log.puts announce
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 loop do
time, cmd_output = Time.now, `#{command}` time, cmd_output = Time.now, `#{command}`
ftime = ftime time ftime = ftime time
last = result = MSWindows::Ping::RecordRoute.parse cmd_output last = result = Ping::RecordRoute.parse cmd_output


if result.pass? if result.pass?
if last_passed if last_passed
route_changed = (result.route != last_passed.route) route_changed = (result.route != last_passed.route)
end

last_passed = result
else
route_changed = false
end end


last_passed = result header = "# #{ftime} #{result.pass? ? (route_changed ? 'Change!' : 'OK') : 'NG'}"
else log.puts header, cmd_output, separator
route_changed = false
end route_summaries = result.route.each_with_index.map{|addr, i|"#{i + 1}: #{addr.to_s.ljust(15)}"}

puts(
header = "# #{ftime} #{result.pass? ? (route_changed ? 'Change!' : 'OK') : 'NG'}" header,
log.puts header, cmd_output, separator route_summaries[0..3].join(' -> '),

route_summaries[4..9].join(' -> ')
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] csv << [ftime, result.pass?, route_changed, *result.route, result.summary, result.rest]


sleep interval sleep interval
end
end end
end end

end end


end end
Expand Down

0 comments on commit 3beb20a

Please sign in to comment.