Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up examples and preliminary publisher
- Loading branch information
Showing
14 changed files
with
117 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
Changelog | ||
===================== | ||
|
||
0.0.2 | ||
----- | ||
|
||
- Fix timestamp | ||
- More documentation | ||
|
||
0.0.1 | ||
----- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Start up reader | ||
bundle exec ./reader.rb | ||
Hit <RETURN> to initialize the Reader and <RETURN> again when you want to stop the reader | ||
|
||
# Run publisher with no arguments to get usage info: | ||
bundle exec ./publisher.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'nsq' | ||
|
||
if ARGV.length != 3 | ||
$stderr.puts "bundle exec ./publisher.rb <topic> <count> <eval-string>" | ||
$stderr.puts " where <topic> is either test_xy or test_z" | ||
$stderr.puts " and <eval-string> could be something like 'sleep rand(100)/10.0'" | ||
$stderr.puts " Example: bundle exec ./publisher.rb test_xy 500 'sleep rand(100)/10.0'" | ||
$stderr.puts " or: bundle exec ./publisher.rb test_z 5000 nil" | ||
exit 1 | ||
end | ||
topic = ARGV[0] | ||
count = ARGV[1].to_i | ||
eval_string = ARGV[2] | ||
|
||
NSQ::Publisher.new('localhost', 4150) do |publisher| | ||
count.times do | ||
publisher.publish(topic, eval_string) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Start up reader | ||
bundle exec ./reader.rb | ||
|
||
# From another terminal, send a message: | ||
curl -d 'hello world' 'http://127.0.0.1:4151/put?topic=test' | ||
bundle exec ./publisher.rb hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'nsq' | ||
|
||
if ARGV.length == 0 | ||
$stderr.puts "bundle exec ./publisher.rb <message>*" | ||
$stderr.puts " Example: bundle exec ./publisher.rb hello world" | ||
exit 1 | ||
end | ||
|
||
NSQ::Publisher.new('localhost', 4150) do |publisher| | ||
publisher.publish('test', ARGV.join(' ')) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'socket' | ||
|
||
module NSQ | ||
class Publisher | ||
def initialize(host, port, options={}, &block) | ||
@socket = TCPSocket.open(host, port) | ||
@socket.write(MAGIC_V2) | ||
@response_timeout = options[:response_timeout] || 5 | ||
yield self if block_given? | ||
ensure | ||
close if block_given? | ||
end | ||
|
||
def publish(topic, message) | ||
buf = ['PUB ', topic, "\n", message.length, message].pack('a*a*a*Na*') | ||
@socket.write(buf) | ||
response = '' | ||
loop do | ||
response += @socket.recv(4096) | ||
size, frame, msg = response.unpack('NNa*') | ||
if response.length == size+4 | ||
case msg | ||
when 'OK' then return | ||
when 'E_INVALID' then raise 'Invalid message' | ||
when 'E_BAD_TOPIC' then raise 'Bad topic' | ||
when 'E_BAD_MESSAGE' then raise 'Bad message' | ||
when 'E_PUT_FAILED' then raise 'Put failed' | ||
else raise "Unknown PUB response: #{msg}" | ||
end | ||
elsif response.length > size+4 | ||
raise "Unexpected PUB response - Expected size = #{size} actual size = #{response.length-4}: message=#{msg}" | ||
end | ||
end | ||
end | ||
|
||
def close | ||
@socket.close | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module NSQ | ||
module Util | ||
|
||
def self.assert_topic_and_channel_valid(topic, channel) #:nodoc: | ||
raise "Invalid topic #{topic}" unless valid_topic_name?(topic) | ||
raise "Invalid channel #{channel}" unless valid_channel_name?(channel) | ||
end | ||
|
||
def self.valid_topic_name?(topic) #:nodoc: | ||
!!topic.match(/^[\.a-zA-Z0-9_-]+$/) | ||
end | ||
|
||
def self.valid_channel_name?(channel) #:nodoc: | ||
!!channel.match(/^[\.a-zA-Z0-9_-]+(#ephemeral)?$/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters