Debian Codegraph @

ruby/examples/server.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 Server < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address)
26 super()
27 @url = url
28 @address = address
29 @senders = {}
30 end
31
32 def on_container_start(container)
33 c = container.connect(@url)
34 c.open_receiver(@address)
35 @relay = nil
36 end
37
38 def on_connection_open(connection)
39 if connection.offered_capabilities &&
40 connection.offered_capabilities.include?(:"ANONYMOUS-RELAY")
41 @relay = connection.open_sender({:target => nil})
42 end
43 end
44
45 def on_message(delivery, message)
46 return unless message.reply_to # Not a request message
47 puts "<- #{message.body}"
48 unless (sender = @relay)
49 sender = (@senders[message.reply_to] ||= delivery.connection.open_sender(message.reply_to))
50 end
51 reply = Qpid::Proton::Message.new
52 reply.address = message.reply_to
53 reply.body = message.body.upcase
54 puts "-> #{reply.body}"
55 reply.correlation_id = message.correlation_id
56 sender.send(reply)
57 end
58
59 def on_transport_error(transport)
60 raise "Connection error: #{transport.condition}"
61 end
62 end
63
64 if ARGV.size != 2
65 STDERR.puts "Usage: #{__FILE__} URL ADDRESS
66 Server listening on URL, reply to messages to ADDRESS"
67 return 1
68 end
69 url, address = ARGV
70 Qpid::Proton::Container.new(Server.new(url, address)).run