Class: Hypertube::Utils::TcpConnectionData

Inherits:
Object
  • Object
show all
Defined in:
lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname) ⇒ TcpConnectionData

Returns a new instance of TcpConnectionData.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 8

def initialize(hostname)
  host_only, port = parse_hostname(hostname)
  @hostname = "#{host_only}:#{port}"
  @port = port
  @headers = {}
  if host_only == 'localhost'
    @ip_address = '127.0.0.1'
  else
    begin
      @ip_address = IPSocket.getaddress(host_only)
    rescue SocketError
      raise 'Unable to resolve hostname to an IP address.'
    end
  end
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



6
7
8
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 6

def hostname
  @hostname
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



6
7
8
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 6

def ip_address
  @ip_address
end

#portObject (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 6

def port
  @port
end

Instance Method Details

#add_header(key, value) ⇒ Object



44
45
46
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 44

def add_header(key, value)
  @headers[key.to_s] = value.to_s
end

#add_headers(headers) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 48

def add_headers(headers)
  return if headers.nil?

  headers.each do |key, value|
    add_header(key, value)
  end
end

#get_address_bytesObject



28
29
30
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 28

def get_address_bytes
  @ip_address.split('.').map(&:to_i)
end

#get_port_bytesObject



32
33
34
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 32

def get_port_bytes
  [@port & 0xFF, @port >> 8]
end

#headersObject



40
41
42
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 40

def headers
  @headers.dup
end

#serialize_connection_dataObject



36
37
38
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 36

def serialize_connection_data
  [Hypertube::Utils::ConnectionType::TCP] + get_address_bytes + get_port_bytes
end

#to_canonical_keyObject



56
57
58
59
60
61
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 56

def to_canonical_key
  headers_for_key = serialize_headers_for_key
  return to_s if headers_for_key.empty?

  "#{to_s}|h:#{headers_for_key}"
end

#to_sObject



24
25
26
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 24

def to_s
  "tcp|#{@hostname}"
end