Class: TcpConnectionData
- Inherits:
-
Object
- Object
- TcpConnectionData
- Defined in:
- lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb
Instance Attribute Summary collapse
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#ip_address ⇒ Object
readonly
Returns the value of attribute ip_address.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #add_header(key, value) ⇒ Object
- #add_headers(headers) ⇒ Object
- #get_address_bytes ⇒ Object
- #get_port_bytes ⇒ Object
- #headers ⇒ Object
-
#initialize(hostname, port) ⇒ TcpConnectionData
constructor
A new instance of TcpConnectionData.
- #serialize_connection_data ⇒ Object
- #to_canonical_key ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(hostname, port) ⇒ TcpConnectionData
Returns a new instance of TcpConnectionData.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 6 def initialize(hostname, port) @hostname = "#{hostname}:#{port}" @port = port @headers = {} 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
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
4 5 6 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 4 def hostname @hostname end |
#ip_address ⇒ Object (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 |
#port ⇒ Object (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
#add_header(key, value) ⇒ Object
41 42 43 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 41 def add_header(key, value) @headers[key.to_s] = value.to_s end |
#add_headers(headers) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 45 def add_headers(headers) return if headers.nil? headers.each do |key, value| add_header(key, value) end end |
#get_address_bytes ⇒ Object
25 26 27 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 25 def get_address_bytes @ip_address.split('.').map(&:to_i) end |
#get_port_bytes ⇒ Object
29 30 31 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 29 def get_port_bytes [@port & 0xFF, @port >> 8] end |
#headers ⇒ Object
37 38 39 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 37 def headers @headers.dup end |
#serialize_connection_data ⇒ Object
33 34 35 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 33 def serialize_connection_data [ConnectionType::TCP] + get_address_bytes + get_port_bytes end |
#to_canonical_key ⇒ Object
53 54 55 56 57 58 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 53 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_s ⇒ Object
21 22 23 |
# File 'lib/hypertube-ruby-sdk/utils/tcp_connection_data.rb', line 21 def to_s "tcp|#{@hostname}" end |