Class: Slowproxy::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/slowproxy/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv) ⇒ Object



4
5
6
# File 'lib/slowproxy/cli.rb', line 4

def self.start(argv)
  new.run(argv)
end

Instance Method Details

#parse_bps(str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/slowproxy/cli.rb', line 38

def parse_bps(str)
  return unless str
  case str.strip.downcase
  when /\A(\d+)(g|m|k|)(bps)?\z/
    num, order, * = $~.captures
    num.to_i * case order
    when ""
      1
    when "k"
      1024
    when "m"
      1024 ** 2
    when "g"
      1024 ** 3
    end
  end
end

#run(argv) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slowproxy/cli.rb', line 8

def run(argv)
  options = {
    port: 8989,
    bps: 128 * 1024,
    debug: false,
  }

  OptionParser.new do |o|
    o.banner = "Usage: #{$0} [options] [speed(g|m|k)[bps]]"
    o.on("-p PORT", "--port=PORT", Integer){|i| options[:port] = i }
    o.on("--debug", TrueClass){|b| options[:debug] = b }
    o.parse!(argv)
    options[:bps] = parse_bps(argv.first) unless argv.empty?
  end

  logger = WEBrick::Log::new(STDOUT, options[:debug] ? WEBrick::Log::DEBUG : WEBrick::Log::INFO)

  server = Server.new(
    Logger: logger,
    Port: options[:port],
    BPS: options[:bps],
  )

  Signal.trap('INT') do
    server.shutdown
  end

  server.start
end