bunny_rpc 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fa099b5e974ffd64bb077db700fa5a967b6dd9e
4
- data.tar.gz: 19330688ff0fb4c40166fb95587cf9c918b7025c
3
+ metadata.gz: 85b3814ee9e2fd9a8071c13f473144edb7b3c884
4
+ data.tar.gz: 575cab001d663544971e53a18d7bd85b93ebe987
5
5
  SHA512:
6
- metadata.gz: 393619942b80f7591b88ce83a4efaa6ab0680c03f59c24f69d65c8018f96b11a278268ebb32da7d82f6c6927ffe7a5ce289a6ac72347bf39006e71bb8763b084
7
- data.tar.gz: 10de4e824d4ee17cf72c83619898c16ba8ee2ad3109a2e0874e02eba6749a5dbc0a278bb9279b7a07af003769c2b8d8f47cc323ff5e7079d1b5cd0f9cc4e4569
6
+ metadata.gz: 7dd3daad75a0a18d4aa7ed11d42a0cf62d30b9e0d69adf4116256d1243758cc936e3df015b83a766770c4de5cadb932d6cf0ebeb18e7b1b306ef475f9d0255c1
7
+ data.tar.gz: d18a2333bfdc23ab0bb846b3b862c83bf88cb332ed8170d58986774df5c63594db4a3e6c42443629bbdcaa295e29727d4f1c92345f37f5c9d76088a4151fac5b
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
+ /test/
data/README.md CHANGED
@@ -22,20 +22,24 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ### Client
26
26
 
27
- ## Development
27
+ require 'bunny_rpc'
28
28
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+ client = BunnyRpc::Client.new
30
30
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+ result = client.publish('playload')
32
32
 
33
- ## Contributing
33
+ ### Server
34
34
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bunny_rpc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
35
+ require 'bunny_rpc'
36
36
 
37
+ client = BunnyRpc::Server.new('queue1')
37
38
 
38
- ## License
39
+ client.subscribe do |data|
40
+ puts data
39
41
 
40
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+ payload = 'get it !'
41
43
 
44
+ client.publish(payload)
45
+ end
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "bunny_rpc"
3
+ require 'bundler/setup'
4
+ require 'bunny_rpc'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/bunny_rpc.gemspec CHANGED
@@ -4,29 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'bunny_rpc/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "bunny_rpc"
7
+ spec.name = 'bunny_rpc'
8
8
  spec.version = BunnyRpc::VERSION
9
- spec.authors = ["menghuanwd"]
10
- spec.email = ["651019063@qq.com"]
9
+ spec.authors = ['menghuanwd']
10
+ spec.email = ['651019063@qq.com']
11
11
 
12
- spec.summary = %q{bunny_rpc}
13
- spec.description = %q{bunny_rpc}
14
- spec.homepage = "https://github.com/menghuanwd/bunny_rpc"
15
- spec.license = "MIT"
12
+ spec.summary = 'bunny_rpc'
13
+ spec.description = 'bunny_rpc'
14
+ spec.homepage = 'https://github.com/menghuanwd/bunny_rpc'
15
+ spec.license = 'MIT'
16
16
 
17
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
- # delete this section to allow pushing this gem to any host.
19
- # if spec.respond_to?(:metadata)
20
- # spec.metadata['allowed_push_host'] = "https://github.com/menghuanwd/bunny_rpc"
21
- # else
22
- # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
- # end
17
+ spec.add_dependency 'bunny', '~> 2.3.0'
24
18
 
25
19
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "exe"
20
+ spec.bindir = 'exe'
27
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
22
+ spec.require_paths = ['lib']
29
23
 
30
- spec.add_development_dependency "bundler", "~> 1.11"
31
- spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'bundler', '~> 1.11'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
32
26
  end
