romp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 ninjudd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,37 @@
1
+ = romp
2
+
3
+ Romp is a lightweight {Streaming Text Oriented Messaging Protocol}[http://stomp.codehaus.org/Protocol]
4
+ for Ruby. Stomp is a text-based wire format that lets clients communicate through a
5
+ variety Stomp message brokers, providing interoperable, cross-language messaging.
6
+
7
+ == Getting started
8
+
9
+ Install a {stomp broker}[http://stomp.codehaus.org/Brokers] and start it up. Both
10
+ {StompServer}[http://stompserver.rubyforge.org] and {CoilMQ}[http://code.google.com/p/coilmq]
11
+ don't seem to handle `ack:auto` correctly, so I've been testing with {HornetQ}[http://jboss.org/hornetq].
12
+
13
+ git clone git://github.com/jmesnil/hornetq-stomp.git
14
+ cd hornetq-stomp
15
+ ant server
16
+
17
+ == Usage
18
+
19
+ require 'romp'
20
+
21
+ r1 = Romp.new("localhost", 61613)
22
+ r2 = Romp.new("localhost", 61613)
23
+
24
+ r1.with_connection(:login => "foo", :password => "password") do
25
+ r1.send("foo", :destination => "/queue/a")
26
+ r1.send("bar", :destination => "/queue/a")
27
+ end
28
+
29
+ r2.with_connection(:login => "bar", :password => "secret") do
30
+ r2.subscribe(:destination => "/queue/a")
31
+ puts r2.receive.body # => foo
32
+ puts r2.receive.body # => bar
33
+ end
34
+
35
+ == Copyright
36
+
37
+ Copyright (c) 2010 Justin Balthrop. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "romp"
8
+ gem.summary = %Q{A Stomp client library for Ruby}
9
+ gem.description = %Q{Romp is a lightweight Streaming Text Oriented Messaging Protocol for Ruby}
10
+ gem.email = "git@justinbalthrop.com"
11
+ gem.homepage = "http://github.com/ninjudd/romp"
12
+ gem.authors = ["ninjudd"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "romp #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/romp.rb ADDED
@@ -0,0 +1,88 @@
1
+ require 'socket'
2
+ require 'pp'
3
+
4
+ class Romp
5
+ class Frame
6
+ attr_reader :type, :headers, :body
7
+ def initialize(type, headers, body)
8
+ @type = type.freeze
9
+ @headers = headers.freeze
10
+ @body = body.freeze
11
+ end
12
+ end
13
+
14
+ def initialize(*args)
15
+ if args.size == 1
16
+ @io = args.first
17
+ else
18
+ @io = TCPSocket.new(*args)
19
+ end
20
+ end
21
+
22
+ def close
23
+ @io.close
24
+ end
25
+
26
+ [:subscribe, :unsubscribe, :begin, :commit, :abort, :ack].each do |command|
27
+ define_method(command) do |*args|
28
+ headers = args.first || {}
29
+ send_frame(command, headers)
30
+ end
31
+ end
32
+
33
+ def send(body, headers = {})
34
+ send_frame(:send, headers, body)
35
+ end
36
+
37
+ def with_connection(headers = {})
38
+ send_frame(:connect, headers)
39
+
40
+ begin
41
+ yield receive(:connected)
42
+ ensure
43
+ send_frame(:disconnect)
44
+ end
45
+ end
46
+
47
+ def receive(expected_type = nil)
48
+ type = @io.gets.chomp.downcase.to_sym
49
+ headers = {}
50
+ while line = @io.gets
51
+ key, val = line.chomp.split(':')
52
+ break unless val
53
+ headers[key.to_sym] = val
54
+ end
55
+
56
+ if len = headers[:content_length]
57
+ body = @io.read(len)
58
+ assert_read("\0")
59
+ else
60
+ body = ''
61
+ while c = @io.read(1)
62
+ break if c == "\0"
63
+ body << c
64
+ end
65
+ end
66
+ assert_read("\n")
67
+ raise "expected #{expected_type} frame, got #{type}\n#{headers[:message]}\n#{body}" if expected_type and type != expected_type
68
+ Frame.new(type, headers, body)
69
+ end
70
+
71
+ private
72
+
73
+ def assert_read(expected)
74
+ string = @io.read(expected.size)
75
+ raise "expected #{expected.inspect} got #{string.inspect}" unless string == expected
76
+ end
77
+
78
+ def send_frame(command, headers = {}, body = '')
79
+ @io.puts(command.to_s.upcase)
80
+ headers.each do |key, val|
81
+ @io.puts("#{key}:#{val}")
82
+ end
83
+ @io.puts("content-length:#{body.size}")
84
+ @io.puts
85
+ @io.write(body)
86
+ @io.puts("\0")
87
+ end
88
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'romp'
8
+
9
+ class Test::Unit::TestCase
10
+ end
data/test/test_romp.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'helper'
2
+
3
+ class TestRomp < Test::Unit::TestCase
4
+ should "romp" do
5
+ r = Romp.new("localhost", 61613)
6
+ r.with_connection(:login => "foo", :password => "password") do
7
+ r.subscribe(:destination => "/queue/a")
8
+ r.send("foo", :destination => "/queue/a")
9
+ r.send("bar", :destination => "/queue/a")
10
+ r.send("baz", :destination => "/queue/a")
11
+
12
+ frame = r.receive(:message)
13
+ assert_equal "foo", frame.body
14
+
15
+ frame = r.receive(:message)
16
+ assert_equal "bar", frame.body
17
+
18
+ frame = r.receive(:message)
19
+ assert_equal "baz", frame.body
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: romp
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - ninjudd
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-07 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Romp is a lightweight Streaming Text Oriented Messaging Protocol for Ruby
23
+ email: git@justinbalthrop.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README.rdoc
31
+ files:
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/romp.rb
38
+ - test/helper.rb
39
+ - test/test_romp.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/ninjudd/romp
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A Stomp client library for Ruby
74
+ test_files:
75
+ - test/helper.rb
76
+ - test/test_romp.rb