Class: ReverseTunnel::Client::Tunnel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Tunnel

Returns a new instance of Tunnel.



48
49
50
# File 'lib/reverse-tunnel/client.rb', line 48

def initialize(attributes = {})
  attributes.each { |k,v| send "#{k}=", v }
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



52
53
54
# File 'lib/reverse-tunnel/client.rb', line 52

def connection
  @connection
end

#hostObject

Returns the value of attribute host.



45
46
47
# File 'lib/reverse-tunnel/client.rb', line 45

def host
  @host
end

#local_portObject

Returns the value of attribute local_port.



46
47
48
# File 'lib/reverse-tunnel/client.rb', line 46

def local_port
  @local_port
end

#portObject

Returns the value of attribute port.



45
46
47
# File 'lib/reverse-tunnel/client.rb', line 45

def port
  @port
end

#sequence_numberObject

Returns the value of attribute sequence_number.



80
81
82
# File 'lib/reverse-tunnel/client.rb', line 80

def sequence_number
  @sequence_number
end

#tokenObject

Returns the value of attribute token.



46
47
48
# File 'lib/reverse-tunnel/client.rb', line 46

def token
  @token
end

Instance Method Details

#local_connectionsObject



107
108
109
# File 'lib/reverse-tunnel/client.rb', line 107

def local_connections
  @local_connections ||= LocalConnections.new
end

#openObject



76
77
78
# File 'lib/reverse-tunnel/client.rb', line 76

def open
  connection.send_data Message::OpenTunnel.new(token).pack
end

#open_session(session_id) ⇒ Object



95
96
97
98
# File 'lib/reverse-tunnel/client.rb', line 95

def open_session(session_id)
  local_host = "localhost"
  EventMachine.connect local_host, local_port, LocalConnection, self, session_id
end

#pingObject



85
86
87
88
89
# File 'lib/reverse-tunnel/client.rb', line 85

def ping
  next_number = self.sequence_number += 1
  ReverseTunnel.logger.debug { "Send ping #{next_number}" } 
  connection.send_data Message::Ping.new(next_number).pack if connection
end

#ping_received(ping) ⇒ Object



91
92
93
# File 'lib/reverse-tunnel/client.rb', line 91

def ping_received(ping)
  ReverseTunnel.logger.info "Receive ping #{ping.sequence_number}"
end

#receive_data(session_id, data) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/reverse-tunnel/client.rb', line 111

def receive_data(session_id, data)
  local_connection = local_connections.find(session_id)
  if local_connection
    ReverseTunnel.logger.debug { "Send data to local connection #{session_id}" }
    local_connection.send_data data
  else
    local_connections.bufferize session_id, data
  end
end

#send_data(session_id, data) ⇒ Object



100
101
102
103
104
105
# File 'lib/reverse-tunnel/client.rb', line 100

def send_data(session_id, data)
  if connection
    ReverseTunnel.logger.debug { "Send data to local connection #{session_id}" }
    connection.send_data Message::Data.new(session_id,data).pack
  end
end

#startObject



71
72
73
74
# File 'lib/reverse-tunnel/client.rb', line 71

def start
  ReverseTunnel.logger.debug { "Connect to #{host}:#{port}" }
  EventMachine.connect host, port, TunnelConnection, self
end

#to_jsonObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/reverse-tunnel/client.rb', line 121

def to_json
  { :token => token, 
    :local_port => local_port, 
    :server_host => host, 
    :server_port => port,
    :local_connections => local_connections.as_json
  }.tap do |attributes|
    attributes[:connection] = connection.as_json if connection
  end.to_json
end