Debian Codegraph @

ruby/examples/ssl_send.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 SimpleSend < Qpid::Proton::MessagingHandler
24
25 def initialize(url, address, expected)
26 super()
27 @url = url
28 @address = address
29 @sent = 0
30 @confirmed = 0
31 @expected = expected
32 end
33
34 def on_container_start(container)
35 # Use anonymous client SSL domain
36 ssl_domain = Qpid::Proton::SSLDomain.new(Qpid::Proton::SSLDomain::MODE_CLIENT)
37 ssl_domain.peer_authentication(Qpid::Proton::SSLDomain::ANONYMOUS_PEER)
38 c = container.connect(@url, { :ssl_domain => ssl_domain })
39 c.open_sender(@address)
40 end
41
42 def on_connection_open(c)
43 raise "No security!" unless c.transport.ssl?
44 STDOUT.puts "Connection secured with #{c.transport.ssl.protocol_name.inspect}"
45 end
46
47 def on_sendable(sender)
48 while sender.credit > 0 && @sent < @expected
49 msg = Qpid::Proton::Message.new("sequence #{@sent}", { :id => @sent } )
50 sender.send(msg)
51 @sent = @sent + 1
52 end
53 end
54
55 def on_tracker_accept(tracker)
56 @confirmed = @confirmed + 1
57 if @confirmed == @expected
58 puts "All #{@expected} messages confirmed!"
59 tracker.connection.close
60 end
61 end
62 end
63
64 unless (2..3).include? ARGV.size
65 STDERR.puts "Usage: #{__FILE__} URL ADDRESS [COUNT]}
66 Connect to URL and send COUNT messages to ADDRESS"
67 return 1
68 end
69 url, address, count = ARGV
70 count = Integer(count || 10)
71 Qpid::Proton::Container.new(SimpleSend.new(url, address, count)).run