Class: 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, port) ⇒ TcpConnectionData

Returns a new instance of TcpConnectionData.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 6

def initialize(hostname, port)
  @port = port
  if hostname == 'localhost'
    @ip_address = '127.0.0.1'
  else
    begin
      @ip_address = IPSocket.getaddress(hostname)
    rescue SocketError
      raise 'Unable to resolve hostname to an IP address.'
    end
  end
end

Instance Attribute Details

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



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

def ip_address
  @ip_address
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 19

def ==(other)
  other.is_a?(TcpConnectionData) && @ip_address == other.ip_address && @port == other.port
end

#get_address_bytesObject



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

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

#get_port_bytesObject



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

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

#serialize_connection_dataObject



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

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

#to_sObject



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

def to_s
  "#{@ip_address}:#{@port}"
end