Class: ReverseTunnel::Server::Tunnel

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Tunnel

Returns a new instance of Tunnel.



85
86
87
# File 'lib/reverse-tunnel/server.rb', line 85

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



93
94
95
# File 'lib/reverse-tunnel/server.rb', line 93

def connection
  @connection
end

#local_hostObject

Returns the value of attribute local_host.



83
84
85
# File 'lib/reverse-tunnel/server.rb', line 83

def local_host
  @local_host
end

#local_portObject

Returns the value of attribute local_port.



83
84
85
# File 'lib/reverse-tunnel/server.rb', line 83

def local_port
  @local_port
end

#local_serverObject

Returns the value of attribute local_server.



112
113
114
# File 'lib/reverse-tunnel/server.rb', line 112

def local_server
  @local_server
end

#tokenObject

Returns the value of attribute token.



83
84
85
# File 'lib/reverse-tunnel/server.rb', line 83

def token
  @token
end

Instance Method Details

#closeObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/reverse-tunnel/server.rb', line 114

def close
  if local_server
    ReverseTunnel.logger.info "Close local connections on #{local_port}"
    EventMachine.stop_server local_server
    self.local_server = nil
  end

  if connection
    ReverseTunnel.logger.info "Close tunnel connection #{token}"
    self.connection.tap do |connection|
      @connection = nil
      connection.close_connection 
    end
  end
end

#connection_closed(connection) ⇒ Object



108
109
110
# File 'lib/reverse-tunnel/server.rb', line 108

def connection_closed(connection)
  self.connection = nil if self.connection == connection
end

#local_connectionsObject



158
159
160
# File 'lib/reverse-tunnel/server.rb', line 158

def local_connections
  @local_connections ||= []
end

#next_session_idObject



170
171
172
173
# File 'lib/reverse-tunnel/server.rb', line 170

def next_session_id
  @next_session_id ||= 0
  @next_session_id += 1
end

#openObject



130
131
132
133
134
135
136
137
# File 'lib/reverse-tunnel/server.rb', line 130

def open
  unless local_server
    ReverseTunnel.logger.info "Listen on #{local_host}:#{local_port} for #{token}"
    self.local_server = EventMachine.start_server local_host, local_port, LocalConnection, self
  end
rescue => e
  ReverseTunnel.logger.error "Can't listen on #{local_host}:#{local_port} for #{token} : #{e}"
end

#open_session(session_id) ⇒ Object



139
140
141
142
143
144
# File 'lib/reverse-tunnel/server.rb', line 139

def open_session(session_id)
  if connection
    ReverseTunnel.logger.debug { "Send open session #{session_id}" }
    connection.send_data Message::OpenSession.new(session_id).pack
  end
end

#ping_received(ping) ⇒ Object



146
147
148
149
# File 'lib/reverse-tunnel/server.rb', line 146

def ping_received(ping)
  ReverseTunnel.logger.debug { "Receive ping #{token}/#{ping.sequence_number}" }
  connection.send_data Message::Ping.new(ping.sequence_number).pack
end

#receive_data(session_id, data) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/reverse-tunnel/server.rb', line 162

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

#send_data(session_id, data) ⇒ Object



151
152
153
154
155
156
# File 'lib/reverse-tunnel/server.rb', line 151

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

#to_json(*args) ⇒ Object



175
176
177
178
179
# File 'lib/reverse-tunnel/server.rb', line 175

def to_json(*args)
  { :token => token, :local_port => local_port }.tap do |attributes|
    attributes[:connection] = connection.as_json if connection
  end.to_json(*args)
end