pigato 0.1.1 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59fe6ec27b63289331f1bcc8dfe5bd56a273861e
4
- data.tar.gz: d196156fb32ed9b50337a1e6b7d2af5d2aaaa5f2
3
+ metadata.gz: 084cf3ce0c185586f92f4801285553e68e23085d
4
+ data.tar.gz: bdffbb4efc8e86d3ffb2ed669fade66987c4e8a9
5
5
  SHA512:
6
- metadata.gz: c9c083863c48629c7b74f878af92f9b81d5a5aba14f9c585293a53cad91208baec1db9cc6d9064f20c05db2465ff9d2af0d67dea2e4e5c57eaba03238aceb3c0
7
- data.tar.gz: 7a6065cba43176980abb835ee529e4199e82e38ea89628cf995c29587aaab01c10b9fa3385dd7b689d7e828df96730e7fe62da2c49389a9cc3ad5ca7a7c65d42
6
+ metadata.gz: b6a2f6ccb48abb66956e8695079b07e53ca372b7ecb12ae6558935c5e3d10c4dda831a6035b0d055d401199bfec1ea894b84c0d1d8cd11db37896914437e1375
7
+ data.tar.gz: 427d7dec7fe448f198a72f3c2c0eff322bf9729dd30c5d8724e3bd83d12fb1bb8a81b38bdaf5cf55305c78f2fa4cb8fc5451b68c96ab17bb65761e48ef4e882b
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "#{File.dirname(__FILE__)}/../lib/pigato.rb"
5
+
6
+ worker = PigatoWorker.new('tcp://localhost:55555', 'echo')
7
+ reply = nil
8
+
9
+ loop do
10
+ request = worker.recv reply
11
+ worker.reply request
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Pigato
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -0,0 +1,111 @@
1
+ require "json"
2
+ require "ffi-rzmq"
3
+
4
+ class PigatoWorker
5
+ HEARTBEAT_LIVENESS = 3 # 3-5 is reasonable
6
+
7
+ def initialize broker, service
8
+ @broker = broker
9
+ @service = service
10
+ @context = ZMQ::Context.new(1)
11
+ @poller = ZMQ::Poller.new
12
+ @worker = nil # Socket to broker
13
+ @heartbeat_at = 0 # When to send HEARTBEAT (relative to time.time(), so in seconds)
14
+ @liveness = 0 # How many attempts left
15
+ @timeout = 2500
16
+ @heartbeat = 2500 # Heartbeat delay, msecs
17
+ @reconnect = 2500 # Reconnect delay, msecs
18
+
19
+ @reply_to = nil
20
+ @reply_rid = nil
21
+ @reply_service = nil
22
+
23
+ reconnect_to_broker
24
+ end
25
+
26
+ def reply reply
27
+ reply = [@reply_to, '', @reply_rid, '0'].concat([reply.to_json])
28
+ send_to_broker Pigato::W_REPLY, reply, nil
29
+ end
30
+
31
+ def recv reply
32
+ loop do
33
+ @reply_rid = nil
34
+ @reply_to = nil
35
+ @reply_service = nil
36
+
37
+ items = @poller.poll(@timeout)
38
+ if items
39
+ messages = []
40
+ @worker.recv_strings messages
41
+
42
+ @liveness = HEARTBEAT_LIVENESS
43
+
44
+ header = messages.shift
45
+ if header != Pigato::W_WORKER
46
+ puts "E: Header is not Pigato::WORKER"
47
+ end
48
+
49
+ command = messages.shift
50
+
51
+ case command
52
+ when Pigato::W_REQUEST
53
+ # We should pop and save as many addresses as there are
54
+ # up to a null part, but for now, just save one...
55
+ puts "REQUEST"
56
+ @reply_to = messages.shift
57
+ @reply_service = messages.shift
58
+ messages.shift # empty
59
+ @reply_rid = messages.shift
60
+ return messages[0] # We have a request to process
61
+ when Pigato::W_HEARTBEAT
62
+ # do nothing
63
+ when Pigato::W_DISCONNECT
64
+ reconnect_to_broker
65
+ else
66
+ end
67
+ else
68
+ @liveness -= 1
69
+ if @liveness == 0
70
+ sleep 0.001*@reconnect
71
+ reconnect_to_broker
72
+ end
73
+ end
74
+
75
+ if Time.now > @heartbeat_at
76
+ send_to_broker Pigato::W_HEARTBEAT
77
+ @heartbeat_at = Time.now + 0.001 * @heartbeat
78
+ end
79
+
80
+ end
81
+ end
82
+
83
+ def reconnect_to_broker
84
+ if @worker
85
+ @poller.deregister @worker, ZMQ::DEALER
86
+ @worker.close
87
+ end
88
+
89
+ @worker = @context.socket ZMQ::DEALER
90
+ @worker.setsockopt ZMQ::IDENTITY, 'WRK01'
91
+ @worker.setsockopt ZMQ::LINGER, 0
92
+ @worker.connect @broker
93
+ @poller.register @worker, ZMQ::POLLIN
94
+ send_to_broker(Pigato::W_READY, @service, [])
95
+ @liveness = HEARTBEAT_LIVENESS
96
+ @heartbeat_at = Time.now + 0.001 * @heartbeat
97
+ end
98
+
99
+ def send_to_broker command, message=nil, options=nil
100
+ if message.nil?
101
+ message = []
102
+ elsif not message.is_a?(Array)
103
+ message = [message]
104
+ end
105
+
106
+ message = [Pigato::W_WORKER, command].concat message
107
+ message = message.concat(options) if options
108
+
109
+ @worker.send_strings message
110
+ end
111
+ end
data/lib/pigato.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require_relative "pigato/version.rb"
2
2
  require_relative "pigato/proto.rb"
3
3
  require_relative "pigato/client.rb"
4
+ require_relative "pigato/worker.rb"
data/pigato.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.8"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_runtime_dependency "ffi-rzmq", "~> 2.0"
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pigato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Ardoino
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ffi-rzmq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
41
55
  description: PIGATO-RUBY
42
56
  email:
43
57
  - paolo.ardoino@gmail.com
@@ -53,11 +67,13 @@ files:
53
67
  - Rakefile
54
68
  - bin/console
55
69
  - bin/setup
56
- - examples/client.rb
70
+ - examples/echo_client.rb
71
+ - examples/echo_worker.rb
57
72
  - lib/pigato.rb
58
73
  - lib/pigato/client.rb
59
74
  - lib/pigato/proto.rb
60
75
  - lib/pigato/version.rb
76
+ - lib/pigato/worker.rb
61
77
  - pigato.gemspec
62
78
  homepage: https://github.com/prdn/pigato
63
79
  licenses:
File without changes