Permalink
Browse files

Merge pull request #31 from stefanschneider/master

added nats-request for executables
  • Loading branch information...
2 parents 87106f2 + 4492c1b commit 4fe54b0352ea8912084d39ca68a76d6ef642b729 @derekcollison derekcollison committed Jan 30, 2012
Showing with 43 additions and 1 deletion.
  1. +41 −0 bin/nats-request
  2. +2 −1 nats.gemspec
View
41 bin/nats-request
@@ -0,0 +1,41 @@
+#!/usr/bin/env ruby
+
+require 'optparse'
+require 'rubygems'
+require 'nats/client'
+
+['TERM', 'INT'].each { |s| trap(s) { puts; exit! } }
+
+def usage
+ puts "Usage: nats-request <subject> <msg> [-s server] [-t] [-n responses]"; exit
+end
+
+args = ARGV.dup
+opts_parser = OptionParser.new do |opts|
+ opts.on('-s SERVER') { |server| $nats_server = server }
+ opts.on('-t') { $show_time = true }
+ opts.on('-n RESPONSES') { |responses| $responses = Integer(responses) if Integer(responses) > 0 }
+end
+args = opts_parser.parse!(args)
+
+subject, msg = args
+usage unless subject
+msg ||= 'Hello World'
+
+def time_prefix
+ "[#{Time.now}] " if $show_time
+end
+
+def header
+ $i=0 unless $i
+ "#{time_prefix}[\##{$i+=1}]"
+end
+
+NATS.on_error { |err| puts "Server Error: #{err}"; exit! }
+
+NATS.start(:uri => $nats_server, :autostart => true) do
+ NATS.request(subject, msg) { |(msg, reply)|
+ puts "#{header} Replied with : '#{msg}'"
+ exit! if $responses && ($responses-=1) < 1
+ }
+end
View
3 nats.gemspec
@@ -23,7 +23,7 @@ spec = Gem::Specification.new do |s|
s.require_paths = ['lib']
s.bindir = 'bin'
- s.executables = [NATSD::APP_NAME, 'nats-pub', 'nats-sub', 'nats-queue', 'nats-top']
+ s.executables = [NATSD::APP_NAME, 'nats-pub', 'nats-sub', 'nats-queue', 'nats-top', 'nats-request']
s.files = %w[
COPYING
@@ -36,6 +36,7 @@ spec = Gem::Specification.new do |s|
bin/nats-pub
bin/nats-queue
bin/nats-top
+ bin/nats-request
lib/nats/client.rb
lib/nats/ext/bytesize.rb
lib/nats/ext/em.rb

0 comments on commit 4fe54b0

Please sign in to comment.