Class: IO::Endpoint::CompositeEndpoint

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

Overview

A composite endpoint is a collection of endpoints that are used in order.

Instance Attribute Summary collapse

Attributes inherited from Generic

#options

Instance Method Summary collapse

Methods inherited from Generic

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

Constructor Details

#initialize(endpoints, **options) ⇒ CompositeEndpoint

Initialize a new composite endpoint.



14
15
16
17
18
19
20
21
22
23
# File 'lib/io/endpoint/composite_endpoint.rb', line 14

def initialize(endpoints, **options)
	super(**options)
	
	# If any options were provided, propagate them to the endpoints:
	if options.any?
		endpoints = endpoints.map{|endpoint| endpoint.with(**options)}
	end
	
	@endpoints = endpoints
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



45
46
47
# File 'lib/io/endpoint/composite_endpoint.rb', line 45

def endpoints
  @endpoints
end

#The endpoints in this composite endpoint.(endpoints) ⇒ Object (readonly)



45
# File 'lib/io/endpoint/composite_endpoint.rb', line 45

attr :endpoints

Instance Method Details

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

Bind all endpoints in the composite.



85
86
87
88
89
90
91
92
93
# File 'lib/io/endpoint/composite_endpoint.rb', line 85

def bind(wrapper = self.wrapper, &block)
	if block_given?
		@endpoints.each do |endpoint|
			endpoint.bind(&block)
		end
	else
		@endpoints.map(&:bind).flatten.compact
	end
end

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

Connect to the first endpoint that succeeds.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/io/endpoint/composite_endpoint.rb', line 67

def connect(wrapper = self.wrapper, &block)
	last_error = nil
	
	@endpoints.each do |endpoint|
		begin
			return endpoint.connect(wrapper, &block)
		rescue => last_error
		end
	end
	
	raise last_error
end

#each(&block) ⇒ Object

Enumerate all endpoints in the composite endpoint.



55
56
57
58
59
# File 'lib/io/endpoint/composite_endpoint.rb', line 55

def each(&block)
	@endpoints.each do |endpoint|
		endpoint.each(&block)
	end
end

#inspectObject

Get a detailed string representation of the composite endpoint.



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

def inspect
	"\#<#{self.class} endpoints=#{@endpoints}>"
end

#sizeObject

The number of endpoints in the composite endpoint.



48
49
50
# File 'lib/io/endpoint/composite_endpoint.rb', line 48

def size
	@endpoints.size
end

#to_sObject

Get a string representation of the composite endpoint.



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

def to_s
	"composite:#{@endpoints.join(",")}"
end

#with(**options) ⇒ Object

Create a new composite endpoint with merged options.



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

def with(**options)
	self.class.new(endpoints.map{|endpoint| endpoint.with(**options)}, **@options.merge(options))
end