| 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 |
# @deprecated all important features are available from {#Connection} |
| 22 |
class Transport |
| 23 |
include Util::Deprecation |
| 24 |
|
| 25 |
# @private |
| 26 |
PROTON_METHOD_PREFIX = "pn_transport" |
| 27 |
# @private |
| 28 |
include Util::Wrapper |
| 29 |
|
| 30 |
# Turn logging off entirely. |
| 31 |
TRACE_OFF = Cproton::PN_TRACE_OFF |
| 32 |
# Log raw binary data into/out of the transport. |
| 33 |
TRACE_RAW = Cproton::PN_TRACE_RAW |
| 34 |
# Log frames into/out of the transport. |
| 35 |
TRACE_FRM = Cproton::PN_TRACE_FRM |
| 36 |
# Log driver related events; i.e., initialization, end of stream, etc. |
| 37 |
TRACE_DRV = Cproton::PN_TRACE_DRV |
| 38 |
|
| 39 |
proton_get :user |
| 40 |
|
| 41 |
# @!attribute channel_max |
| 42 |
# |
| 43 |
# @return [Integer] The maximum allowed channel. |
| 44 |
# |
| 45 |
proton_set_get :channel_max |
| 46 |
|
| 47 |
# @!attribute [r] remote_channel_max |
| 48 |
# |
| 49 |
# @return [Integer] The maximum allowed channel of a transport's remote peer. |
| 50 |
# |
| 51 |
proton_caller :remote_channel_max |
| 52 |
|
| 53 |
# @!attribute max_frame_size |
| 54 |
# |
| 55 |
# @return [Integer] The maximum frame size. |
| 56 |
proton_set_get :max_frame |
| 57 |
proton_get :remote_max_frame |
| 58 |
|
| 59 |
# @!attribute [r] remote_max_frame_size |
| 60 |
# |
| 61 |
# @return [Integer] The maximum frame size of the transport's remote peer. |
| 62 |
# |
| 63 |
proton_get :remote_max_frame_size |
| 64 |
|
| 65 |
# @!attribute idle_timeout |
| 66 |
# |
| 67 |
# @deprecated use {Connection#open} with the +:idle_timeout+ option to set |
| 68 |
# the timeout, and {Connection#idle_timeout} to query the remote timeout. |
| 69 |
# |
| 70 |
# The Connection timeout values are in *seconds* and are automatically |
| 71 |
# converted. |
| 72 |
# |
| 73 |
# @return [Integer] The idle timeout in *milliseconds*. |
| 74 |
# |
| 75 |
proton_set_get :idle_timeout |
| 76 |
|
| 77 |
# @!attribute [r] remote_idle_timeout in milliseconds |
| 78 |
# |
| 79 |
# @deprecated Use {Connection#idle_timeout} to query the remote timeout. |
| 80 |
# |
| 81 |
# @return [Integer] The idle timeout for the transport's remote peer. |
| 82 |
# |
| 83 |
proton_set_get :remote_idle_timeout |
| 84 |
|
| 85 |
# @!attribute [r] capacity |
| 86 |
# |
| 87 |
# If the engine is in an exception state such as encountering an error |
| 88 |
# condition or reaching the end of stream state, a negative value will |
| 89 |
# be returned indicating the condition. |
| 90 |
# |
| 91 |
# If an error is indicated, further deteails can be obtained from |
| 92 |
# #error. |
| 93 |
# |
| 94 |
# Calls to #process may alter the value of this value. See #process for |
| 95 |
# more details |
| 96 |
# |
| 97 |
# @return [Integer] The amount of free space for input following the |
| 98 |
# transport's tail pointer. |
| 99 |
# |
| 100 |
proton_caller :capacity |
| 101 |
|
| 102 |
# @!attribute [r] head |
| 103 |
# |
| 104 |
# This referneces queued output data. It reports the bytes of output data. |
| 105 |
# |
| 106 |
# Calls to #pop may alter this attribute, and any data it references. |
| 107 |
# |
| 108 |
# @return [String] The transport's head pointer. |
| 109 |
# |
| 110 |
proton_caller :head |
| 111 |
|
| 112 |
# @!attribute [r] tail |
| 113 |
# |
| 114 |
# The amount of free space following this data is reported by #capacity. |
| 115 |
# |
| 116 |
# Calls to #process may alter the value of this attribute. |
| 117 |
# |
| 118 |
# @return [String] The transport's tail pointer. |
| 119 |
# |
| 120 |
proton_caller :tail |
| 121 |
|
| 122 |
# @!attribute [r] pending |
| 123 |
# |
| 124 |
# If the ending is in an exceptional state, such as encountering an error |
| 125 |
# condition or reachign the end of the stream state, a negative value will |
| 126 |
# be returned indicating the condition. |
| 127 |
# |
| 128 |
# If an error is indicated, further details can be obtained from #error. |
| 129 |
# |
| 130 |
# Calls to #pop may alter the value of this pointer as well. |
| 131 |
# |
| 132 |
# @return [Integer] The number of pending output bytes following the header |
| 133 |
# pointer. |
| 134 |
# |
| 135 |
# @raise [TransportError] If any error other than an end of stream occurs. |
| 136 |
# |
| 137 |
proton_caller :pending |
| 138 |
|
| 139 |
# @!attribute [r] closed? |
| 140 |
# |
| 141 |
# A transport is defined to be closed when both the tail and the head are |
| 142 |
# closed. In other words, when both #capacity < 0 and #pending < 0. |
| 143 |
# |
| 144 |
# @return [Boolean] Returns true if the tranpsort is closed. |
| 145 |
# |
| 146 |
proton_caller :closed? |
| 147 |
|
| 148 |
# @!attribute [r] frames_output |
| 149 |
# |
| 150 |
# @return [Integer] The number of frames output by a transport. |
| 151 |
# |
| 152 |
proton_get :frames_output |
| 153 |
|
| 154 |
# @!attribute [r] frames_input |
| 155 |
# |
| 156 |
# @return [Integer] The number of frames input by a transport. |
| 157 |
# |
| 158 |
proton_get :frames_input |
| 159 |
|
| 160 |
# @private |
| 161 |
include Util::ErrorHandler |
| 162 |
|
| 163 |
# @private |
| 164 |
def self.wrap(impl) |
| 165 |
return nil if impl.nil? |
| 166 |
|
| 167 |
self.fetch_instance(impl, :pn_transport_attachments) || Transport.new(impl) |
| 168 |
end |
| 169 |
|
| 170 |
# Creates a new transport instance. |
| 171 |
def initialize(impl = Cproton.pn_transport) |
| 172 |
@impl = impl |
| 173 |
@ssl = nil |
| 174 |
self.class.store_instance(self, :pn_transport_attachments) |
| 175 |
end |
| 176 |
|
| 177 |
# Set server mode for this tranport - enables protocol detection |
| 178 |
# and server-side authentication for incoming connections |
| 179 |
def set_server() Cproton.pn_transport_set_server(@impl); end |
| 180 |
|
| 181 |
# Returns whether the transport has any buffered data. |
| 182 |
# |
| 183 |
# @return [Boolean] True if the transport has no buffered data. |
| 184 |
# |
| 185 |
def quiesced? |
| 186 |
Cproton.pn_transport_quiesced(@impl) |
| 187 |
end |
| 188 |
|
| 189 |
# @return [Condition, nil] transport error condition or nil if there is no error. |
| 190 |
def condition |
| 191 |
Condition.convert(Cproton.pn_transport_condition(@impl)) |
| 192 |
end |
| 193 |
|
| 194 |
# Set the error condition for the transport. |
| 195 |
# @param c [Condition] The condition to set |
| 196 |
def condition=(c) |
| 197 |
Condition.assign(Cproton.pn_transport_condition(@impl), c) |
| 198 |
end |
| 199 |
|
| 200 |
# Binds to the given connection. |
| 201 |
# |
| 202 |
# @param connection [Connection] The connection. |
| 203 |
# |
| 204 |
def bind(connection) |
| 205 |
Cproton.pn_transport_bind(@impl, connection.impl) |
| 206 |
end |
| 207 |
|
| 208 |
# Unbinds from the previous connection. |
| 209 |
# |
| 210 |
def unbind |
| 211 |
Cproton.pn_transport_unbind(@impl) |
| 212 |
end |
| 213 |
|
| 214 |
# Updates the transports trace flags. |
| 215 |
# |
| 216 |
# @param level [Integer] The trace level. |
| 217 |
# |
| 218 |
# @see TRACE_OFF |
| 219 |
# @see TRACE_RAW |
| 220 |
# @see TRACE_FRM |
| 221 |
# @see TRACE_DRV |
| 222 |
# |
| 223 |
def trace(level) |
| 224 |
Cproton.pn_transport_trace(@impl, level) |
| 225 |
end |
| 226 |
|
| 227 |
# Return the AMQP connection associated with the transport. |
| 228 |
# |
| 229 |
# @return [Connection, nil] The bound connection, or nil. |
| 230 |
# |
| 231 |
def connection |
| 232 |
Connection.wrap(Cproton.pn_transport_connection(@impl)) |
| 233 |
end |
| 234 |
|
| 235 |
# Log a message to the transport's logging mechanism. |
| 236 |
# |
| 237 |
# This can be using in a debugging scenario as the message will be |
| 238 |
# prepended with the transport's identifier. |
| 239 |
# |
| 240 |
# @param message [String] The message to be logged. |
| 241 |
# |
| 242 |
def log(message) |
| 243 |
Cproton.pn_transport_log(@impl, message) |
| 244 |
end |
| 245 |
|
| 246 |
# Pushes the supplied bytes into the tail of the transport. |
| 247 |
# |
| 248 |
# @param data [String] The bytes to be pushed. |
| 249 |
# |
| 250 |
# @return [Integer] The number of bytes pushed. |
| 251 |
# |
| 252 |
def push(data) |
| 253 |
Cproton.pn_transport_push(@impl, data, data.length) |
| 254 |
end |
| 255 |
|
| 256 |
# Process input data following the tail pointer. |
| 257 |
# |
| 258 |
# Calling this function will cause the transport to consume the specified |
| 259 |
# number of bytes of input occupying the free space following the tail |
| 260 |
# pointer. It may also change the value for #tail, as well as the amount of |
| 261 |
# free space reported by #capacity. |
| 262 |
# |
| 263 |
# @param size [Integer] The number of bytes to process. |
| 264 |
# |
| 265 |
# @raise [TransportError] If an error occurs. |
| 266 |
# |
| 267 |
def process(size) |
| 268 |
Cproton.pn_transport_process(@impl, size) |
| 269 |
end |
| 270 |
|
| 271 |
# Indicate that the input has reached EOS (end of stream). |
| 272 |
# |
| 273 |
# This tells the transport that no more input will be forthcoming. |
| 274 |
# |
| 275 |
# @raise [TransportError] If an error occurs. |
| 276 |
# |
| 277 |
def close_tail |
| 278 |
Cproton.pn_transport_close_tail(@impl) |
| 279 |
end |
| 280 |
|
| 281 |
# Returns the specified number of bytes from the transport's buffers. |
| 282 |
# |
| 283 |
# @param size [Integer] The number of bytes to return. |
| 284 |
# |
| 285 |
# @return [String] The data peeked. |
| 286 |
# |
| 287 |
# @raise [TransportError] If an error occurs. |
| 288 |
# |
| 289 |
def peek(size) |
| 290 |
cd, out = Cproton.pn_transport_peek(@impl, size) |
| 291 |
return nil if cd == Qpid::Proton::Error::EOS |
| 292 |
raise TransportError.new if cd < -1 |
| 293 |
out |
| 294 |
end |
| 295 |
|
| 296 |
# Removes the specified number of bytes from the pending output queue |
| 297 |
# following the transport's head pointer. |
| 298 |
# |
| 299 |
# @param size [Integer] The number of bytes to remove. |
| 300 |
# |
| 301 |
def pop(size) |
| 302 |
Cproton.pn_transport_pop(@impl, size) |
| 303 |
end |
| 304 |
|
| 305 |
# Indicate that the output has closed. |
| 306 |
# |
| 307 |
# Tells the transport that no more output will be popped. |
| 308 |
# |
| 309 |
# @raise [TransportError] If an error occurs. |
| 310 |
# |
| 311 |
def close_head |
| 312 |
Cproton.pn_transport_close_head(@impl) |
| 313 |
end |
| 314 |
|
| 315 |
# Process any pending transport timer events. |
| 316 |
# |
| 317 |
# This method should be called after all pending input has been |
| 318 |
# processed by the transport (see #input), and before generating |
| 319 |
# output (see #output). |
| 320 |
# |
| 321 |
# It returns the deadline for the next pending timer event, if any |
| 322 |
# art present. |
| 323 |
# |
| 324 |
# @param now [Time] The timestamp. |
| 325 |
# |
| 326 |
# @return [Integer] If non-zero, the expiration time of the next pending |
| 327 |
# timer event for the transport. The caller must invoke #tick again at |
| 328 |
# least once at or before this deadline occurs. |
| 329 |
# |
| 330 |
def tick(now) |
| 331 |
Cproton.pn_transport_tick(@impl, now) |
| 332 |
end |
| 333 |
|
| 334 |
# Create, or return existing, SSL object for the transport. |
| 335 |
# @return [SASL] the SASL object |
| 336 |
def sasl |
| 337 |
SASL.new(self) |
| 338 |
end |
| 339 |
|
| 340 |
# Creates, or returns an existing, SSL object for the transport. |
| 341 |
# |
| 342 |
# @param domain [SSLDomain] The SSL domain. |
| 343 |
# @param session_details [SSLDetails] The SSL session details. |
| 344 |
# |
| 345 |
# @return [SSL] The SSL object. |
| 346 |
# |
| 347 |
def ssl(domain = nil, session_details = nil) |
| 348 |
@ssl ||= SSL.create(self, domain, session_details) |
| 349 |
end |
| 350 |
|
| 351 |
# @private |
| 352 |
def ssl? |
| 353 |
[email protected]? |
| 354 |
end |
| 355 |
|
| 356 |
# @private |
| 357 |
# Options are documented {Connection#open}, keep that consistent with this |
| 358 |
def apply opts |
| 359 |
sasl if opts[:sasl_enabled] # Explicitly enabled |
| 360 |
unless opts.include?(:sasl_enabled) && !opts[:sasl_enabled] # Not explicitly disabled |
| 361 |
sasl.allowed_mechs = opts[:sasl_allowed_mechs] if opts.include? :sasl_allowed_mechs |
| 362 |
sasl.allow_insecure_mechs = opts[:sasl_allow_insecure_mechs] if opts.include? :sasl_allow_insecure_mechs |
| 363 |
end |
| 364 |
self.channel_max= opts[:max_sessions] if opts.include? :max_sessions |
| 365 |
self.max_frame = opts[:max_frame_size] if opts.include? :max_frame_size |
| 366 |
# NOTE: The idle_timeout option is in Numeric *seconds*, can be Integer, Float or Rational. |
| 367 |
# This is consistent with idiomatic ruby. |
| 368 |
# The transport #idle_timeout property is in *milliseconds* passed direct to C. |
| 369 |
# Direct use of the transport is deprecated. |
| 370 |
self.idle_timeout= (opts[:idle_timeout]*1000).round if opts.include? :idle_timeout |
| 371 |
self.ssl(opts[:ssl_domain]) if opts[:ssl_domain] |
| 372 |
end |
| 373 |
|
| 374 |
can_raise_error :process, :error_class => TransportError |
| 375 |
can_raise_error :close_tail, :error_class => TransportError |
| 376 |
can_raise_error :pending, :error_class => TransportError, :below => Error::EOS |
| 377 |
can_raise_error :close_head, :error_class => TransportError |
| 378 |
end |
| 379 |
end |