Class: ReverseTunnel::Client::TunnelConnection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/reverse-tunnel/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tunnel) ⇒ TunnelConnection

Returns a new instance of TunnelConnection.



186
187
188
# File 'lib/reverse-tunnel/client.rb', line 186

def initialize(tunnel)
  @tunnel = tunnel
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



182
183
184
# File 'lib/reverse-tunnel/client.rb', line 182

def created_at
  @created_at
end

#hearbeatObject (readonly)

Returns the value of attribute hearbeat.



184
185
186
# File 'lib/reverse-tunnel/client.rb', line 184

def hearbeat
  @hearbeat
end

#tunnelObject

Returns the value of attribute tunnel.



182
183
184
# File 'lib/reverse-tunnel/client.rb', line 182

def tunnel
  @tunnel
end

Instance Method Details

#as_jsonObject



202
203
204
# File 'lib/reverse-tunnel/client.rb', line 202

def as_json
  { :created_at => created_at }
end

#message_unpackerObject



198
199
200
# File 'lib/reverse-tunnel/client.rb', line 198

def message_unpacker
  @message_unpacker ||= Message::Unpacker.new
end

#post_initObject



190
191
192
193
194
195
196
# File 'lib/reverse-tunnel/client.rb', line 190

def post_init
  ReverseTunnel.logger.debug { "New tunnel connection" }
  self.created_at = Time.now

  tunnel.connection = self
  tunnel.open
end

#receive_data(data) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/reverse-tunnel/client.rb', line 206

def receive_data(data)
  ReverseTunnel.logger.debug { "Received data '#{data.unpack('H*').join}'" }
  message_unpacker.feed data

  message_unpacker.each do |message|
    ReverseTunnel.logger.debug { "Received message in tunnel #{message.inspect}" }

    if message.data?
      tunnel.receive_data message.session_id, message.data
    elsif message.open_session?
      tunnel.open_session message.session_id
    elsif message.ping?
      tunnel.ping_received message
    end
  end
end

#unbindObject



223
224
225
226
# File 'lib/reverse-tunnel/client.rb', line 223

def unbind
  ReverseTunnel.logger.debug { "Close tunnel connection" }
  tunnel.connection = nil
end