librmdp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'minitest'
4
+ gem 'simplecov', :require => false, :group => :test
5
+ # Specify your gem's dependencies in librmdp.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Benoist
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Librmdp
2
+
3
+ A ruby implementation of the majordomo pattern
4
+ http://rfc.zeromq.org/spec:7
5
+
6
+ At this stage it's in POC stage. Use at own risk.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'librmdp'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install librmdp
21
+
22
+ ## Usage
23
+
24
+ Start a broker
25
+ ==============
26
+
27
+ broker = Majordomo::Broker.new
28
+ broker.mediate
29
+
30
+ Build a worker with 'echo' as service name
31
+ ==========================================
32
+
33
+ worker = Majordomo::Worker.new('tcp://0.0.0.0:5555', 'echo')
34
+
35
+ loop do
36
+ request = worker.receive_message(reply_to = '')
37
+ # do something with a request
38
+ worker.send_message(request, reply_to)
39
+ end
40
+
41
+ Build a client
42
+ ==============
43
+ client = Majordomo::Client.new('tcp://0.0.0.0:5555')
44
+ client.send_message('echo', 'a')
45
+
46
+ response = client.receive_message
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib"
6
+ t.libs.push "test"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+ desc "Run tests"
11
+ task :default => :test
data/lib/librmdp.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'ffi-rzmq'
2
+ require 'majordomo/version'
3
+
4
+ module Majordomo
5
+ CLIENT = 'MDPC01'
6
+ WORKER = 'MDPW01'
7
+
8
+ READY = "\x01"
9
+ REQUEST = "\x02"
10
+ REPLY = "\x03"
11
+ HEARTBEAT = "\x04"
12
+ DISCONNECT = "\x05"
13
+ HEARTBEAT_LIVENESS = 3 # 3-5 is reasonable
14
+ HEARTBEAT_INTERVAL = 1000 # msecs
15
+ HEARTBEAT_EXPIRY = HEARTBEAT_INTERVAL * HEARTBEAT_LIVENESS
16
+ end
17
+
18
+ require 'majordomo/broker'
19
+ require 'majordomo/worker'
20
+ require 'majordomo/client'
@@ -0,0 +1,125 @@
1
+ require 'majordomo/broker/service'
2
+ require 'majordomo/broker/worker'
3
+
4
+ module Majordomo
5
+ class Broker
6
+
7
+ attr_accessor :workers, :waiting, :services, :socket
8
+
9
+ def initialize(bind = 'tcp://*:5555')
10
+ @context = ZMQ::Context.new
11
+ @socket = @context.socket(ZMQ::ROUTER)
12
+ @poller = ZMQ::Poller.new
13
+ @services = {}
14
+ @workers = {}
15
+ @waiting = []
16
+ @heartbeat_at = Time.now + 0.001 * HEARTBEAT_INTERVAL
17
+
18
+ @socket.bind(bind)
19
+ @poller.register(@socket, ZMQ::POLLIN)
20
+ trap(:INT) { exit }
21
+ at_exit { destroy }
22
+ end
23
+
24
+ def mediate
25
+ loop do
26
+ items = @poller.poll(HEARTBEAT_INTERVAL)
27
+
28
+ if items > 0
29
+ @socket.recv_strings(message = [])
30
+ sender = message.shift
31
+ message.shift #empty
32
+ header = message.shift
33
+ case header
34
+ when CLIENT
35
+ message_client(sender, message)
36
+ when WORKER
37
+ message_worker(sender, message)
38
+ end
39
+ end
40
+
41
+ if Time.now > @heartbeat_at
42
+ purge
43
+ waiting.each do |worker|
44
+ worker.send_message(HEARTBEAT)
45
+ end
46
+ @heartbeat_at = Time.now + 0.001 * HEARTBEAT_INTERVAL
47
+ puts "Heartbeat #{waiting.count}"
48
+ end
49
+ end
50
+ end
51
+
52
+ def destroy
53
+ @socket.close
54
+ @context.terminate
55
+ end
56
+
57
+ def bind(endpoint)
58
+ @socket.bind(endpoint)
59
+ end
60
+
61
+ def message_worker(sender, message)
62
+ command = message.shift
63
+ identity = sender.unpack('H*').first
64
+ worker_ready = !!@workers[identity]
65
+
66
+ worker = Worker.require(self, sender)
67
+
68
+ case command
69
+ when READY
70
+ if worker_ready
71
+ worker.delete(true)
72
+ else
73
+ service = message.shift
74
+ worker.service = Service.require(self, service)
75
+ @waiting << worker
76
+ worker.service.waiting << worker
77
+ worker.service.workers += 1
78
+ worker.expires_at = Time.now + 0.001 * HEARTBEAT_EXPIRY
79
+ worker.service.dispatch
80
+ end
81
+ when REPLY
82
+ if worker_ready
83
+ client = message.shift
84
+ message.shift
85
+ message.unshift(worker.service.name)
86
+ message.unshift(CLIENT)
87
+ message.unshift(nil)
88
+ message.unshift(client)
89
+ @socket.send_strings(message)
90
+ else
91
+ worker.delete(true)
92
+ end
93
+ when HEARTBEAT
94
+ if worker_ready
95
+ if @waiting.any?
96
+ @waiting.delete(worker)
97
+ @waiting << worker
98
+ end
99
+ worker.expires_at = Time.now + 0.001 * HEARTBEAT_EXPIRY
100
+ else
101
+ worker.delete(true)
102
+ end
103
+ when DISCONNECT
104
+ worker.delete(false)
105
+ end
106
+ end
107
+
108
+ def message_client(sender, message)
109
+ service_name = message.shift
110
+ service = Service.require(self, service_name)
111
+
112
+ message.unshift nil
113
+ message.unshift sender
114
+
115
+ service.requests << message
116
+ service.dispatch
117
+ end
118
+
119
+ def purge
120
+ waiting.each do |worker|
121
+ worker.delete(false) if Time.now > worker.expires_at
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,52 @@
1
+ require 'set'
2
+
3
+ module Majordomo
4
+ class Broker
5
+ class Service
6
+ attr_accessor :broker, :name, :requests, :waiting, :workers, :blacklist
7
+
8
+ def initialize(broker, name)
9
+ @broker = broker
10
+ @name = name
11
+ @requests = []
12
+ @waiting = []
13
+ @workers = 0
14
+ @blacklist = ::Set.new
15
+ end
16
+
17
+ def self.require(broker, name)
18
+ service = broker.services[name]
19
+
20
+ if service == nil
21
+ service = self.new(broker, name)
22
+ broker.services[name] = service
23
+ end
24
+ service
25
+ end
26
+
27
+ def dispatch
28
+ broker.purge
29
+ return if waiting.empty?
30
+
31
+ while requests.any?
32
+ worker = waiting.shift
33
+ message = requests.shift
34
+ worker.send_message(REQUEST, message)
35
+ waiting << worker
36
+ end
37
+ end
38
+
39
+ def enable_command(command)
40
+ @blacklist.delete(command)
41
+ end
42
+
43
+ def disable_command(command)
44
+ @blacklist.add(command)
45
+ end
46
+
47
+ def command_enabled?(command)
48
+ @blacklist.include?(command)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,47 @@
1
+ module Majordomo
2
+ class Broker
3
+ class Worker
4
+ attr_accessor :broker, :identity, :address, :service, :expires_at
5
+
6
+ def initialize(broker, identity, address)
7
+ @broker = broker
8
+ @identity = identity
9
+ @address = address
10
+ end
11
+
12
+ def self.require(broker, address)
13
+ identity = address.unpack('H*').first
14
+ worker = broker.workers[identity]
15
+
16
+ if worker == nil
17
+ worker = self.new(broker, identity, address)
18
+ broker.workers[identity] = worker
19
+ end
20
+
21
+ worker
22
+ end
23
+
24
+ def delete(disconnect)
25
+ if disconnect
26
+ send_message(DISCONNECT)
27
+ end
28
+
29
+ if service
30
+ service.waiting.delete(self)
31
+ service.workers -= 1
32
+ end
33
+ broker.waiting.delete(self)
34
+ broker.workers.delete(identity)
35
+ end
36
+
37
+ def send_message(command, message = [])
38
+ message.unshift(command)
39
+ message.unshift(WORKER)
40
+ message.unshift(nil)
41
+ message.unshift(address)
42
+
43
+ broker.socket.send_strings(message)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ module Majordomo
2
+ class Client
3
+ attr_accessor :timeout, :client
4
+
5
+ def initialize(broker)
6
+ @context = ZMQ::Context.new
7
+ @poller = ZMQ::Poller.new
8
+ @broker = broker
9
+
10
+ @timeout = 2500
11
+
12
+ connect_to_broker
13
+ end
14
+
15
+ def connect_to_broker
16
+ if @client
17
+ @poller.deregister(@client, ZMQ::POLLIN)
18
+ @client.close
19
+ end
20
+ @client = @context.socket(ZMQ::DEALER)
21
+ @client.connect(@broker)
22
+ @poller.register(@client, ZMQ::POLLIN)
23
+ end
24
+
25
+ def send_message(service, request)
26
+ request = [request] unless request.is_a?(Array)
27
+ request.unshift(service)
28
+ request.unshift(CLIENT)
29
+ request.unshift('')
30
+ @client.send_strings(request)
31
+ end
32
+
33
+ def receive_message
34
+ @client.recv_strings(message = [])
35
+ message.shift
36
+ header = message.shift
37
+ raise unless header == CLIENT
38
+
39
+ message.shift #service
40
+
41
+ message
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Majordomo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,89 @@
1
+ module Majordomo
2
+ class Worker
3
+ attr_accessor :broker, :service, :worker, :heartbeat_at, :heartbeat, :liveness, :reconnect
4
+
5
+ def initialize(broker, service)
6
+ @context = ZMQ::Context.new
7
+ @poller = ZMQ::Poller.new
8
+ @broker = broker
9
+ @service = service
10
+
11
+ @heartbeat = HEARTBEAT_INTERVAL
12
+ @reconnect = HEARTBEAT_INTERVAL
13
+ connect_to_broker
14
+
15
+ trap(:INT) { exit }
16
+ at_exit { destroy }
17
+ end
18
+
19
+ def destroy
20
+ @worker.close
21
+ @context.terminate
22
+ end
23
+
24
+ def connect_to_broker
25
+ if @worker
26
+ puts 'Closing connection to broker'
27
+ @poller.deregister(@worker, ZMQ::POLLIN)
28
+ @worker.close
29
+ end
30
+ @worker = @context.socket(ZMQ::DEALER)
31
+ @worker.connect(@broker)
32
+ @worker.setsockopt(ZMQ::LINGER, 0)
33
+ @poller.register(@worker, ZMQ::POLLIN)
34
+
35
+ send_to_broker(READY, [@service])
36
+ @liveness = HEARTBEAT_LIVENESS
37
+ @heartbeat_at = Time.now + 0.001 * @heartbeat
38
+ end
39
+
40
+ def send_to_broker(command, message = [])
41
+ message.unshift(command)
42
+ message.unshift(WORKER)
43
+ message.unshift('')
44
+
45
+ worker.send_strings(message)
46
+ end
47
+
48
+ def receive_message(reply_to)
49
+ loop do
50
+ items = @poller.poll(@heartbeat)
51
+ if items > 0
52
+ @worker.recv_strings(message = [])
53
+
54
+ @liveness = HEARTBEAT_LIVENESS
55
+ message.shift
56
+ header = message.shift
57
+ raise unless header == WORKER
58
+
59
+ command = message.shift
60
+ case command
61
+ when REQUEST
62
+ reply_to << message.shift
63
+ message.shift
64
+ return message
65
+ when HEARTBEAT
66
+ # Do nothing
67
+ when DISCONNECT
68
+ connect_to_broker
69
+ else
70
+ raise
71
+ end
72
+ elsif (@liveness -= 1) == 0
73
+ sleep(@reconnect*0.001)
74
+ connect_to_broker
75
+ end
76
+
77
+ if Time.now > @heartbeat_at
78
+ send_to_broker(HEARTBEAT)
79
+ end
80
+ end
81
+ end
82
+
83
+ def send_message(report, reply_to)
84
+ report.unshift('')
85
+ report.unshift(reply_to)
86
+ send_to_broker(REPLY, report)
87
+ end
88
+ end
89
+ end
data/librmdp.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'majordomo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'librmdp'
8
+ spec.version = Majordomo::VERSION
9
+ spec.authors = ['Benoist']
10
+ spec.email = %w(benoist.claassen@gmail.com)
11
+ spec.description = %q{A ruby implementation of the ZeroMQ majordomo pattern v0.1}
12
+ spec.summary = %q{A ruby implementation of the ZeroMQ majordomo pattern v0.1}
13
+ spec.homepage = 'https://github.com/benoist/librmdp'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_dependency 'ffi-rzmq'
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class TestClient < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @client = Majordomo::Client.new('tcp://0.0.0.0:5555')
7
+ end
8
+
9
+ def test_connect_to_broker
10
+ @client.connect_to_broker
11
+ assert_kind_of ZMQ::Socket, @client.client
12
+
13
+ old_client = @client.client.dup
14
+ @client.connect_to_broker
15
+ refute_equal old_client, @client.client
16
+ end
17
+
18
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class TestIntegration < MiniTest::Unit::TestCase
4
+ def setup
5
+ @threads = []
6
+ Thread.abort_on_exception = true
7
+ end
8
+
9
+ def start_broker
10
+ @threads << Thread.new do
11
+ Majordomo::Broker.new.mediate
12
+ end
13
+ end
14
+
15
+ def start_worker
16
+ @threads << Thread.new do
17
+ worker = Majordomo::Worker.new('tcp://0.0.0.0:5555', 'echo')
18
+
19
+ request = worker.receive_message(reply_to = '')
20
+ worker.send_message(request, reply_to)
21
+ Thread.current.terminate
22
+ end
23
+ end
24
+
25
+ def close_threads
26
+ @threads.each { |thread| thread.exit }
27
+ @threads = []
28
+ end
29
+
30
+ def test_round_trip
31
+ start_broker
32
+ start_worker
33
+
34
+ client = Majordomo::Client.new('tcp://0.0.0.0:5555')
35
+ client.send_message('echo', 'a')
36
+
37
+ assert_equal %w(a), client.receive_message
38
+
39
+ close_threads
40
+ end
41
+
42
+ end
43
+
44
+
45
+
46
+
47
+
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH << File.expand_path('../../test', __FILE__)
6
+
7
+ require 'minitest/autorun'
8
+ require 'librmdp'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: librmdp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Benoist
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: ffi-rzmq
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: A ruby implementation of the ZeroMQ majordomo pattern v0.1
63
+ email:
64
+ - benoist.claassen@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/librmdp.rb
75
+ - lib/majordomo/broker.rb
76
+ - lib/majordomo/broker/service.rb
77
+ - lib/majordomo/broker/worker.rb
78
+ - lib/majordomo/client.rb
79
+ - lib/majordomo/version.rb
80
+ - lib/majordomo/worker.rb
81
+ - librmdp.gemspec
82
+ - test/client_test.rb
83
+ - test/integration_test.rb
84
+ - test/test_helper.rb
85
+ homepage: https://github.com/benoist/librmdp
86
+ licenses:
87
+ - MIT
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.25
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A ruby implementation of the ZeroMQ majordomo pattern v0.1
110
+ test_files:
111
+ - test/client_test.rb
112
+ - test/integration_test.rb
113
+ - test/test_helper.rb