Class: VNCTunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_xen/vnc_tunnel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_url) ⇒ VNCTunnel

Returns a new instance of VNCTunnel.



8
9
10
11
12
13
14
15
16
# File 'lib/foreman_xen/vnc_tunnel.rb', line 8

def initialize(full_url)
  @uri = URI(full_url)
  logger.info(full_url)
  self.host = 'localhost'
  s         = TCPServer.new('127.0.0.1', 0)
  self.port = s.addr[1]
  s.close
  @read_from_server = true
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



2
3
4
# File 'lib/foreman_xen/vnc_tunnel.rb', line 2

def host
  @host
end

#portObject

Returns the value of attribute port.



2
3
4
# File 'lib/foreman_xen/vnc_tunnel.rb', line 2

def port
  @port
end

Instance Method Details

#loggerObject



37
38
39
# File 'lib/foreman_xen/vnc_tunnel.rb', line 37

def logger
  Rails.logger
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/foreman_xen/vnc_tunnel.rb', line 18

def start
  client_srv = TCPServer.new('127.0.0.1', port)
  thr        = Thread.new do
    req         = "CONNECT #{@uri.path}?#{@uri.query} HTTP/1.1\r\n\r\n"
    @srv_socket = TCPSocket.open(@uri.host, 80)
    @srv_socket.print req
    header = @srv_socket.readline
    if header == "HTTP/1.1 200 OK\r\n"
      @srv_socket.each_line do |line|
        break if line == "\r\n"
      end
      listen client_srv
    else
      logger.error "Cannot connect to the conosle located at #{uri} reason: #{header}"
      raise "Cannot connect to the console located at #{uri} reason: #{header}"
    end
  end
end