Class: XRBP::WebSocket::MultiConnection

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/xrbp/websocket/multi/multi_connection.rb

Overview

Base class facilitating transparent multiple connection dispatching. This provides mechanism which to instantiate multiple WebSocket::Connection instances proxying requests to them depending on the next_connection selected.

This class provides all the common logic to manage multiple connections. Subclasses should override and implement next_connection specifying the strategy used to select the connection to use for any given request.

Direct Known Subclasses

Fallback, Parallel, Prioritized, RoundRobin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*urls) {|_self| ... } ⇒ MultiConnection

MultiConnection initializer taking list of urls which to connect to

Parameters:

  • urls (Array<String>)

    list of urls which to establish connections to

Yields:

  • (_self)

Yield Parameters:


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 29

def initialize(*urls)
  @connections = []

  urls.each { |url|
    @connections << Connection.new(url)
  }

  connections.each { |c| c.parent = self }

  yield self if block_given?
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections


22
23
24
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 22

def connections
  @connections
end

Instance Method Details

#_add_pluginObject


66
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 66

alias :_add_plugin :add_plugin

#add_plugin(*plg) ⇒ Object


68
69
70
71
72
73
74
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 68

def add_plugin(*plg)
  connections.each { |c|
    c.add_plugin *plg
  }

  _add_plugin(*plg)
end

#close!Object

Close all connections


47
48
49
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 47

def close!
  connections.each { |c| c.close! }
end

#connectObject


83
84
85
86
87
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 83

def connect
  @connections.each { |c|
    c.connect
  }
end

#force_quit!Object

Force terminate all connections


42
43
44
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 42

def force_quit!
  connections.each { |c| c.force_quit! }
end

#next_connection(prev = nil) ⇒ Object

Always return first connection by default, override in subclasses


78
79
80
81
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 78

def next_connection(prev=nil)
  return nil unless prev.nil?
  @connections.first
end

#plugin_namespaceObject


18
19
20
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 18

def plugin_namespace
  WebSocket
end

#wait_for_closeObject

Block until all connections are closed


57
58
59
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 57

def wait_for_close
  connections.each { |c| c.wait_for_close }
end

#wait_for_completedObject

Block until all connections are completed


62
63
64
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 62

def wait_for_completed
  connections.each { |c| c.wait_for_completed }
end

#wait_for_openObject

Block until all connections are openend


52
53
54
# File 'lib/xrbp/websocket/multi/multi_connection.rb', line 52

def wait_for_open
  connections.each { |c| c.wait_for_open }
end