class HostEndpoint
Represents an endpoint for a hostname and service that resolves to multiple addresses.
Definitions
def initialize(specification, **options)
Initialize a new host endpoint.
Signature
-
parameter
specificationArray The host specification array containing nodename, service, family, socktype, protocol, and flags.
-
parameter
optionsHash Additional options to pass to the parent class.
Implementation
def initialize(specification, **options)
super(**options)
@specification = specification
end
def to_s
Get a string representation of the host endpoint.
Signature
-
returns
String A string representation showing hostname and service.
Implementation
def to_s
"host:#{@specification[0]}:#{@specification[1]}"
end
def inspect
Get a detailed string representation of the host endpoint.
Signature
-
returns
String A detailed string representation including all specification parameters.
Implementation
def inspect
nodename, service, family, socktype, protocol, flags = @specification
"\#<#{self.class} name=#{nodename.inspect} service=#{service.inspect} family=#{family.inspect} type=#{socktype.inspect} protocol=#{protocol.inspect} flags=#{flags.inspect}>"
end
attr :specification
Signature
-
attribute
Array The host specification array.
def hostname
Get the hostname from the specification.
Signature
-
returns
String, nil The hostname (nodename) from the specification.
Implementation
def hostname
@specification[0]
end
def service
Get the service from the specification.
Signature
-
returns
String, Integer, nil The service (port) from the specification.
Implementation
def service
@specification[1]
end
def connect(wrapper = self.wrapper, &block)
Try to connect to the given host by connecting to each address in sequence until a connection is made.
Signature
-
yields
{|socket| ...} If a block is given, yields the connected socket (may be invoked multiple times during connection attempts).
-
parameter
socketSocket The socket which is being connected.
-
parameter
-
returns
Socket the connected socket
-
raises
Exception if no connection could complete successfully
Implementation
def connect(wrapper = self.wrapper, &block)
last_error = nil
Addrinfo.foreach(*@specification) do |address|
begin
socket = wrapper.connect(address, **@options)
rescue => last_error
Console.debug(self, "Failed to connect:", address, exception: last_error)
# Try again unless if possible, otherwise raise...
else
return socket unless block_given?
begin
return yield(socket)
ensure
socket.close
end
end
end
raise last_error
end
def bind(wrapper = self.wrapper, &block)
Invokes the given block for every address which can be bound to.
Signature
-
yields
{|socket| ...} For each address that can be bound, yields the bound socket.
-
parameter
socketSocket The bound socket.
-
parameter
-
returns
Array<Socket> an array of bound sockets
Implementation
def bind(wrapper = self.wrapper, &block)
Addrinfo.foreach(*@specification).map do |address|
wrapper.bind(address, **@options, &block)
end
end
def each
Signature
-
yields
{|endpoint| ...} For each resolved address, yields an address endpoint.
-
parameter
endpointAddressEndpoint An address endpoint.
-
parameter
Implementation
def each
return to_enum unless block_given?
Addrinfo.foreach(*@specification) do |address|
yield AddressEndpoint.new(address, **@options)
end
end