1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
module Qpid::Proton
# The base for both Sender and Receiver, providing common functionality
# between both ends.
#
# A Link has a single parent Qpid::Proton::Session instance.
#
class Link < Endpoint
# @private
PROTON_METHOD_PREFIX = "pn_link"
# @private
include Util::Wrapper
# The sender will send all deliveries initially unsettled.
SND_UNSETTLED = Cproton::PN_SND_UNSETTLED
# The sender will send all deliveries settled to the receiver.
SND_SETTLED = Cproton::PN_SND_SETTLED
# The sender may send a mixture of settled and unsettled deliveries.
SND_MIXED = Cproton::PN_SND_MIXED
# The receiver will settle deliveries regardless of what the sender does.
RCV_FIRST = Cproton::PN_RCV_FIRST
# The receiver will only settle deliveries after the sender settles.
RCV_SECOND = Cproton::PN_RCV_SECOND
# @!attribute [r] state
#
# Returns the endpoint state flags.
#
proton_caller :state
# @deprecated Use {Sender#open} or {Receiver#open}
proton_caller :open
# Close the local end of the link. The remote end may or may not be closed.
# @param error [Condition] Optional error condition to send with the close.
def close(error=nil)
Condition.assign(_local_condition, error)
Cproton.pn_link_close(@impl)
end
# @!method detach
#
# Detaches the link.
proton_caller :detach
# Advance the current delivery to the next on the link.
#
# For sending links, this operation is used to finish sending message data
# for the current outgoing delivery and move on to the next outgoing
# delivery (if any).
#
# For receiving links, this operatoin is used to finish accessing message
# data from the current incoming delivery and move on to the next incoming
# delivery (if any).
#
# @return [Boolean] True if the current delivery was changed.
#
# @see #current
#
proton_caller :advance
proton_caller :unsettled
# @!attribute [r] credit
#
# Returns the credit balance for a link.
#
# Links use a credit based flow control scheme. Every receiver maintains a
# credit balance that corresponds to the number of deliveries that the
# receiver can accept at any given moment.
#
# As more capacity becomes available at the receiver, it adds credit to this
# balance and communicates the new balance to the sender. Whenever a
# delivery is sent/received, the credit balance maintained by the link is
# decremented by one.
#
# Once the credit balance at the sender reaches zero, the sender must pause
# sending until more credit is obtained from the receiver.
#
# NOte that a sending link may still be used to send deliveries eve if
# credit reaches zero. However those deliveries will end up being buffer by
# the link until enough credit is obtained from the receiver to send them
# over the wire. In this case the balance reported will go negative.
#
# @return [Integer] The credit balance.
#
# @see #flow
#
proton_caller :credit
# @!attribute [r] remote_credit
#
# Returns the remote view of the credit.
#
# The remote view of the credit for a link differs from the local view of
# credit for a link by the number of queued deliveries. In other words,
# remote credit is defined as credit - queued.
#
# @see #queued
# @see #credit
#
# @return [Integer] The remove view of the credit.
#
proton_caller :remote_credit
# @!attribute [r] available
#
# Returns the available deliveries hint for a link.
#
# The available count for a link provides a hint as to the number of
# deliveries that might be able to be sent if sufficient credit were issued
# by the receiving link endpoint.
#
# @return [Integer] The available deliveries hint.
#
# @see Sender#offered
#
proton_caller :available
# @!attribute [r] queued
#
# Returns the number of queued deliveries for a link.
#
# Links may queue deliveries for a number of reasons. For example, there may
# be insufficient credit to send them to the receiver, or they simply may
# not have yet had a chance to be written to the wire.
#
# @return [Integer] The number of queued deliveries.
#
# @see #credit
#
proton_caller :queued
# @!attribute [r] name
#
# Returns the name of the link.
#
# @return [String] The name.
#
proton_caller :name
# @!attribute [r] sender?
#
# Returns if the link is a sender.
#
# @return [Boolean] True if the link is a sender.
#
proton_is :sender
# @!attribute [r] receiver?
#
# Returns if the link is a receiver.
#
# @return [Boolean] True if the link is a receiver.
#
proton_is :receiver
# @private
proton_get :attachments
# Drains excess credit.
#
# When a link is in drain mode, the sender must use all excess credit
# immediately and release any excess credit back to the receiver if there
# are no deliveries available to send.
#
# When invoked on a Sender that is in drain mode, this operation will
# release all excess credit back to the receiver and return the number of
# credits released back to the sender. If the link is not in drain mode,
# this operation is a noop.
#
# When invoked on a Receiver, this operation will return and reset the
# number of credits the sender has released back to it.
#
# @return [Integer] The number of credits drained.
#
proton_caller :drained
# @private
def self.wrap(impl)
return unless impl
return fetch_instance(impl, :pn_link_attachments) ||
(Cproton.pn_link_is_sender(impl) ? Sender : Receiver).new(impl)
end
# @private
def initialize(impl)
@impl = impl
self.class.store_instance(self, :pn_link_attachments)
end
# Returns additional error information.
#
# Whenever a link operation fails (i.e., returns an error code) additional
# error details can be obtained from this method. Ther error object that is
# returned may also be used to clear the error condition.
#
# @return [Error] The error.
#
def error
Cproton.pn_link_error(@impl)
end
# @deprecated use {Session#each_link, Connection#each_link}
def next(state_mask)
deprecated __method__, "Session#each_link, Connection#each_link"
return Link.wrap(Cproton.pn_link_next(@impl, state_mask))
end
# Returns the locally defined source terminus.
#
# @return [Terminus] The terminus
def source
Terminus.new(Cproton.pn_link_source(@impl))
end
# Returns the locally defined target terminus.
#
# @return [Terminus] The terminus.
#
def target
Terminus.new(Cproton.pn_link_target(@impl))
end
# Returns a representation of the remotely defined source terminus.
#
# @return [Terminus] The terminus.
#
def remote_source
Terminus.new(Cproton.pn_link_remote_source(@impl))
end
# Returns a representation of the remotely defined target terminus.
#
# @return [Terminus] The terminus.
#
def remote_target
Terminus.new(Cproton.pn_link_remote_target(@impl))
end
# Returns the parent session.
#
# @return [Session] The session.
#
def session
Session.wrap(Cproton.pn_link_session(@impl))
end
# Returns the parent connection.
#
# @return [Connection] The connection.
#
def connection
self.session.connection
end
# @deprecated use {Sender#send}
def delivery(tag)
deprecated __method__, "Sender#send"
Delivery.new(Cproton.pn_delivery(@impl, tag))
end
# Returns the current delivery.
#
# Each link maintains a sequence of deliveries in the order they were
# created, along with a reference to the *current* delivery. All send and
# receive operations on a link take place on the *current* delivery. If a
# link has no current delivery, the current delivery is automatically
# pointed to the *next* delivery created on the link.
#
# Once initialized, the current delivery remains the same until it is
# changed by advancing, or until it is settled.
#
# @see #next
# @see Delivery#settle
#
# @return [Delivery] The current delivery.
#
def current
Delivery.wrap(Cproton.pn_link_current(@impl))
end
# Sets the local sender settle mode.
#
# @param mode [Integer] The settle mode.
#
# @see #SND_UNSETTLED
# @see #SND_SETTLED
# @see #SND_MIXED
#
def snd_settle_mode=(mode)
Cproton.pn_link_set_snd_settle_mode(@impl, mode)
end
# Returns the local sender settle mode.
#
# @return [Integer] The local sender settle mode.
#
# @see #snd_settle_mode
#
def snd_settle_mode
Cproton.pn_link_snd_settle_mode(@impl)
end
# Sets the local receiver settle mode.
#
# @param mode [Integer] The settle mode.
#
# @see #RCV_FIRST
# @see #RCV_SECOND
#
def rcv_settle_mode=(mode)
Cproton.pn_link_set_rcv_settle_mode(@impl, mode)
end
# Returns the local receiver settle mode.
#
# @return [Integer] The local receiver settle mode.
#
def rcv_settle_mode
Cproton.pn_link_rcv_settle_mode(@impl)
end
# @private
def _local_condition
Cproton.pn_link_condition(@impl)
end
# @private
def _remote_condition
Cproton.pn_link_remote_condition(@impl)
end
def ==(other)
other.respond_to?(:impl) &&
(Cproton.pni_address_of(other.impl) == Cproton.pni_address_of(@impl))
end
end
end
|