Debian Codegraph @

ruby/examples/client.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 Client < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address, requests)
26 super()
27 @url = url
28 @address = address
29 @requests = requests
30 end
31
32 def on_container_start(container)
33 c = container.connect(@url)
34 @sender = c.open_sender(@address)
35 @receiver = c.open_receiver({:dynamic => true})
36 end
37
38 def next_request
39 if @receiver.remote_source.address
40 req = Qpid::Proton::Message.new
41 req.reply_to = @receiver.remote_source.address
42 req.body = @requests.first
43 puts "-> #{req.body}"
44 @sender.send(req)
45 end
46 end
47
48 def on_receiver_open(receiver)
49 next_request
50 end
51
52 def on_message(delivery, message)
53 puts "<- #{message.body}"
54 @requests.delete_at(0)
55 if [email protected]?
56 next_request
57 else
58 delivery.connection.close
59 end
60 end
61
62 def on_transport_error(transport)
63 raise "Connection error: #{transport.condition}"
64 end
65
66 end
67
68 REQUESTS = ["Twas brillig, and the slithy toves",
69 "Did gire and gymble in the wabe.",
70 "All mimsy were the borogroves,",
71 "And the mome raths outgrabe."]
72
73 if ARGV.size != 2
74 STDERR.puts "Usage: #{__FILE__} URL ADDRESS
75 Connect to URL and send messages to ADDRESS"
76 return 1
77 end
78 url, address = ARGV
79 Qpid::Proton::Container.new(Client.new(url, address, REQUESTS)).run