Module: Envoy::Client::Trunk

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

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Protocol

#receive_object, #send_object, #serializer

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

Class Method Details

.start(config, state = State.new(false, 0)) ⇒ Object



15
16
17
# 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

Instance Method Details

#channelsObject



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

def channels
  @channels ||= {}
end

#initialize(config, state) ⇒ Object



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

def initialize (config, state)
  @config = config
  @state = state
end

#log(*args) ⇒ Object



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

def log (*args)
  Envoy.log(*args)
end

#post_initObject



112
113
114
115
116
117
# 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) ⇒ Object



33
34
35
36
37
38
# 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) ⇒ Object



80
81
82
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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_haltObject



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

def receive_halt
  @halting = true
  EventMachine.stop_event_loop
end

#receive_keepaliveObject



60
61
# File 'lib/envoy/client/trunk.rb', line 60

def receive_keepalive
end

#receive_message(text, level = INFO) ⇒ Object



63
64
65
# File 'lib/envoy/client/trunk.rb', line 63

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

#receive_pingObject



67
68
69
70
71
72
73
# 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_tlsObject



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

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

#receive_stream(id, data) ⇒ Object



40
41
42
43
44
# 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_completedObject



105
106
107
108
109
110
# 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

#unbindObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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