haredo 0.0.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 +7 -0
- data/README.md +93 -0
- data/src/lib/haredo/peer.rb +119 -0
- data/src/lib/haredo/version.rb +5 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1585745834668ad5a1db7ceade69f55cad742453
|
4
|
+
data.tar.gz: adf26761a2a995d47f3e3d7d5c11453fc78b65cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 20b36a13ae81dc1c33636d786da3c3248e3071faf9d5a55cede56b9c63ba7bf9165be0d2f13d0cc2e3d32126c2f6c5cb53e6109eb41dd74b9195003cbc895141
|
7
|
+
data.tar.gz: dbe8eff27818b3ef38fa3687bfc20df928ba4e2e88edc85c7c789c0eeef4b7e8d1ec8b34cf03315c8d806ae8eb43c261943df9134b51d6ac31463c11f04c4d1b
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# HareDo
|
2
|
+
|
3
|
+
## About
|
4
|
+
|
5
|
+
An easy-to-use framework for quickly creating client/server applications in Ruby
|
6
|
+
over RabbitMQ. The RabbitMQ terminology uses metaophors of publishers and
|
7
|
+
consumers. This framework -- which is a very thin wrapper on top of
|
8
|
+
[Bunny](http://rubybunny.info/) -- slightly modifies the semantics and provides
|
9
|
+
an intuitive framework for easily and quickly implementing network services and
|
10
|
+
clients to use them.
|
11
|
+
|
12
|
+
## How It Works
|
13
|
+
|
14
|
+
Here is a complete example -- the following plugin/program (included in
|
15
|
+
<tt>src/examples/service.rb</tt>) create a simple network service that takes a
|
16
|
+
number and add one, along with a client that uses it:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
#!/usr/bin/env ruby
|
20
|
+
|
21
|
+
# Simple service
|
22
|
+
class Service < HareDo::Service
|
23
|
+
|
24
|
+
def initialize(name)
|
25
|
+
super
|
26
|
+
connect($rabbitmq_username, $rabbitmq_password, $rabbitmq_host)
|
27
|
+
end
|
28
|
+
|
29
|
+
def serve(msg)
|
30
|
+
headers = msg.properties.headers
|
31
|
+
reply_to = msg.properties.reply_to
|
32
|
+
|
33
|
+
data = headers['i'].to_i + 1
|
34
|
+
send(reply_to, data.to_s, { :rc => 1 })
|
35
|
+
end
|
36
|
+
|
37
|
+
end # class Server
|
38
|
+
|
39
|
+
def dump_message(msg)
|
40
|
+
puts 'Headers:'
|
41
|
+
msg.properties.each do |k,v|
|
42
|
+
puts " #{k}: #{v}"
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "Data: #{msg.data}"
|
46
|
+
end
|
47
|
+
|
48
|
+
client = HareDo::Client.new()
|
49
|
+
client.connect($rabbitmq_username, $rabbitmq_password, $rabbitmq_host)
|
50
|
+
service = Service.new($haredo_test_queue)
|
51
|
+
|
52
|
+
service.run(false)
|
53
|
+
|
54
|
+
1.upto(10) do |i|
|
55
|
+
client.send($haredo_test_queue, 'data', { :i => i })
|
56
|
+
msg = @client.receive()
|
57
|
+
|
58
|
+
dump_message msg
|
59
|
+
|
60
|
+
puts msg.data.to_i == i + 1
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
The server runs in non-blocking mode. The client makes 10 calls sending 10
|
65
|
+
monotonically increasing integer values, waits for the response (using a
|
66
|
+
blocking timeout) after each call and checks the results.
|
67
|
+
|
68
|
+
## Installation
|
69
|
+
|
70
|
+
You can install directly from the command line via Ruby gems as follows:
|
71
|
+
|
72
|
+
gem install haredo
|
73
|
+
|
74
|
+
## Building from Source
|
75
|
+
|
76
|
+
This project uses CMake. To build the gem:
|
77
|
+
|
78
|
+
cmake .
|
79
|
+
make gem
|
80
|
+
|
81
|
+
The resultant gem will be generated in the <tt>pkg</tt> directory.
|
82
|
+
|
83
|
+
To build on Debian/Ubuntu:
|
84
|
+
|
85
|
+
fakeroot debian/rules clean
|
86
|
+
dpkg-buildpackage -b -uc -us
|
87
|
+
|
88
|
+
The resulting binary package will be created in the parent directory.
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
Copyright information is located in the COPYING file. The software license is
|
93
|
+
located in the LICENSE file.
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "bunny"
|
2
|
+
require 'haredo/version'
|
3
|
+
|
4
|
+
module HareDo
|
5
|
+
|
6
|
+
class Message
|
7
|
+
|
8
|
+
attr_reader :info, :properties, :data
|
9
|
+
|
10
|
+
def initialize(info=nil, properties=nil, data=nil)
|
11
|
+
@info = info
|
12
|
+
@properties = properties
|
13
|
+
@data = data
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
class Node
|
19
|
+
|
20
|
+
attr_reader :queue
|
21
|
+
|
22
|
+
def initialize()
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
def connect(user, password, host='localhost', port='5672')
|
27
|
+
@cnx = Bunny.new("amqp://#{user}:#{password}@#{host}:#{port}")
|
28
|
+
|
29
|
+
@cnx.start()
|
30
|
+
|
31
|
+
@channel = @cnx.create_channel()
|
32
|
+
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
36
|
+
def disconnect()
|
37
|
+
@cnx.close()
|
38
|
+
end
|
39
|
+
|
40
|
+
def send(to, data, headers = {}, from=nil)
|
41
|
+
|
42
|
+
if from.nil?
|
43
|
+
from = @queue.name if @queue
|
44
|
+
end
|
45
|
+
|
46
|
+
@exchange.publish( data,
|
47
|
+
:routing_key => to,
|
48
|
+
:headers => headers,
|
49
|
+
:reply_to => from )
|
50
|
+
end
|
51
|
+
|
52
|
+
end # class Node
|
53
|
+
|
54
|
+
class Client < Node
|
55
|
+
|
56
|
+
attr_reader :queue
|
57
|
+
|
58
|
+
def initialize()
|
59
|
+
super
|
60
|
+
end
|
61
|
+
|
62
|
+
def connect(user, password, host='localhost', port='5672')
|
63
|
+
super
|
64
|
+
|
65
|
+
@queue = @channel.queue( '', :auto_delete => true,
|
66
|
+
:arguments => { "x-message-ttl" => 1000 } )
|
67
|
+
|
68
|
+
@exchange = @channel.default_exchange()
|
69
|
+
end
|
70
|
+
|
71
|
+
def disconnect()
|
72
|
+
@queue.delete() if @queue
|
73
|
+
super
|
74
|
+
end
|
75
|
+
|
76
|
+
def receive(timeout = 1.0)
|
77
|
+
now = Time::now.to_f
|
78
|
+
|
79
|
+
while true
|
80
|
+
delivery_info, properties, payload = @queue.pop()
|
81
|
+
|
82
|
+
if delivery_info != nil
|
83
|
+
return Message.new(delivery_info, properties, payload)
|
84
|
+
end
|
85
|
+
|
86
|
+
if (Time::now.to_f - now) > timeout
|
87
|
+
return Message.new()
|
88
|
+
end
|
89
|
+
|
90
|
+
sleep 0.001
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end # class Client
|
95
|
+
|
96
|
+
class Service < Node
|
97
|
+
|
98
|
+
def initialize(name)
|
99
|
+
super()
|
100
|
+
|
101
|
+
@queue_name = name
|
102
|
+
end
|
103
|
+
|
104
|
+
def createQueue()
|
105
|
+
return @channel.queue(@queue_name, :auto_delete => true, :exclusive => true)
|
106
|
+
end
|
107
|
+
|
108
|
+
def run(block = true)
|
109
|
+
@queue = createQueue()
|
110
|
+
@exchange = @channel.default_exchange()
|
111
|
+
|
112
|
+
queue.subscribe(:block => block) do |info, props, data|
|
113
|
+
serve Message.new(info, props, data)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end # class Service
|
118
|
+
|
119
|
+
end # module HareDo
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: haredo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Owens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: mikeowens@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files:
|
18
|
+
- README.md
|
19
|
+
files:
|
20
|
+
- src/lib/haredo/peer.rb
|
21
|
+
- src/lib/haredo/version.rb
|
22
|
+
- README.md
|
23
|
+
homepage: https://github.com/mikeowens/haredo
|
24
|
+
licenses: []
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- src/lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.0.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: A simple client/server framework using RabbitMQ
|
46
|
+
test_files: []
|