@@ -0,0 +1,75 @@
1
+ require 'bunny'
2
+
3
+ module BunnyRpc
4
+ class Client
5
+ attr_reader :payload, :correlation_id
6
+
7
+ def initialize(options={})
8
+ @options = options
9
+ @mutex = Mutex.new
10
+ @cv = ConditionVariable.new
11
+ end
12
+
13
+ def publish(body, queue_name)
14
+ subscribe
15
+
16
+ exchange.publish(body, routing_key: queue_name, reply_to: reply_queue.name, correlation_id: @correlation_id)
17
+
18
+ add_lock
19
+
20
+ @payload
21
+ end
22
+
23
+ def close
24
+ @connect.stop
25
+ end
26
+
27
+ def subscribe
28
+ reply_queue.subscribe do |_, properties, payload|
29
+ if properties.correlation_id == @correlation_id
30
+ @payload = payload
31
+
32
+ del_lock
33
+ else
34
+ del_lock
35
+ raise 'correlation_id error !'
36
+ end
37
+ end
38
+ end
39
+
40
+ def add_lock
41
+ @mutex.synchronize { @cv.wait(@mutex) }
42
+ end
43
+
44
+ def del_lock
45
+ @mutex.synchronize { @cv.signal }
46
+ end
47
+
48
+ def connect
49
+ return @connect if defined?(@connect)
50
+ @connect = Bunny.new(@options)
51
+ @connect.start
52
+ @connect
53
+ end
54
+
55
+ def channel
56
+ @channel ||= connect.create_channel
57
+ end
58
+
59
+ def exchange
60
+ @exchange ||= channel.default_exchange
61
+ end
62
+
63
+ def reply_queue
64
+ @reply_queue ||= channel.queue(reply_queue_name, exclusive: true, auto_delete: true)
65
+ end
66
+
67
+ def reply_queue_name
68
+ "reply_to-#{rand(10)}"
69
+ end
70
+
71
+ def correlation_id
72
+ "#{rand(10)}#{rand(10)}#{rand(10)}"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,47 @@
1
+ module BunnyRpc
2
+ class Server
3
+ attr_accessor :reply_correlation_id, :reply_to
4
+
5
+ def initialize(queue_name, options={})
6
+ @queue_name = queue_name
7
+ @options = options
8
+ end
9
+
10
+ def publish(payload)
11
+ exchange.publish(
12
+ payload,
13
+ routing_key: @reply_to,
14
+ correlation_id: @reply_correlation_id
15
+ )
16
+ end
17
+
18
+ def subscribe
19
+ queue.subscribe(block: true) do |_, properties, payload|
20
+ @reply_to = properties.reply_to
21
+ @reply_correlation_id = properties.correlation_id
22
+
23
+ yield payload
24
+ end
25
+ end
26
+
27
+ def connect
28
+ return @connect if defined?(@connect)
29
+ @connect = Bunny.new(@options)
30
+ @connect.start
31
+ @connect
32
+ end
33
+
34
+ def channel
35
+ @channel ||= connect.create_channel
36
+ end
37
+
38
+ def exchange
39
+ @exchange ||= channel.default_exchange
40
+ end
41
+
42
+ def queue
43
+ @queue ||= channel.queue(@queue_name, exclusive: true)
44
+ end
45
+
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module BunnyRpc
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'.freeze
3
3
  end
data/lib/bunny_rpc.rb CHANGED
@@ -1,5 +1,3 @@
1
- require "bunny_rpc/version"
2
-
3
- module BunnyRpc
4
- # Your code goes here...
5
- end
1
+ require 'bunny_rpc/version'
2
+ require 'bunny_rpc/client'
3
+ require 'bunny_rpc/server'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - menghuanwd
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-01 00:00:00.000000000 Z
11
+ date: 2016-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -55,6 +69,8 @@ files:
55
69
  - bin/setup
56
70
  - bunny_rpc.gemspec
57
71
  - lib/bunny_rpc.rb
72
+ - lib/bunny_rpc/client.rb
73
+ - lib/bunny_rpc/server.rb
58
74
  - lib/bunny_rpc/version.rb
59
75
  homepage: https://github.com/menghuanwd/bunny_rpc
60
76
  licenses: