Class: Rbkb::Cli::Telson

Inherits:
PlugCli show all
Defined in:
lib/rbkb/cli/telson.rb

Overview

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

This is an implementation of the original blackbag "telson" around ruby and eventmachine.

Telson can do the following things with minimum fuss:

- Run as a "stubbed" network client using UDP or TCP
- Debugging network protocols
- Observe client/server behaviors using different messages at
various phases of a conversation.

Constant Summary

Constants inherited from PlugCli

PlugCli::RX_HOST_AND_PORT, PlugCli::RX_PORT_OPT_ADDR

Instance Attribute Summary

Attributes inherited from PlugCli

#blit_addr, #blit_port, #blit_proto, #local_addr, #local_port, #plug_opts, #target_addr, #target_port, #transport

Attributes inherited from Executable

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

Instance Method Summary collapse

Methods inherited from PlugCli

#parse_target_argument

Methods inherited from Executable

#bail, #bail_args, #exit, run

Constructor Details

#initialize(*args) ⇒ Telson

Returns a new instance of Telson.



16
17
18
19
20
21
22
23
# File 'lib/rbkb/cli/telson.rb', line 16

def initialize(*args)
  super(*args) do |this|
    this.local_addr = '0.0.0.0'
    this.local_port = 0
  end

  @persist = false
end

Instance Method Details

#go(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rbkb/cli/telson.rb', line 49

def go(*args)
  super(*args)
  loop do
    EventMachine.run do
      if @transport == :TCP

        c = EventMachine.bind_connect(@local_addr,
                                      @local_port,
                                      @target_addr,
                                      @target_port,
                                      Plug::Telson,
                                      @transport,
                                      @plug_opts)
      elsif @transport == :UDP
        c = EventMachine.open_datagram_socket(@local_addr,
                                              @local_port,
                                              Plug::Telson,
                                              @transport,
                                              @plug_opts)

        c.peers.add_peer_manually(@target_addr, @target_port)

      ### someday maybe raw or others?
      else
        raise 'bad transport protocol'
      end
      EventMachine.start_server(@blit_addr, @blit_port, Plug::Blit, @blit_proto, c)
      Plug::UI.verbose("** BLITSRV-#{@blit_addr}:#{@blit_port}(TCP) Started") # XXX
    end
    break unless @persist

    Plug::UI.verbose('** RECONNECTING') # XXX
  end
end

#make_parser ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rbkb/cli/telson.rb', line 25

def make_parser
  arg = super()

  arg.on('-r', '--reconnect', 'Attempt to reconnect endlessly.') do
    @persist = true
  end

  arg.on('-s', '--source=(ADDR:?)PORT', 'Bind client on port and addr') do |p|
    if /^(?:([\w.]+):)?(\d+)$/.match(p)
      @local_addr = ::Regexp.last_match(1) if ::Regexp.last_match(1)
      @local_port = ::Regexp.last_match(2).to_i
    else
      bail("Invalid source argument: #{p.inspect}")
    end
  end
end

#parse(*args) ⇒ Object



42
43
44
45
46
47
# File 'lib/rbkb/cli/telson.rb', line 42

def parse(*args)
  super(*args)

  parse_target_argument
  parse_catchall
end