Class: Rbkb::Cli::Blit

Inherits:
Executable show all
Defined in:
lib/rbkb/cli/blit.rb

Overview

Copyright 2009 emonti at matasano.com See README.rdoc for license information

blit is for use with any of the “plug” tools such as telson, feed, blitplug. It is used to send data over a socket via their OOB blit listener.

Instance Attribute Summary collapse

Attributes inherited from Executable

#argv, #exit_status, #oparse, #opts, #stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Executable

#bail, #bail_args, #exit, run

Constructor Details

#initialize(*args) ⇒ Blit

Returns a new instance of Blit.



12
13
14
15
16
17
18
19
20
# File 'lib/rbkb/cli/blit.rb', line 12

def initialize(*args)
  super(*args)
  {
    :b_addr => Plug::Blit::DEFAULT_IPADDR,
    :b_port => Plug::Blit::DEFAULT_PORT,
    :bp_proto => :TCP,
    :b_peeridx => 0,
  }.each {|k, v| @opts[k] ||= v}
end

Instance Attribute Details

#blit_msgObject

Returns the value of attribute blit_msg.



10
11
12
# File 'lib/rbkb/cli/blit.rb', line 10

def blit_msg
  @blit_msg
end

Instance Method Details

#go(*args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rbkb/cli/blit.rb', line 71

def go(*args)
  super(*args)

  begin
    Plug::Blit.blit_init(
      :addr => @opts[:b_addr],
      :port => @opts[:b_port],
      :protocol => @opts[:b_proto]
    )

    Plug::Blit.blit_raw(@blit_msg)
  rescue
    bail($!)
  end

  self.exit(0)
end

#make_parserObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbkb/cli/blit.rb', line 22

def make_parser()
  super()
  add_std_file_opt(:indat)
  arg = @oparse

  arg.banner += " <data | blank for stdin>"

  arg.on("-t", "--trans-protocol=PROTO", 
               "Blit transport protocol TCP/UDP") do |t|
    @opts[:b_proto] = t.upcase.to_sym
  end

  arg.on("-b", "--blitsrv=ADDR:PORT", 
         "Where to send blit messages") do |b|

    unless(m=/^(?:([\w\.]+):)?(\d+)$/.match(b))
      bail "invalid blit address/port"
    end
    @opts[:b_port] = m[2].to_i
    @opts[:b_port] = m[1] if m[1]
  end

  arg.on("-i", "--peer-index=IDX", Numeric, 
         "Index for remote peer to receive") do |i|
    @opts[:b_peeridx] = i
  end

  arg.on("-l", "--list-peers", "Lists the peer array for the target") do
    @blit_msg = Plug::Blit.make_list_peers
  end

  arg.on("-k", "--kill", "Stops the remote event loop.") do
    @blit_msg = Plug::Blit.make_kill
  end

  return arg
end

#parse(*args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/rbkb/cli/blit.rb', line 60

def parse(*args)
  super(*args)

  unless @blit_msg
    if @opts[:indat].nil?
      @opts[:indat] = (@argv.length > 0)?  @argv.join(" ") : @stdin.read()
    end
    @blit_msg = Plug::Blit.make_sendmsg(@opts[:b_peeridx], @opts[:indat]) 
  end
end