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