stomp_publisher 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/lib/stomp_publisher/connection_error.rb +10 -0
- data/lib/stomp_publisher/frame.rb +67 -0
- data/lib/stomp_publisher/header.rb +40 -0
- data/lib/stomp_publisher/version.rb +3 -0
- data/lib/stomp_publisher.rb +70 -0
- data/stomp_publisher.gemspec +25 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a06c015c42df9acf3aa489fd8f83185eaba3fd4
|
4
|
+
data.tar.gz: 125c8db2ccba8513d3d8fe390357f65349756dc0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 25c6767732759e8e8b6896047b9c159aa45b82c6b9fc22f9f540285e36ff169868424f7b453d722ed2b169d7ee1154136c3aacf23fcc9e606b1ccf9ad26a24e7
|
7
|
+
data.tar.gz: d8a71e807a0767a1fe939869fc1ac4d7d18fe9ef43db3ef402b7b8614ae456e53fe3d309a4639f0fbff2efc4df1cc19e0ab329f593b0fb40d6a81b95189ecc2e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brian Abreu
|
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,43 @@
|
|
1
|
+
# StompPublisher
|
2
|
+
|
3
|
+
This is a simple library to synchronously publish STOMP messages. It is designed to be as simple as possible and only supports publishing. It uses non-blocking socket operations to reliably timeout without using hackish thread timeouts.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'stomp_publisher'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install stomp_publisher
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'stomp_publisher'
|
23
|
+
publisher = StompPublisher.new(
|
24
|
+
host: "localhost",
|
25
|
+
port: 61613,
|
26
|
+
login: "guest",
|
27
|
+
passcode: "guest",
|
28
|
+
vhost: "/",
|
29
|
+
connect_timeout: 0.5,
|
30
|
+
read_timeout: 0.25,
|
31
|
+
write_timeout: 0.25
|
32
|
+
)
|
33
|
+
receipt_id = publisher.publish("myqueue", '{ "hello": "world" }', :"content-type" => "application/json")
|
34
|
+
publisher.publish("myqueue", "hello", :"custom-header" => "world", :"receipt_id" => "my custom STOMP receipt")
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class StompPublisher
|
2
|
+
class Frame
|
3
|
+
class InvalidFrame < Exception
|
4
|
+
end
|
5
|
+
|
6
|
+
COMMANDS = %w(
|
7
|
+
SEND
|
8
|
+
SUBSCRIBE
|
9
|
+
UNSUBSCRIBE
|
10
|
+
BEGIN
|
11
|
+
COMMIT
|
12
|
+
ABORT
|
13
|
+
ACK
|
14
|
+
NACK
|
15
|
+
DISCONNECT
|
16
|
+
CONNECT
|
17
|
+
STOMP
|
18
|
+
CONNECTED
|
19
|
+
MESSAGE
|
20
|
+
RECEIPT
|
21
|
+
ERROR
|
22
|
+
)
|
23
|
+
|
24
|
+
attr_accessor :command, :header, :body
|
25
|
+
|
26
|
+
def self.parse(frame)
|
27
|
+
command = COMMANDS.detect { |cmd| frame =~ /^(#{cmd}\r?\n)/ } or
|
28
|
+
raise InvalidFrame.new("invalid or missing command")
|
29
|
+
header_start_ndx = $1.length + 1
|
30
|
+
|
31
|
+
header_separator_ndx = frame.index(/(\r?\n\r?\n)/) or
|
32
|
+
raise InvalidFrame.new("missing headers")
|
33
|
+
body_start_ndx = header_separator_ndx + $1.length
|
34
|
+
|
35
|
+
body_terminator_ndx = frame.rindex(/\0(\r?\n)*/) or
|
36
|
+
raise InvalidFrame.new("missing end of body")
|
37
|
+
|
38
|
+
header = Header.parse(frame[(command.length + 1)..(header_separator_ndx - 1)])
|
39
|
+
body = frame[body_start_ndx..(body_terminator_ndx - 1)]
|
40
|
+
|
41
|
+
if (content_length = header["content-length"])
|
42
|
+
content_length = Integer(content_length) or
|
43
|
+
raise InvalidFrame("invalid content-length")
|
44
|
+
|
45
|
+
body.bytesize == content_length or
|
46
|
+
raise InvalidFrame("content-length was #{body.bytesize}, expected: #{content_length}")
|
47
|
+
end
|
48
|
+
|
49
|
+
Frame.new(command, header, body)
|
50
|
+
end
|
51
|
+
|
52
|
+
def initialize(command, header, body = "")
|
53
|
+
self.command = command
|
54
|
+
self.header = header
|
55
|
+
self.body = body
|
56
|
+
end
|
57
|
+
|
58
|
+
def body=(body)
|
59
|
+
@body = body
|
60
|
+
self.header['content-length'] = body.to_s.bytesize
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_s
|
64
|
+
"#{command}\n#{header}\n\n#{body}\0"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
class StompPublisher
|
4
|
+
class Header
|
5
|
+
include Net::HTTPHeader
|
6
|
+
|
7
|
+
ESCAPES = {
|
8
|
+
"\\" => "\\\\",
|
9
|
+
"\r" => "\\r",
|
10
|
+
"\n" => "\\n",
|
11
|
+
":" => "\\c",
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.parse(header_text)
|
15
|
+
header = self.new
|
16
|
+
header_text.each_line do |line|
|
17
|
+
header.add_field(*line.chomp.split(":", 2))
|
18
|
+
end
|
19
|
+
header
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(headers = {})
|
23
|
+
initialize_http_header(headers)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
each_name.flat_map do |key|
|
28
|
+
get_fields(key).map { |value| "#{encode(key)}:#{encode(value)}" }
|
29
|
+
end * "\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
def encode(value)
|
33
|
+
ESCAPES.inject(value.to_s) { |value, (from, to)| value.gsub(from, to) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def decode(value)
|
37
|
+
ESCAPES.inject(value.to_s) { |value, (from, to)| value.gsub(to, from) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'stomp_publisher/frame'
|
2
|
+
require 'stomp_publisher/header'
|
3
|
+
require 'stomp_publisher/connection_error'
|
4
|
+
|
5
|
+
require 'tcp_timeout'
|
6
|
+
require 'securerandom'
|
7
|
+
|
8
|
+
class StompPublisher
|
9
|
+
FRAME_READ_SIZE = 8192
|
10
|
+
MAX_FRAME_SIZE = 65536
|
11
|
+
|
12
|
+
def initialize(host: "localhost", port: 61613, login: nil, passcode: nil, vhost: host, **socket_args)
|
13
|
+
@host = host
|
14
|
+
@port = port
|
15
|
+
@login = login or raise ArgumentError.new("missing argument login")
|
16
|
+
@passcode = passcode or raise ArgumentError.new("missing argument passcode")
|
17
|
+
@vhost = vhost
|
18
|
+
@socket_args = socket_args
|
19
|
+
end
|
20
|
+
|
21
|
+
def publish(queue, message, **properties)
|
22
|
+
socket = TCPTimeout::TCPSocket.new(@host, @port, **@socket_args)
|
23
|
+
connect(socket, @login, @passcode, @vhost)
|
24
|
+
send(socket, message, properties.merge(destination: "/queue/#{queue}"))
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def connect(socket, login, passcode, vhost)
|
29
|
+
header = Header.new(
|
30
|
+
login: login,
|
31
|
+
passcode: passcode,
|
32
|
+
host: vhost,
|
33
|
+
"accept-version" => "1.2"
|
34
|
+
)
|
35
|
+
frame = Frame.new("CONNECT", header)
|
36
|
+
socket.write(frame.to_s)
|
37
|
+
|
38
|
+
response_frame = read_frame(socket)
|
39
|
+
if (response_frame.command != "CONNECTED")
|
40
|
+
raise ConnectionError.new("Failed to login: #{response_frame.body}", response_frame)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def send(socket, message, receipt_id: SecureRandom.hex(16), **properties)
|
45
|
+
frame = Frame.new("SEND", Header.new(properties.merge(receipt: receipt_id)), message)
|
46
|
+
socket.write(frame.to_s)
|
47
|
+
|
48
|
+
response_frame = read_frame(socket)
|
49
|
+
if (response_frame.command != "RECEIPT")
|
50
|
+
raise ConnectionError.new("Did not receive expected receipt", response_frame)
|
51
|
+
elsif ((response_receipt = response_frame.header["receipt-id"]) != receipt_id)
|
52
|
+
raise ConnectionError.new("Received unexpected receipt id: #{response_receipt}", response_frame)
|
53
|
+
end
|
54
|
+
|
55
|
+
receipt_id
|
56
|
+
end
|
57
|
+
|
58
|
+
def read_frame(socket)
|
59
|
+
response = ""
|
60
|
+
begin
|
61
|
+
response << socket.readpartial(FRAME_READ_SIZE)
|
62
|
+
if (response.bytesize > MAX_FRAME_SIZE)
|
63
|
+
raise ConnectionError.new("Frame was larger than the max size of #{MAX_FRAME_SIZE}", nil)
|
64
|
+
end
|
65
|
+
Frame.parse(response)
|
66
|
+
rescue Frame::InvalidFrame
|
67
|
+
retry
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stomp_publisher/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "stomp_publisher"
|
8
|
+
spec.version = StompPublisher::VERSION
|
9
|
+
spec.authors = ["Brian Abreu"]
|
10
|
+
spec.email = ["brian@nutsonline.com"]
|
11
|
+
spec.description = %q{A simple library for publishing messages via the STOMP protocol}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/nuts/ruby-stomp-publisher"
|
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_runtime_dependency "tcp_timeout"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stomp_publisher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Abreu
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: tcp_timeout
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A simple library for publishing messages via the STOMP protocol
|
56
|
+
email:
|
57
|
+
- brian@nutsonline.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/stomp_publisher.rb
|
68
|
+
- lib/stomp_publisher/connection_error.rb
|
69
|
+
- lib/stomp_publisher/frame.rb
|
70
|
+
- lib/stomp_publisher/header.rb
|
71
|
+
- lib/stomp_publisher/version.rb
|
72
|
+
- stomp_publisher.gemspec
|
73
|
+
homepage: https://github.com/nuts/ruby-stomp-publisher
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A simple library for publishing messages via the STOMP protocol
|
97
|
+
test_files: []
|