Class: IO::Endpoint::HostEndpoint

Inherits:
Generic
  • Object
show all
Defined in:
lib/io/endpoint/host_endpoint.rb

Overview

Represents an endpoint for a hostname and service that resolves to multiple addresses.

Instance Attribute Summary collapse

Attributes inherited from Generic

#options

Instance Method Summary collapse

Methods inherited from Generic

#accept, #bound, #connected, #linger, #local_address, parse, #reuse_address?, #reuse_port?, #timeout, #with, #wrapper

Constructor Details

#initialize(specification, **options) ⇒ HostEndpoint

Initialize a new host endpoint.



14
15
16
17
18
# File 'lib/io/endpoint/host_endpoint.rb', line 14

def initialize(specification, **options)
	super(**options)
	
	@specification = specification
end

Instance Attribute Details

#specificationObject (readonly)

Returns the value of attribute specification.



36
37
38
# File 'lib/io/endpoint/host_endpoint.rb', line 36

def specification
  @specification
end

#The host specification array.(hostspecificationarray.) ⇒ Object (readonly)



36
# File 'lib/io/endpoint/host_endpoint.rb', line 36

attr :specification

Instance Method Details

#bind(wrapper = self.wrapper, &block) ⇒ Object

Invokes the given block for every address which can be bound to.



82
83
84
85
86
# File 'lib/io/endpoint/host_endpoint.rb', line 82

def bind(wrapper = self.wrapper, &block)
	Addrinfo.foreach(*@specification).map do |address|
		wrapper.bind(address, **@options, &block)
	end
end

#connect(wrapper = self.wrapper, &block) ⇒ Object

Try to connect to the given host by connecting to each address in sequence until a connection is made.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/io/endpoint/host_endpoint.rb', line 55

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

#eachObject



90
91
92
93
94
95
96
# File 'lib/io/endpoint/host_endpoint.rb', line 90

def each
	return to_enum unless block_given?
	
	Addrinfo.foreach(*@specification) do |address|
		yield AddressEndpoint.new(address, **@options)
	end
end

#hostnameObject

Get the hostname from the specification.



40
41
42
# File 'lib/io/endpoint/host_endpoint.rb', line 40

def hostname
	@specification[0]
end

#inspectObject

Get a detailed string representation of the host endpoint.



28
29
30
31
32
# File 'lib/io/endpoint/host_endpoint.rb', line 28

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

#serviceObject

Get the service from the specification.



46
47
48
# File 'lib/io/endpoint/host_endpoint.rb', line 46

def service
	@specification[1]
end

#to_sObject

Get a string representation of the host endpoint.



22
23
24
# File 'lib/io/endpoint/host_endpoint.rb', line 22

def to_s
	"host:#{@specification[0]}:#{@specification[1]}"
end