Class: IO::Endpoint::HostEndpoint

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

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

Returns a new instance of HostEndpoint.



10
11
12
13
14
# File 'lib/io/endpoint/host_endpoint.rb', line 10

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

Instance Attribute Details

#specificationObject (readonly)

Returns the value of attribute specification.



27
28
29
# File 'lib/io/endpoint/host_endpoint.rb', line 27

def specification
  @specification
end

Instance Method Details

#bind(wrapper = self.wrapper) {|Socket| ... } ⇒ Array<Socket>

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

Yields:

  • (Socket)

    the bound socket

Returns:

  • (Array<Socket>)

    an array of bound sockets



66
67
68
69
70
# File 'lib/io/endpoint/host_endpoint.rb', line 66

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

#connect(wrapper = self.wrapper) {|Socket| ... } ⇒ Socket

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

Yields:

  • (Socket)

    the socket which is being connected, may be invoked more than once

Returns:

  • (Socket)

    the connected socket

Raises:

  • if no connection could complete successfully



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/io/endpoint/host_endpoint.rb', line 41

def connect(wrapper = self.wrapper, &block)
	last_error = nil
	
	Addrinfo.foreach(*@specification) do |address|
		begin
			socket = wrapper.connect(address, **@options)
		rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EAGAIN => 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

#each {|AddressEndpoint| ... } ⇒ Object

Yields:

  • (AddressEndpoint)

    address endpoints by resolving the given host specification



73
74
75
76
77
78
79
# File 'lib/io/endpoint/host_endpoint.rb', line 73

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

#hostnameObject



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

def hostname
	@specification[0]
end

#inspectObject



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

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



33
34
35
# File 'lib/io/endpoint/host_endpoint.rb', line 33

def service
	@specification[1]
end

#to_sObject



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

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