io_request 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +3 -1
- data/examples/simple_example.rb +52 -0
- data/lib/io_request/client.rb +20 -10
- data/lib/io_request/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da825a2b5c4621543a1c66625eebf871852a07a5ed51db4fa3df51a26ed23874
|
4
|
+
data.tar.gz: a47015fa8c95cad7baadf55d0ef43921272c9fa4898d4a78f238b6f48ca15842
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a4ccf24e708cfe1bf1d428f780be958a54516dd57dbfc25b450eba57b4ea111dfcc09f88a1fdfefe12eb55bf450541665f3e0a3eb3458406c9958d843f8af3b
|
7
|
+
data.tar.gz: 044ff5beb060aa71aa95971f3d4ad6eb2b0dab452a3c2df16460adafc8adae690a8d13f2a9993dcf32d43020abd03cbc041ac23d7aec97674247b12aead5bed7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "io_request"
|
2
|
+
|
3
|
+
r1, w1 = IO.pipe
|
4
|
+
r2, w2 = IO.pipe
|
5
|
+
|
6
|
+
client_1 = IORequest::Client.new read: r1, write: w2
|
7
|
+
client_2 = IORequest::Client.new read: r2, write: w1
|
8
|
+
|
9
|
+
# Use
|
10
|
+
# Set up responders
|
11
|
+
# Authorization
|
12
|
+
client_2.respond type: "auth" do |request|
|
13
|
+
puts "Client 2: Authorization attempt as #{request.data[:username].inspect}"
|
14
|
+
sleep 2 # Some processing
|
15
|
+
{ type: "auth_success" }
|
16
|
+
end
|
17
|
+
|
18
|
+
# Default
|
19
|
+
client_2.respond do |request|
|
20
|
+
puts "Client 2: #{request.data.inspect}"
|
21
|
+
{ type: "success" }
|
22
|
+
end
|
23
|
+
|
24
|
+
# Send requests
|
25
|
+
auth = false
|
26
|
+
auth_request = client_1.request(
|
27
|
+
data: { type: "auth", username: "mymail@example.com", password: "let's pretend password hash is here" },
|
28
|
+
sync: true
|
29
|
+
) do |response|
|
30
|
+
unless response.data[:type] == "auth_success"
|
31
|
+
puts "Client 1: Authorization failed. Response: #{response.data.inspect}"
|
32
|
+
next
|
33
|
+
end
|
34
|
+
|
35
|
+
auth = true
|
36
|
+
# Do something
|
37
|
+
end
|
38
|
+
exit unless auth
|
39
|
+
puts "Client 1: Authorized!"
|
40
|
+
|
41
|
+
message = client_1.request(
|
42
|
+
data: { type: "message", message: "Hello!" },
|
43
|
+
sync: true
|
44
|
+
) do |response|
|
45
|
+
puts "Client 1: Message responded"
|
46
|
+
end
|
47
|
+
|
48
|
+
# Close
|
49
|
+
r1.close
|
50
|
+
w1.close
|
51
|
+
r2.close
|
52
|
+
w2.close
|
data/lib/io_request/client.rb
CHANGED
@@ -8,9 +8,6 @@ module IORequest
|
|
8
8
|
include Utility::WithProgName
|
9
9
|
include Utility::MultiThread
|
10
10
|
|
11
|
-
# IO-like object provided at initialization.
|
12
|
-
attr_reader :io
|
13
|
-
|
14
11
|
# Initialize new client over IO.
|
15
12
|
#
|
16
13
|
# @option options [:gets] read IO to read from.
|
@@ -31,8 +28,7 @@ module IORequest
|
|
31
28
|
#
|
32
29
|
# Optional block can be provided. It will be called when response received.
|
33
30
|
#
|
34
|
-
# @
|
35
|
-
#
|
31
|
+
# @option options [Hash] data data to send.
|
36
32
|
# @option options [Boolean] sync whether to join request after sending.
|
37
33
|
# @option options [Integer, Float] timeout timeout for {Request#join}.
|
38
34
|
#
|
@@ -42,6 +38,7 @@ module IORequest
|
|
42
38
|
def request(data: {}, sync: false, timeout: nil, &block)
|
43
39
|
req = Request.new(data)
|
44
40
|
@out_requests[req] = block
|
41
|
+
IORequest.debug("Sending request ##{req.id}", prog_name)
|
45
42
|
send(req.to_hash)
|
46
43
|
req.join(timeout) if sync
|
47
44
|
req
|
@@ -85,12 +82,18 @@ module IORequest
|
|
85
82
|
in_thread do
|
86
83
|
responder = find_responder(req)
|
87
84
|
data = nil
|
88
|
-
begin
|
89
|
-
|
85
|
+
data = begin
|
86
|
+
if responder
|
87
|
+
responder.call(req)
|
88
|
+
else
|
89
|
+
IORequest.warn "Responder not found!"
|
90
|
+
nil
|
91
|
+
end
|
90
92
|
rescue Exception => e
|
91
|
-
IORequest.warn
|
93
|
+
IORequest.warn "Provided block raised exception:\n#{e.full_message}", prog_name
|
94
|
+
nil
|
92
95
|
end
|
93
|
-
data
|
96
|
+
data ||= {}
|
94
97
|
res = Response.new(data, req)
|
95
98
|
send(res.to_hash)
|
96
99
|
end
|
@@ -121,15 +124,22 @@ module IORequest
|
|
121
124
|
|
122
125
|
# find responder for provided request.
|
123
126
|
def find_responder(req)
|
127
|
+
result = nil
|
124
128
|
@responders.each do |subdata, block|
|
125
|
-
|
129
|
+
if req.data.contains? subdata
|
130
|
+
result = block
|
131
|
+
break
|
132
|
+
end
|
126
133
|
end
|
134
|
+
|
135
|
+
result
|
127
136
|
end
|
128
137
|
|
129
138
|
# Send data.
|
130
139
|
#
|
131
140
|
# @param [Hash]
|
132
141
|
def send(data)
|
142
|
+
IORequest.debug("Sending hash: #{data.inspect}", prog_name)
|
133
143
|
send_raw(encode(data_to_string data))
|
134
144
|
end
|
135
145
|
|
data/lib/io_request/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: io_request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fizvlad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-11-
|
11
|
+
date: 2019-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -109,6 +109,7 @@ files:
|
|
109
109
|
- README.md
|
110
110
|
- Rakefile
|
111
111
|
- bin/console
|
112
|
+
- examples/simple_example.rb
|
112
113
|
- io_request.gemspec
|
113
114
|
- lib/io_request.rb
|
114
115
|
- lib/io_request/client.rb
|