barrister-amqp 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/barrister/amqp.rb +30 -10
- data/lib/barrister/amqp/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5d666f62d0f15f879db33946493805770f7ebdfe
|
|
4
|
+
data.tar.gz: 08092e239d1262a62d7acfde0469794733b362b3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b1a1b715eebd09298c9cc83be12a21761eb4005d266a02f4d739c5256d2d9e31b02169de9007fd9acf2c88db2ab2191af0ce37af224405d58388076e04402923
|
|
7
|
+
data.tar.gz: 5af09b06d43e9564c4fac114759f341a19a79b9e43a91bf0d772ae0538ea51a4475b99479100de5bf983739b03f58c4d22e33985670d8d5ca7436315a3343d0f
|
data/lib/barrister/amqp.rb
CHANGED
|
@@ -3,16 +3,31 @@ require 'bunny'
|
|
|
3
3
|
|
|
4
4
|
module Barrister
|
|
5
5
|
module Amqp
|
|
6
|
+
Wrapper = Struct.new(:message) do
|
|
7
|
+
def wrap
|
|
8
|
+
{
|
|
9
|
+
"id" => Barrister.rand_str(22),
|
|
10
|
+
"message" => JSON.generate(message, { :ascii_only=>true })
|
|
11
|
+
}
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def unwrap
|
|
15
|
+
message
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Config = OpenStruct.new(debug: false, wrapper: Wrapper)
|
|
20
|
+
|
|
6
21
|
class Transport
|
|
7
22
|
# NOTE: transport needs to implement request method for the Barrister::Client to
|
|
8
23
|
# send requests to the server
|
|
9
24
|
def initialize(service_name, options={})
|
|
10
25
|
conn = Bunny.new ENV.fetch('AMQP_URL') #"Please set AMQP_URL to something like: amqp://user:password@host:port/vhost"
|
|
11
26
|
conn.start
|
|
12
|
-
@ch
|
|
13
|
-
@service_q
|
|
14
|
-
@reply_q
|
|
15
|
-
@x
|
|
27
|
+
@ch = conn.create_channel
|
|
28
|
+
@service_q = @ch.queue(service_name, auto_delete: false)
|
|
29
|
+
@reply_q = @ch.queue('', exclusive: true)
|
|
30
|
+
@x = @ch.default_exchange
|
|
16
31
|
@response_table = Hash.new { |h,k| h[k] = Queue.new }
|
|
17
32
|
|
|
18
33
|
@reply_q.subscribe(block: false) do |delivery_info, properties, payload|
|
|
@@ -21,14 +36,18 @@ module Barrister
|
|
|
21
36
|
end
|
|
22
37
|
|
|
23
38
|
def request(message)
|
|
24
|
-
|
|
39
|
+
enveloppe = Config.wrapper.new(message).wrap
|
|
40
|
+
# NOTE message could be an array
|
|
41
|
+
print "[AMQP TRANSPORT --->] \n #{enveloppe} \n" if Config.debug
|
|
42
|
+
@x.publish(enveloppe['message'], { correlation_id: enveloppe['id'], reply_to: @reply_q.name, routing_key: @service_q.name})
|
|
25
43
|
|
|
26
|
-
response = @response_table[
|
|
27
|
-
@response_table.delete
|
|
44
|
+
response = @response_table[enveloppe['id']].pop
|
|
45
|
+
@response_table.delete enveloppe['id']
|
|
28
46
|
|
|
29
47
|
begin
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
JSON.parse(response).tap do |resp|
|
|
49
|
+
print "[AMQP TRANSPORT <---] \n #{resp} \n" if Config.debug
|
|
50
|
+
end
|
|
32
51
|
rescue JSON::ParserError => e
|
|
33
52
|
raise RpcException.new(-32000, "Bad response #{e.message}")
|
|
34
53
|
end
|
|
@@ -62,7 +81,8 @@ module Barrister
|
|
|
62
81
|
|
|
63
82
|
def start
|
|
64
83
|
@service_q.subscribe(block: true) do |delivery_info, properties, payload|
|
|
65
|
-
|
|
84
|
+
payload = Config.wrapper.new(payload).unwrap
|
|
85
|
+
puts "handling payload: #{payload}" if Config.debug
|
|
66
86
|
@x.publish(@server.handle_json(payload), { routing_key: properties.reply_to, correlation_id: properties.correlation_id })
|
|
67
87
|
end
|
|
68
88
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: barrister-amqp
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- gregory
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-01-
|
|
11
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bunny
|