yolodice-client 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -0
  3. data/lib/yolodice_client.rb +44 -5
  4. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28beeaeb68b98fbd9f62194e22ca472c73194283
4
- data.tar.gz: a59197fdaa0adda43544cc8053eb2a596c188d98
3
+ metadata.gz: 89b107ea48a90ef892fd4b9a3039da928cc57ad0
4
+ data.tar.gz: 0d25fee7964cbf8affb821ce40ff4e1f1025e603
5
5
  SHA512:
6
- metadata.gz: 551030f12886d365a26cebcdcf9f703196b77f413afb82361ced5e5409b6362fb22f6981829ddd6a5a3324203807c658fa33ef2f833e5da50b2ead2c68629a15
7
- data.tar.gz: d06b7f79620c175ac78c720aeecd62bd5f82062cfa1efe51b31807d589b854420bb84044b0c0122db5aa0b3f81405b13a0901cd940e199d083b6963487be898b
6
+ metadata.gz: 2384127fe8a12093d74cafbc806d0c68cbaba6fc051c4fbd5d669101e0791bdfec63153bd45f004435fc7088f6bb13af6960ae8cd2d3e7986b6e7c7b7b4b6b2f
7
+ data.tar.gz: 30207911b9426ece6d7d053cb4f02efc63ce0207a023649e70500678d436c740dc47912f0308df569377e31c01f4c4156aedab7f9e2bf2b8ef05560e34f2a038
data/README.md CHANGED
@@ -97,10 +97,13 @@ There are two error classes:
97
97
 
98
98
  * `YolodiceClient::Error` that inherits from `StandardError` and is used for errors thrown by the client itself,
99
99
  * `YolodiceClient::RemoteError` that inherits from `StandardError` that is used to pass errors from the remote server,
100
+ * `YolodiceClient::ConnectionCloserError` that inherits from `StandardError` that is thrown when connection is closed,
100
101
  * any errors from underlaying `TCPSocket` or `SSLSocket` are passed through.
101
102
 
102
103
  `RemoteError` has two extra attributes: `code` and `data` that are mapped to values in the error object returned from the server.
103
104
 
105
+ If you want to handle connection issues within your app, you could rescue `YolodiceClient::ConnectionCloserError`, `TCPSocket` and `SSLSocket` errors and try `#connect` and `#authenticate` on error.
106
+
104
107
  ## Helper methods
105
108
 
106
109
  #### .satoshi_to_btc(v)
@@ -52,6 +52,10 @@ class YolodiceClient
52
52
  # Connects to the host.
53
53
 
54
54
  def connect
55
+ if @connection && !@connection.closed?
56
+ raise Error, 'Connection already open'
57
+ end
58
+
55
59
  @connection = if @opts[:ssl]
56
60
  log.debug "Connecting to #{@opts[:host]}:#{@opts[:port]} over SSL"
57
61
  socket = TCPSocket.open @opts[:host], @opts[:port]
@@ -70,8 +74,16 @@ class YolodiceClient
70
74
  log.debug 'Listening thread started'
71
75
  loop do
72
76
  begin
77
+ if @connection.closed?
78
+ # keep the thread alive for a while
79
+ sleep 1
80
+ end
81
+
73
82
  msg = @connection.gets
74
- next if msg == nil
83
+ if msg == nil
84
+ # Connection is closed upstream.
85
+ close
86
+ end
75
87
  log.debug{ "<<< #{msg}" }
76
88
  message = JSON.parse msg
77
89
  if message['id'] && (message.has_key?('result') || message.has_key?('error'))
@@ -110,7 +122,7 @@ class YolodiceClient
110
122
  loop do
111
123
  begin
112
124
  sleep 30
113
- call :ping
125
+ call :ping unless @connection.closed?
114
126
  rescue StandardError => e
115
127
  log.error e
116
128
  end
@@ -127,8 +139,23 @@ class YolodiceClient
127
139
  log.debug "Closing connection"
128
140
  # Stop threads
129
141
  @connection.close
130
- @listening_thread.exit
131
- @pinging_thread.exit
142
+ # Send error to all pending requests
143
+ message = { 'client_error' => 'Connection closed' }
144
+ while(callback = @thread_semaphore.synchronize{ @current_requests.shift }) do
145
+ callback = callback[1]
146
+ if callback.is_a? Integer
147
+ # it's a thread
148
+ @receive_queues[callback] << message
149
+ elsif callback.is_a? Proc
150
+ # A thread pool would be better.
151
+ Thread.new do
152
+ callback.call message
153
+ end
154
+ end
155
+ end
156
+ # Thread.stop
157
+ @pinging_thread.kill
158
+ @listening_thread.kill
132
159
  true
133
160
  end
134
161
 
@@ -162,7 +189,7 @@ class YolodiceClient
162
189
  # * <tt>&blk</tt> -- a callback (optional) to be called upon receiving a response for async calls. The callback will receive the response object.
163
190
 
164
191
  def call method, *arguments, &blk
165
- raise Error, "Not connected" unless @connection && !@connection.closed?
192
+ raise ConnectionClosedError, "Not connected" unless @connection && !@connection.closed?
166
193
  params = if arguments.count == 0
167
194
  nil
168
195
  elsif arguments.is_a?(Array) && arguments[0].is_a?(Hash)
@@ -195,10 +222,17 @@ class YolodiceClient
195
222
  response['result']
196
223
  elsif response['error']
197
224
  raise RemoteError.new response['error']
225
+ elsif response['client_error'] && response['client_error'] == 'Connection closed'
226
+ raise ConnectionClosedError
198
227
  end
199
228
  end
200
229
  end
201
230
 
231
+
232
+ def check_connection
233
+ raise ConnectionClosedError if @connection.closed?
234
+ end
235
+
202
236
 
203
237
  ##
204
238
  # Overloading the <tt>method_missing</tt> gives a convenience way to call server-side methods. This method calls the <tt>call</tt> with the same set of arguments.
@@ -220,6 +254,11 @@ class YolodiceClient
220
254
 
221
255
  class Error < StandardError; end
222
256
 
257
+ ##
258
+ # Thrown whenever the connection is closed while it is not supposed to be.
259
+
260
+ class ConnectionClosedError < StandardError; end
261
+
223
262
 
224
263
  ##
225
264
  # Thrown when an error is received from the server. <tt>RemoteError</tt> has two extra attributes: <tt>code</tt> and <tt>data</tt> that correspond to the values returned in the error object in server response.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yolodice-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ethan_nx
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-04 00:00:00.000000000 Z
11
+ date: 2017-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bitcoin-ruby
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.8
19
+ version: 0.0.10
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.8
26
+ version: 0.0.10
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ffi
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '1.9'
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 1.9.10
36
+ version: 1.9.18
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '1.9'
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 1.9.10
46
+ version: 1.9.18
47
47
  description: A simple JSON-RPC2 client dedicated for YOLOdice.com API.
48
48
  email:
49
49
  executables: []