Class: XRBP::WebSocket::Connection

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
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

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

#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

#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

#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