Debian Codegraph @

ruby/examples/simple_recv.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 SimpleReceive < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address, count)
26 super()
27 @url = url
28 @address = address
29 @expected = count
30 @received = 0
31 end
32
33 def on_container_start(container)
34 c = container.connect(@url)
35 c.open_receiver(@address)
36 end
37
38 def on_message(delivery, message)
39 if @expected.zero? || (@received < @expected)
40 puts "Received: #{message.body}"
41 @received = @received + 1
42 if @received == @expected
43 delivery.connection.close
44 end
45 end
46 end
47 end
48
49 unless (2..3).include? ARGV.size
50 STDERR.puts "Usage: #{__FILE__} URL ADDRESS [COUNT]}
51 Connect to URL and receive COUNT messages from ADDRESS"
52 return 1
53 end
54 url, address, count = ARGV
55 count = Integer(count || 10)
56 Qpid::Proton::Container.new(SimpleReceive.new(url, address, count)).run
57