Debian Codegraph @

ruby/examples/helloworld.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 HelloWorld < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address)
26 super()
27 @url, @address = url, address
28 end
29
30 def on_container_start(container)
31 conn = container.connect(@url)
32 conn.open_sender(@address)
33 conn.open_receiver(@address)
34 end
35
36 def on_sendable(sender)
37 sender.send(Qpid::Proton::Message.new("Hello world!"))
38 sender.close
39 end
40
41 def on_message(delivery, message)
42 puts message.body
43 delivery.connection.close
44 end
45
46 def on_transport_error(transport)
47 raise "Connection error: #{transport.condition}"
48 end
49 end
50
51 if ARGV.size != 2
52 STDERR.puts "Usage: #{__FILE__} URL ADDRESS
53 Connect to URL, send a message to ADDRESS and receive it back"
54 return 1
55 end
56 url, address = ARGV
57 Qpid::Proton::Container.new(HelloWorld.new(url, address)).run