Debian Codegraph @

ruby/examples/simple_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 SimpleSend < 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 def on_container_start(container)
35 c = container.connect(@url)
36 c.open_sender(@address)
37 end
38
39 def on_sendable(sender)
40 while sender.credit > 0 && @sent < @expected
41 msg = Qpid::Proton::Message.new("sequence #{@sent}", { :id => @sent } )
42 sender.send(msg)
43 @sent = @sent + 1
44 end
45 end
46
47 def on_tracker_accept(tracker)
48 @confirmed = @confirmed + 1
49 if @confirmed == @expected
50 puts "All #{@expected} messages confirmed!"
51 tracker.connection.close
52 end
53 end
54 end
55
56 unless (2..3).include? ARGV.size
57 STDERR.puts "Usage: #{__FILE__} URL ADDRESS [COUNT]}
58 Connect to URL and send COUNT messages to ADDRESS"
59 return 1
60 end
61 url, address, count = ARGV
62 count = Integer(count || 10)
63 Qpid::Proton::Container.new(SimpleSend.new(url, address, count)).run