module Envoy::Client::Trunk

Constants

State

Attributes

config[R]

Public Class Methods

new(config, state) click to toggle source
# File lib/envoy/client/trunk.rb, line 19
def initialize (config, state)
  @config = config
  @state = state
end
start(config, state = State.new(false, 0)) click to toggle source
# File lib/envoy/client/trunk.rb, line 15
def self.start (config, state = State.new(false, 0))
  EM.connect(*config.server, Envoy::Client::Trunk, config, state)
end

Public Instance Methods

channels() click to toggle source
# File lib/envoy/client/trunk.rb, line 24
def channels
  @channels ||= {}
end
log(*args) click to toggle source
# File lib/envoy/client/trunk.rb, line 84
def log (*args)
  Envoy.log(*args)
end
post_init() click to toggle source
# File lib/envoy/client/trunk.rb, line 112
def post_init
  self.comm_inactivity_timeout = 60
  log TRACE, "Requesting TLS negotiation."
  #send_object :start_tls
  send_object :pong
end
receive_close(id) click to toggle source
# File lib/envoy/client/trunk.rb, line 33
def receive_close id
  return unless channels[id]
  log TRACE, "closed stream #{id}"
  channels[id].close_connection true
  channels.delete(id)
end
receive_confirm(options) click to toggle source
# File lib/envoy/client/trunk.rb, line 80
def receive_confirm (options)
  log DEBUG, "Server confirmed our request. Proxy set up."
end
receive_connection(id) click to toggle source
# File lib/envoy/client/trunk.rb, line 46
def receive_connection id
  log TRACE, "New connection request with id `#{id}'"
  channels[id] = case @config.export[0]
    when :tcp
      EM.connect(*@config.export[1, 2], Channel, id, self)
    when :unix
      EM.connect_unix_domain(*@config.export[1], Channel, id, self)
    else
      raise @config.export[0].inspect
    end
rescue
  send_object :close, id
end
receive_halt() click to toggle source
# File lib/envoy/client/trunk.rb, line 75
def receive_halt
  @halting = true
  EventMachine.stop_event_loop
end
receive_keepalive() click to toggle source
# File lib/envoy/client/trunk.rb, line 60
def receive_keepalive
end
receive_message(text, level = INFO) click to toggle source
# File lib/envoy/client/trunk.rb, line 63
def receive_message text, level = INFO
  log level, text
end
receive_ping() click to toggle source
# File lib/envoy/client/trunk.rb, line 67
def receive_ping
  unless @state.connected
    ssl_handshake_completed
  end
  log TRACE, "Server pinged. Ponging back."
  send_object :pong
end
receive_start_tls() click to toggle source
# File lib/envoy/client/trunk.rb, line 28
def receive_start_tls
  log DEBUG, "Securing channel."
  start_tls
end
receive_stream(id, data) click to toggle source
# File lib/envoy/client/trunk.rb, line 40
def receive_stream id, data
  return unless channels[id]
  log TRACE, "#{data.length} bytes of data received on stream #{id}"
  channels[id].send_data data
end
ssl_handshake_completed() click to toggle source
# File lib/envoy/client/trunk.rb, line 105
def ssl_handshake_completed
  log DEBUG, "Channel is secure, sending options"
  @state.connected = true
  send_object :options, @config.options
  log DEBUG, "Exporting #{@config.export.join(":")}"
end
unbind() click to toggle source
# File lib/envoy/client/trunk.rb, line 88
def unbind
  if @halting
    log DEBUG, "Shutting down because server told us to."
  elsif $exiting
    log DEBUG, "Shutting down because the local system told us to."
  elsif @state.connected
    log WARN, "Lost connection. Retrying..." if @state.reconnects == 0
    EM.add_timer 0.5 do
      @state.reconnects += 1
      Trunk.start(@config, @state)
    end
  else
    log FATAL, "Couldn't connect. Abandoning ship."
    EventMachine.stop_event_loop
  end
end