Debian Codegraph @

ruby/lib/core/delivery.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing,
11 # software distributed under the License is distributed on an
12 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 # KIND, either express or implied. See the License for the
14 # specific language governing permissions and limitations
15 # under the License.
16
17
18 module Qpid::Proton
19 # Allow a {Receiver} to indicate the status of a received message to the {Sender}
20 class Delivery < Transfer
21 def initialize(*args) super; @message = nil; end
22
23 # @return [Receiver] The parent {Receiver} link.
24 def receiver() link; end
25
26 # Accept the receiveed message.
27 def accept() settle ACCEPTED; end
28
29 # Reject a message, indicating to the sender that is invalid and should
30 # never be delivered again to this or any other receiver.
31 def reject() settle REJECTED; end
32
33 # Release a message, indicating to the sender that it was not processed
34 # but may be delivered again to this or another receiver.
35 #
36 # @param opts [Hash] Instructions to the sender to modify re-delivery.
37 # To allow re-delivery with no modifications at all use +release(nil)+
38 #
39 # @option opts [Boolean] :failed (true) Instruct the sender to increase
40 # {Message#delivery_count} so future receivers will know there was a
41 # previous failed delivery.
42 #
43 # @option opts [Boolean] :undeliverable (false) Instruct the sender that this
44 # message should never be re-delivered to this receiver, although it may be
45 # delivered other receivers.
46 #
47 # @option opts [Hash] :annotations Instruct the sender to update the
48 # {Message#annotations} with these +key=>value+ pairs before re-delivery,
49 # replacing existing entries in {Message#annotations} with the same key.
50 def release(opts = nil)
51 opts = { :failed => false } if (opts == false) # deprecated
52 failed = !opts || opts.fetch(:failed, true)
53 undeliverable = opts && opts[:undeliverable]
54 annotations = opts && opts[:annotations]
55 annotations = nil if annotations && annotations.empty?
56 if failed || undeliverable || annotations
57 d = Cproton.pn_delivery_local(@impl)
58 Cproton.pn_disposition_set_failed(d, true) if failed
59 Cproton.pn_disposition_set_undeliverable(d, true) if undeliverable
60 Codec::Data.from_object(Cproton.pn_disposition_annotations(d), annotations) if annotations
61 settle(MODIFIED)
62 else
63 settle(RELEASED)
64 end
65 end
66
67 # @deprecated use {#release} with modification options
68 def modify()
69 deprecated __method__, "release(modification_options)"
70 release failed=>true
71 end
72
73 # @return [Boolean] True if the transfer was aborted by the sender.
74 proton_caller :aborted?
75
76 # @return true if the incoming message is complete, call {#message} to retrieve it.
77 def complete?() readable? && !aborted? && !partial?; end
78
79 # Get the message from the delivery.
80 # @return [Message] The message
81 # @raise [AbortedError] if the message has been aborted (check with {#aborted?}
82 # @raise [UnderflowError] if the message is incomplete (check with {#complete?}
83 # @raise [::ArgumentError] if the delivery is not the current delivery on a receiving link.
84 def message
85 unless @message
86 raise AbortedError, "message aborted by sender" if aborted?
87 raise UnderflowError, "incoming message incomplete" if partial?
88 raise ArgumentError, "no incoming message" unless readable?
89 @message = Message.new
90 @message.decode(link.receive(pending))
91 link.advance
92 end
93 @message
94 end
95 end
96 end