io_request 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f13666edea5cbb28ef091256048953a91693bb015d089b497dfa2657b75e2ca5
4
- data.tar.gz: d05a984fe433d148c049f2156637fe7bf7ff6e19aa89f85d90f776e4337b8110
3
+ metadata.gz: 39b0e0eb362ea37ce9f2b7359ec439bfc422ca12b2f89ba4483a8cba20c99a2f
4
+ data.tar.gz: 7c7fc713e6de0386789cabbde9a68183d52c8cba268bce30ca9a76c503cb8f3b
5
5
  SHA512:
6
- metadata.gz: a4abe0a1f30f5760ab110f9b8ad097d4eda24548418398d0b24102d4b2fd8bb652082414c993672aa81915a939e8f07130a87eb1f137543ca87a54a1e0879ead
7
- data.tar.gz: 5a541175e7b27d967c22333f700a2a08b4de10b4fa0804230a5ef90406d6be28eeaa62025e9dca32d8986dd553b846cbfd16c2cc5b66848eb81b0a0ec2a08099
6
+ metadata.gz: 5473de710bff683d5dc610a8eb43207353853280c5a4d5a81eccefb1cf40ea924811b5cc7ad16b9b155ebc43186a8691a32c0648cd6dc80c1bb44ba697b38507
7
+ data.tar.gz: 8121c16e2f3e3d1b8776aff30d34415a1d29699d9725208621c5af62b17f2664011cea4a03f7b2de370c676dfd0c3f3ec274de9700ba1980ad4e749864cb5bb6
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Main module.
4
- module IORequest; end
4
+ module IORequest
5
+ # Client received message of zero size.
6
+ class ZeroSizeMessageError < RuntimeError; end
7
+ end
5
8
 
6
9
  require_relative 'io_request/version'
7
10
  require_relative 'io_request/logging'
@@ -63,15 +63,21 @@ module IORequest
63
63
  # Close connection.
64
64
  def close
65
65
  close_internal
66
+
66
67
  join_threads
67
- @open = false
68
68
  end
69
69
 
70
70
  # @yieldparam [Hash]
71
71
  # @yieldreturn [Hash]
72
- def respond(&block)
73
- IORequest.logger.debug(prog_name) { 'Saved responder block' }
74
- @responder = block
72
+ def on_request(&block)
73
+ IORequest.logger.debug(prog_name) { 'Saved on_request block' }
74
+ @on_request = block
75
+ end
76
+ alias respond on_request
77
+
78
+ def on_close(&block)
79
+ IORequest.logger.debug(prog_name) { 'Saved on_close block' }
80
+ @on_close = block
75
81
  end
76
82
 
77
83
  # If callback block is provided, request will be sent asynchroniously.
@@ -96,27 +102,25 @@ module IORequest
96
102
 
97
103
  def close_internal
98
104
  IORequest.logger.debug(prog_name) { 'Closing connection' }
99
- begin
100
- send_zero_size_request
101
- rescue StandardError
102
- IORequest.logger.debug(prog_name) { 'Failed to send zero-sized message. Closing anyway' }
103
- end
104
- stop_data_transition
105
+ send_zero_size_request
105
106
  close_io
106
- end
107
-
108
- def stop_data_transition
109
- return unless defined?(@data_transition_thread) && !@data_transition_thread.nil?
110
-
111
- IORequest.logger.debug(prog_name) { 'Killing data transition thread' }
112
- @data_transition_thread.kill
113
107
  @data_transition_thread = nil
108
+ @open = false
109
+ @on_close&.call if defined?(@on_close)
114
110
  end
115
111
 
116
112
  def close_io
117
- IORequest.logger.debug(prog_name) { 'Closing IO' }
118
- @mutex_r.synchronize { @io_r&.close }
119
- @mutex_w.synchronize { @io_w&.close }
113
+ begin
114
+ @io_r&.close
115
+ rescue StandardError => e
116
+ IORequest.logger.debug "Failed to close read IO: #{e}"
117
+ end
118
+ begin
119
+ @io_w&.close
120
+ rescue StandardError => e
121
+ IORequest.logger.debug "Failed to close write IO: #{e}"
122
+ end
123
+ IORequest.logger.debug(prog_name) { 'Closed IO streams' }
120
124
  end
121
125
 
122
126
  def authorization
@@ -138,8 +142,11 @@ module IORequest
138
142
  IORequest.logger.debug(prog_name) { 'Starting data transition loop' }
139
143
  loop do
140
144
  data_transition_iteration
145
+ rescue ZeroSizeMessageError
146
+ IORequest.logger.debug(prog_name) { 'Connection was closed from the other side' }
147
+ break
141
148
  rescue StandardError => e
142
- IORequest.logger.debug(prog_name) { "Data transition iteration failed: #{e}" }
149
+ IORequest.logger.debug(prog_name) { "Data transition unknown error: #{e}" }
143
150
  break
144
151
  end
145
152
  close_internal
@@ -156,7 +163,8 @@ module IORequest
156
163
  end
157
164
 
158
165
  def handle_request(message)
159
- data = @responder&.call(message.data) || {}
166
+ data = {}
167
+ data = @on_request&.call(message.data) if defined?(@on_request)
160
168
  response = Message.new(data, type: :response, to: message.id)
161
169
  send_response(response)
162
170
  end
@@ -180,6 +188,8 @@ module IORequest
180
188
  IORequest.logger.debug(prog_name) { 'Sending zero size message' }
181
189
  @io_w.write([0].pack('S'))
182
190
  end
191
+ rescue StandardError => e
192
+ IORequest.logger.debug(prog_name) { "Failed to send zero-sized message(#{e})" }
183
193
  end
184
194
 
185
195
  def send_request_and_wait_for_response(request)
@@ -79,8 +79,11 @@ module IORequest
79
79
  client = IORequest::Client.new authorizer: @authorizer
80
80
  begin
81
81
  client.open read_write: ssl_socket
82
- client.respond { |data| @requests_handler.call(data, client) }
82
+ client.on_request { |data| @requests_handler.call(data, client) }
83
83
  @clients << client
84
+ client.on_close do
85
+ @clients.select!(&:open?)
86
+ end
84
87
  rescue StandardError
85
88
  IORequest.debug "Failed to open client: #{e}"
86
89
  ssl_socket.close
@@ -121,9 +124,8 @@ module IORequest
121
124
  @client = IORequest::Client.new authorizer: @authorizer
122
125
  begin
123
126
  @client.open read_write: ssl_socket
124
- @client.respond(&@requests_handler)
127
+ @client.on_request(&@requests_handler)
125
128
  rescue StandardError
126
- IORequest.debug "Failed to open client: #{e}"
127
129
  ssl_socket.close
128
130
  @client = nil
129
131
  end
@@ -81,8 +81,8 @@ module IORequest
81
81
  # @param io_r [:read]
82
82
  # @return [Message]
83
83
  def self.read_from(io_r)
84
- size = io_r.read(2).unpack1('S')
85
- raise '0 size received' if size.zero?
84
+ size = io_r.read(2)&.unpack1('S') || 0
85
+ raise ZeroSizeMessageError if size.zero?
86
86
 
87
87
  json_string = io_r.read(size).unpack1("a#{size}")
88
88
  msg = JSON.parse(json_string, symbolize_names: true)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module IORequest
4
4
  # Gem version.
5
- VERSION = '2.2.0'
5
+ VERSION = '2.3.0'
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io_request
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fizvlad