Module: Envoy::Client::Trunk

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

Constant Summary

Constants included from Protocol

Protocol::VERBOSITIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#receive_object, #send_object, #serializer, #verbosity

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.start(options) ⇒ Object



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

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

Instance Method Details

#channelsObject



26
27
28
# File 'lib/envoy/client/trunk.rb', line 26

def channels
  @channels ||= {}
end

#initialize(options) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/envoy/client/trunk.rb', line 17

def initialize options
  @options = options
  @log = STDERR
  if @options.has_key?(:log)
    @log = @options[:log] && File.open(@options[:log], "a")
  end
  log DEBUG, "envoy #{Envoy::VERSION} starting up"
end

#log(level, text, io = @log) ⇒ Object



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

def log (level, text, io = @log)
  return unless io
  return unless level <= verbosity
  message = [
    @options[:timestamps] ? Time.now.strftime("%F %T") : nil,
    @options[:show_log_level] ? "#{VERBOSITIES[level][0]}:" : nil,
    text
  ].compact.join(" ")
  if @options[:color_log_level]
    #FATAL ERROR WARN\  INFO\  DEBUG TRACE
    message = message.colorize(%i"red red yellow green default light_black"[level])
  end 
  io.puts message
  io.flush
end

#post_initObject



118
119
120
121
122
# File 'lib/envoy/client/trunk.rb', line 118

def post_init
  self.comm_inactivity_timeout = 25
  log DEBUG, "Requesting TLS negotiation."
  send_object :start_tls
end

#receive_close(id) ⇒ Object



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

def receive_close id
  log TRACE, "closed stream #{id}"
  channels[id].close_connection true
  channels.delete(id)
end

#receive_confirm(options) ⇒ Object



85
86
87
# File 'lib/envoy/client/trunk.rb', line 85

def receive_confirm (options)
  log DEBUG, "Server confirmed our request. Proxy set up."
end

#receive_connection(id) ⇒ Object



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

def receive_connection id
  log TRACE, "New connection request with id `#{id}'"
  channels[id] = EM.connect(options[:local_host] || '127.0.0.1',
                            options[:local_port], Channel, id, self)
end

#receive_haltObject



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

def receive_halt
  @halting = true
  EventMachine.stop_event_loop
end

#receive_keepaliveObject



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

def receive_keepalive
end

#receive_message(text, level = INFO) ⇒ Object



71
72
73
# File 'lib/envoy/client/trunk.rb', line 71

def receive_message text, level = INFO
  log level, text
end

#receive_pingObject



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

def receive_ping
  log TRACE, "Server pinged. Ponging back."
  send_object :pong
end

#receive_start_tlsObject



30
31
32
33
# File 'lib/envoy/client/trunk.rb', line 30

def receive_start_tls
  log DEBUG, "Securing channel."
  start_tls
end

#receive_stream(id, data) ⇒ Object



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

def receive_stream id, data
  log TRACE, "#{data.length} bytes of data received on stream #{id}"
  channels[id].send_data data
end

#ssl_handshake_completedObject



110
111
112
113
114
115
116
# File 'lib/envoy/client/trunk.rb', line 110

def ssl_handshake_completed
  log DEBUG, "Channel is secure, sending options"
  options[:did_connect] = true
  options[:reconnect] = 0 if options[:hosts]
  send_object :options, options
  log DEBUG, "Exporting #{@options[:local_host]}:#{@options[:local_port]}"
end

#unbindObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/envoy/client/trunk.rb', line 89

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 !@halting && r = @options[:reconnect]
    log WARN, "Lost connection. Retrying..." if r == 0
    EM.add_timer 0.5 do
      @options[:reconnect] += 1
      Trunk.start @options
    end
  else
    if options[:did_connect]
      log FATAL, "Connection lost. Not point reconnecting because the host is randomly generated."
    else
      log FATAL, "Couldn't connect. Abandoning ship."
    end
    EventMachine.stop_event_loop
  end
end