Module: Envoy::Client::Trunk

Includes:
Protocol
Defined in:
lib/envoy/client/trunk.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#receive_object, #send_object, #serializer

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/envoy/client/trunk.rb', line 10

def options
  @options
end

Class Method Details

.start(options) ⇒ Object



12
13
14
# File 'lib/envoy/client/trunk.rb', line 12

def self.start options
  EM.connect options[:server_host], options[:server_port].to_i, Envoy::Client::Trunk, options
end

Instance Method Details

#channelsObject



20
21
22
# File 'lib/envoy/client/trunk.rb', line 20

def channels
  @channels ||= {}
end

#initialize(options) ⇒ Object



16
17
18
# File 'lib/envoy/client/trunk.rb', line 16

def initialize options
  @options = options
end

#post_initObject



80
81
82
# File 'lib/envoy/client/trunk.rb', line 80

def post_init
  send_object :start_tls
end

#receive_close(id) ⇒ Object



28
29
30
31
# File 'lib/envoy/client/trunk.rb', line 28

def receive_close id
  channels[id].close_connection true
  channels.delete(id)
end

#receive_connection(id) ⇒ Object



37
38
39
40
# File 'lib/envoy/client/trunk.rb', line 37

def receive_connection id
  channels[id] = EM.connect(options[:local_host] || '127.0.0.1',
                            options[:local_port], Channel, id, self)
end

#receive_haltObject



50
51
52
53
# File 'lib/envoy/client/trunk.rb', line 50

def receive_halt
  @halting = true
  EventMachine.stop_event_loop
end

#receive_keepaliveObject



42
43
# File 'lib/envoy/client/trunk.rb', line 42

def receive_keepalive
end

#receive_message(message) ⇒ Object



45
46
47
48
# File 'lib/envoy/client/trunk.rb', line 45

def receive_message message
  t = Time.now.strftime("%F %T")
  STDERR.puts t + " " + message.split("\n").join("\n#{t.gsub(/./, ' ')} ")
end

#receive_start_tlsObject



24
25
26
# File 'lib/envoy/client/trunk.rb', line 24

def receive_start_tls
  start_tls
end

#receive_stream(id, data) ⇒ Object



33
34
35
# File 'lib/envoy/client/trunk.rb', line 33

def receive_stream id, data
  channels[id].send_data data
end

#ssl_handshake_completedObject



74
75
76
77
78
# File 'lib/envoy/client/trunk.rb', line 74

def ssl_handshake_completed
  options[:did_connect] = true
  options[:reconnect] = %w"- \\ | /" if options[:hosts]
 send_object :options, options
end

#unbindObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/envoy/client/trunk.rb', line 55

def unbind
  if @halting
    STDERR.puts "Server shut us down."
  elsif !@halting && r = @options[:reconnect]
    STDERR.write "Lost connection. Retrying... #{r[0]}\r"
    EM.add_timer 0.5 do
      @options[:reconnect] = r.rotate
      Trunk.start @options
    end
  else
    if options[:did_connect]
      STDERR.puts "Connection lost. Not point reconnecting because the host is randomly generated."
    else
      STDERR.puts "Couldn't connect. Abandoning ship."
    end
    EventMachine.stop_event_loop
  end
end