1b1b3b5a73c6f8c41d1c8b3cb2affc0d972ac19f
[packages/precise/mcollective.git] / ext / stompclient
1 #!/usr/bin/env ruby
2 # == Synopsis
3 #
4 # stompclient: Generic client to consume and produce STOMP queues and topics, tested against
5 # Apache Active MQ
6 #
7 # == Description
8 # A simple client that can connect to an STOMP server, subscribe to topics and queues and also
9 # send to topics and queues.
10 #
11 # == Usage
12 # stompclient [OPTIONS]
13 #
14 # --help, -h:
15 #   Show Help
16 #
17 # --server, -s
18 #   The server to connect to, can also be set in STOMP_SERVER environment variable
19 #
20 # --port, -p
21 #   The port to connect to, default to 6163
22 #
23 # --user, -u
24 #   The user to connect as, can also be set in STOMP_USER environment variable
25 #
26 # --password, -P
27 #   The password to use, can also be set in STOMP_PASSWORD environment variable
28 #
29 # When connected to a server, use the 'help' command to see further information about
30 # using the client, common commands that can be issued are:
31 #
32 # - subscribe /topic/foo: Subscribes to topic 'foo'
33 # - /topic/foo bar: Sends 'bar' to the topic 'foo'
34 # - details: Toggle the display or timestamp and topic or queue information for each message
35 #
36 #
37 # == Changelog
38 # - 20 December 2009 Include into MCollective
39 # - 17 March 2009 Initial release
40 #
41 # R.I.Pienaar <rip@devco.net> more information at www.devco.net
42 #
43 # Licensed under the Apache License, Version 2.0
44
45 require 'rubygems'
46 require 'stomp'
47 require 'readline'
48 require 'thread'
49 require 'getoptlong'
50
51 opts = GetoptLong.new(
52     [ '--server', '-s', GetoptLong::REQUIRED_ARGUMENT],
53     [ '--port', '-p', GetoptLong::REQUIRED_ARGUMENT],
54     [ '--user', '-u', GetoptLong::REQUIRED_ARGUMENT],
55     [ '--password', '-P', GetoptLong::REQUIRED_ARGUMENT],
56     [ '--help', '-h', GetoptLong::NO_ARGUMENT]
57 )
58
59 @user = ENV["STOMP_USER"];
60 @password = ENV["STOMP_PASSWORD"]
61 @server = ENV["STOMP_SERVER"]
62 @port = ENV["STOMP_PORT"] || 6163
63
64 opts.each { |opt, arg|
65   case opt
66     when '--help'
67       begin
68         require 'rdoc/ri/ri_paths'
69         require 'rdoc/usage'
70         RDoc::usage
71         exit
72       rescue Exception => e
73         puts("Install RDoc::usage or view the comments in the top of the script to get detailed help") if e.to_str != "exit"
74       end
75
76       exit
77     when '--server'
78       @server = arg
79     when '--port'
80       @port = arg
81     when '--user'
82       @user = arg
83     when '--password'
84       @password = arg
85   end
86 }
87
88 @conn = Stomp::Connection.open(@user, @password, @server, @port, true)
89
90 STDOUT.sync = true
91
92 def showhelp
93     puts("List of commands:")
94     puts("\n\t- subscribe /(topic|queue)/foo    subscribes to topic of queue 'foo'")
95     puts("\t- /(topic|queue|/foo bar          sends msg 'bar' to topic of queue 'foo'")
96     puts("\t- quit|exit|q|^d                  exit")
97     puts("\t- detail                          show/dont show time and topic a msg was received on")
98     puts("\t- help                            show this help")
99 end
100
101 @showdetails = true
102
103 Thread.new(@conn) do |amq|
104     while true
105         msg = amq.receive
106         dest = msg.headers["destination"]
107         time = Time.now.strftime('%H:%M:%S')
108
109         if @showdetails
110             msg = "\r#{time}:#{dest} > #{msg.body.chomp}\n"
111         else
112             msg = "\r#{msg.body.chomp}\n"
113         end
114
115         puts (msg)
116     end
117 end
118
119 loop do
120     line = Readline::readline('AMQ> ')
121     if line
122         Readline::HISTORY.push(line) if line != ""
123     else
124         exit
125     end
126
127     if (line =~ /^(\/(topic|queue)\/\S+)\s+(.+)$/)
128         puts("Sending '#{$3}' to #{$1}")
129
130         if @conn.respond_to?("publish")
131             @conn.publish($1, $3)
132         else
133             @conn.send($1, $3)
134         end
135
136     elsif (line =~ /^sub\S* (\/(topic|queue)\/\S+)$/)
137         puts("Subscribing to #{$1}")
138
139         @conn.subscribe($1)
140     elsif (line =~ /^det(ail)*$/)
141         if @showdetails
142             @showdetails = false
143             puts("No longer showing details")
144         else
145             @showdetails = true
146             puts("Showing time and topic for each msg")
147         end
148     elsif (line =~ /^(quit|exit|q)$/)
149         exit
150     elsif (line =~ /^(help|h|\?)$/)
151         showhelp
152     elsif (line =~ /^$/)
153     else
154         puts("ERROR: unrecognised input: #{line}")
155     end
156 end