Debian Codegraph @

ruby/examples/direct_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 DirectReceive < 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 class ListenOnce < Qpid::Proton::Listener::Handler
34 def on_open(l) STDOUT.puts "Listening on #{l.port}\n"; STDOUT.flush; end
35 def on_accept(l) l.close; end
36 end
37
38 def on_container_start(container)
39 container.listen(@url, ListenOnce.new)
40 end
41
42 def on_message(delivery, message)
43 if @expected.zero? || (@received < @expected)
44 puts "Received: #{message.body}"
45 @received = @received + 1
46 if @received == @expected
47 delivery.connection.close
48 end
49 end
50 end
51 end
52
53 unless (2..3).include? ARGV.size
54 STDERR.puts "Usage: #{__FILE__} URL ADDRESS [COUNT]
55 Listen on URL and receive COUNT messages from ADDRESS"
56 return 1
57 end
58 url, address, count = ARGV
59 count = Integer(count || 10)
60 Qpid::Proton::Container.new(DirectReceive.new(url, address, count)).run
61