Debian Codegraph @

ruby/examples/direct_send.rb

raw
1 #--
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #++
19
20 require 'qpid_proton'
21 require 'optparse'
22
23 class DirectSend < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address, expected)
26 super()
27 @url = url
28 @address = address
29 @sent = 0
30 @confirmed = 0
31 @expected = expected
32 end
33
34 class ListenOnce < Qpid::Proton::Listener::Handler
35 def on_open(l) STDOUT.puts "Listening on #{l.port}\n"; STDOUT.flush; end
36 def on_accept(l) l.close; end
37 end
38
39 def on_container_start(container)
40 container.listen(@url, ListenOnce.new)
41 end
42
43 def on_sendable(sender)
44 while sender.credit > 0 && @sent < @expected
45 msg = Qpid::Proton::Message.new("sequence #{@sent}", { :id => @sent } )
46 sender.send(msg)
47 @sent = @sent + 1
48 end
49 end
50
51 def on_tracker_accept(tracker)
52 @confirmed = @confirmed + 1
53 if @confirmed == @expected
54 puts "All #{@expected} messages confirmed!"
55 tracker.connection.close
56 end
57 end
58 end
59
60 unless (2..3).include? ARGV.size
61 STDERR.puts "Usage: #{__FILE__} URL ADDRESS [COUNT]
62 Listen on URL and send COUNT messages to ADDRESS"
63 return 1
64 end
65 url, address, count = ARGV
66 count = Integer(count || 10)
67 Qpid::Proton::Container.new(DirectSend.new(url, address, count)).run