IO::EndpointSourceIOEndpointCompositeEndpoint

class CompositeEndpoint

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

Definitions

def initialize(endpoints, **options)

Initialize a new composite endpoint.

Signature

parameter endpoints Array(Generic)

The endpoints to compose.

parameter options Hash

Additional options to pass to the parent class and propagate to endpoints.

Implementation

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

def to_s

Get a string representation of the composite endpoint.

Signature

returns String

A string representation listing all endpoints.

Implementation

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

def inspect

Get a detailed string representation of the composite endpoint.

Signature

returns String

A detailed string representation including all endpoints.

Implementation

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

def with(**options)

Create a new composite endpoint with merged options.

Signature

parameter options Hash

Additional options to merge with existing options.

returns CompositeEndpoint

A new composite endpoint instance with merged options.

Implementation

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

attr :endpoints

Signature

attribute Array(Generic)

The endpoints in this composite endpoint.

def size

The number of endpoints in the composite endpoint.

Implementation

def size
	@endpoints.size
end

def each(&block)

Enumerate all endpoints in the composite endpoint.

Signature

yields {|endpoint| ...}

For each endpoint in the composite, yields it.

parameter endpoint Generic

An endpoint in the composite.

Implementation

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

def connect(wrapper = self.wrapper, &block)

Connect to the first endpoint that succeeds.

Signature

parameter wrapper Wrapper

The wrapper to use for connecting.

yields {|socket| ...}

If a block is given, yields the connected socket from the first successful endpoint.

parameter socket Socket

The connected socket.

returns Socket

The connected socket.

raises Exception

If all endpoints fail to connect, raises the last error encountered.

Implementation

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

def bind(wrapper = self.wrapper, &block)

Bind all endpoints in the composite.

Signature

parameter wrapper Wrapper

The wrapper to use for binding.

yields {|socket| ...}

For each endpoint that is bound, yields the bound socket.

parameter socket Socket

A bound socket.

returns Array(Socket)

An array of bound sockets if no block is given.

Implementation

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