Debian Codegraph @

ruby/lib/core/receiver.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 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # The receiving endpoint.
22 #
23 # @see Sender
24 #
25 class Receiver < Link
26
27 # @private
28 PROTON_METHOD_PREFIX = "pn_link"
29 # @private
30 include Util::Wrapper
31
32 # Open {Receiver} link
33 #
34 # @overload open_receiver(address)
35 # @param address [String] address of the source to receive from
36 # @overload open_receiver(opts)
37 # @param opts [Hash] Receiver options, see {Receiver#open}
38 # @option opts [Integer] :credit_window automatically maintain this much credit
39 # for messages to be pre-fetched while the current message is processed.
40 # @option opts [Boolean] :auto_accept if true, deliveries that are not settled by
41 # the application in {MessagingHandler#on_message} are automatically accepted.
42 # @option opts [Boolean] :dynamic (false) dynamic property for source {Terminus#dynamic}
43 # @option opts [String,Hash] :source source address or source options, see {Terminus#apply}
44 # @option opts [String,Hash] :target target address or target options, see {Terminus#apply}
45 # @option opts [String] :name (generated) unique name for the link.
46 def open(opts=nil)
47 opts ||= {}
48 opts = { :source => opts } if opts.is_a? String
49 @credit_window = opts.fetch(:credit_window, 10)
50 @auto_accept = opts.fetch(:auto_accept, true)
51 source.apply(opts[:source])
52 target.apply(opts[:target])
53 source.dynamic = !!opts[:dynamic]
54 super()
55 self
56 end
57
58 # @return [Integer] credit window, see {#open}
59 attr_reader :credit_window
60
61 # @return [Boolean] auto_accept flag, see {#open}
62 attr_reader :auto_accept
63
64 # @!attribute drain
65 #
66 # The drain mode.
67 #
68 # If a receiver is in drain mode, then the sending endpoint of a link must
69 # immediately use up all available credit on the link. If this is not
70 # possible, the excess credit must be returned by invoking #drained.
71 #
72 # Only the receiving endpoint can set the drain mode.
73 #
74 # @return [Boolean] True if drain mode is set.
75 #
76 proton_set_get :drain
77
78 # @!attribute [r] draining?
79 #
80 # Returns if a link is currently draining.
81 #
82 # A link is defined to be draining when drain mode is set to true and
83 # the sender still has excess credit.
84 #
85 # @return [Boolean] True if the receiver is currently draining.
86 #
87 proton_caller :draining?
88
89 # Grants credit for incoming deliveries.
90 #
91 # @param n [Integer] The amount to increment the link credit.
92 #
93 def flow(n)
94 Cproton.pn_link_flow(@impl, n)
95 end
96
97 # Allows receiving up to the specified limit of data from the remote
98 # endpoint.
99 #
100 # Note that large messages can be streamed across the network, so just
101 # because there is no data to read does not imply the message is complete.
102 #
103 # To ensure the entirety of the message data has been read, either call
104 # #receive until nil is returned, or verify that #partial? is false and
105 # Delivery#pending is 0.
106 #
107 # @param limit [Integer] The maximum bytes to receive.
108 #
109 # @return [Integer, nil] The number of bytes received, or nil if the end of
110 # the stream was reached.
111 #
112 # @see Deliver#pending To see how much buffer space is needed.
113 #
114 # @raise [LinkError] If an error occurs.
115 #
116 def receive(limit)
117 (n, bytes) = Cproton.pn_link_recv(@impl, limit)
118 return nil if n == Qpid::Proton::Error::EOS
119 raise LinkError.new("[#{n}]: #{Cproton.pn_link_error(@impl)}") if n < 0
120 return bytes
121 end
122
123 end
124
125 end