Class: XRBP::WebSocket::Connection

Inherits:
Object
  • Object
show all
Includes:
EventEmitter, HasPlugin, ThreadRegistry
Defined in:
lib/xrbp/websocket/connection.rb

Overview

Primary websocket interface, use Connection to perform websocket requests.

Examples:

retrieve data via a websocket

connection = WebSocket::Connection.new "wss://s1.ripple.com:443"
puts connection.send_data('{"command" : "server_info"}')

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ThreadRegistry

#rsleep, #thread_registry, #wake_all

Methods included from HasPlugin

#add_plugin, #define_instance_method, #plugin, #plugin?, #plugins

Constructor Details

#initialize(url) {|_self| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
27
28
# File 'lib/xrbp/websocket/connection.rb', line 23

def initialize(url)
  @url        = url
  @force_quit = false

  yield self if block_given?
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



21
22
23
# File 'lib/xrbp/websocket/connection.rb', line 21

def parent
  @parent
end

#urlObject (readonly)

Returns the value of attribute url.



20
21
22
# File 'lib/xrbp/websocket/connection.rb', line 20

def url
  @url
end

Instance Method Details

#add_work(&bl) ⇒ Object

Add work to the internal client thread pool



48
49
50
# File 'lib/xrbp/websocket/connection.rb', line 48

def add_work(&bl)
  client.add_work &bl
end

#async_close!Object

Close in a non-blocking way, and immediately return.



80
81
82
# File 'lib/xrbp/websocket/connection.rb', line 80

def async_close!
  client.async_close if open?
end

#clientObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xrbp/websocket/connection.rb', line 150

def client
  @client ||= begin
    client = Client.new(@url)
    conn = self

    client.on :connecting do
      conn.emit :connecting
      conn.parent.emit :connecting, conn if conn.parent
    end

    client.on :open do
      conn.emit :open
      conn.parent.emit :open, conn if conn.parent

      conn.state_mutex.synchronize {
        conn.open_cv.signal
      }

      conn.plugins.each { |plg|
        plg.opened if plg.respond_to?(:opened)
      }
    end

    client.on :close do
      conn.emit :close
      conn.parent.emit :close, conn if conn.parent

      conn.state_mutex.synchronize {
        conn.close_cv.signal
      }

      conn.plugins.each { |plg|
        plg.closed if plg.respond_to?(:closed)
      }
    end

    client.on :completed do |err|
      conn.emit :completed
      conn.parent.emit :completed, conn if conn.parent

      conn.state_mutex.synchronize {
        conn.completed_cv.signal
      }

      conn.plugins.each { |plg|
        plg.completed if plg.respond_to?(:completed)
      }
    end

    client.on :error do |err|
      conn.emit :error, err
      conn.parent.emit :error, conn, err if conn.parent

      conn.plugins.each { |plg|
        plg.error err if plg.respond_to?(:error)
      }
    end

    client.on :message do |msg|
      conn.emit :message, msg
      conn.parent.emit :message, conn, msg if conn.parent

      conn.plugins.each { |plg|
        plg.message msg if plg.respond_to?(:message)
      }
    end

    client
  end
end

#close!Object

Close the connection, blocking until completed



75
76
77
# File 'lib/xrbp/websocket/connection.rb', line 75

def close!
  client.close if open?
end

#close_cvObject



139
140
141
# File 'lib/xrbp/websocket/connection.rb', line 139

def close_cv
  @close_cv ||= ConditionVariable.new
end

#closed?Boolean

Indicates the connection is closed (may not be completed)

Returns:

  • (Boolean)


64
65
66
# File 'lib/xrbp/websocket/connection.rb', line 64

def closed?
  !open?
end

#completed?Boolean

Indicates if connection is completely closed and cleaned up

Returns:

  • (Boolean)


70
71
72
# File 'lib/xrbp/websocket/connection.rb', line 70

def completed?
  client.completed?
end

#completed_cvObject



143
144
145
# File 'lib/xrbp/websocket/connection.rb', line 143

def completed_cv
  @completed_cv ||= ConditionVariable.new
end

#connectObject

Initiate new client connection



33
34
35
# File 'lib/xrbp/websocket/connection.rb', line 33

def connect
  client.connect
end

#force_quit!Object

Immediately terminate the connection and all related operations



96
97
98
99
100
# File 'lib/xrbp/websocket/connection.rb', line 96

def force_quit!
  @force_quit = true
  wake_all
  # TODO immediate terminate socket connection
end

#force_quit?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/xrbp/websocket/connection.rb', line 91

def force_quit?
  @force_quit
end

#initialized?Boolean

Indicates the connection is initialized

Returns:

  • (Boolean)


53
54
55
# File 'lib/xrbp/websocket/connection.rb', line 53

def initialized?
  !!@client
end

#next_connection(prev) ⇒ Object

Return next connection of parent if applicable



40
41
42
43
# File 'lib/xrbp/websocket/connection.rb', line 40

def next_connection(prev)
  return nil unless !!parent
  parent.next_connection(prev)
end

#open?Boolean

Indicates the connection is open

Returns:

  • (Boolean)


58
59
60
# File 'lib/xrbp/websocket/connection.rb', line 58

def open?
  initialized? && client.open?
end

#open_cvObject



135
136
137
# File 'lib/xrbp/websocket/connection.rb', line 135

def open_cv
  @open_cv ||= ConditionVariable.new
end

#plugin_namespaceObject



16
17
18
# File 'lib/xrbp/websocket/connection.rb', line 16

def plugin_namespace
  WebSocket
end

#send_data(data) ⇒ Object

Send raw data via this connection



85
86
87
# File 'lib/xrbp/websocket/connection.rb', line 85

def send_data(data)
  client.send_data(data)
end

#state_mutexObject



131
132
133
# File 'lib/xrbp/websocket/connection.rb', line 131

def state_mutex
  @state_mutex ||= Mutex.new
end

#wait_for_closeObject

Block until connection is closed



114
115
116
117
118
119
120
# File 'lib/xrbp/websocket/connection.rb', line 114

def wait_for_close
  return unless initialized?

  state_mutex.synchronize {
    close_cv.wait(state_mutex, 0.1)
  } while !force_quit? && open?
end

#wait_for_completedObject

Block until connection is completed



123
124
125
126
127
128
129
# File 'lib/xrbp/websocket/connection.rb', line 123

def wait_for_completed
  return unless initialized?

  state_mutex.synchronize {
    completed_cv.wait(state_mutex, 0.1)
  } while !force_quit? && !completed?
end

#wait_for_openObject

Block until connection is open



105
106
107
108
109
110
111
# File 'lib/xrbp/websocket/connection.rb', line 105

def wait_for_open
  return unless initialized?

  state_mutex.synchronize {
    open_cv.wait(state_mutex, 0.1)
  } until force_quit? || open?
end