| 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 |
require 'socket' |
| 19 |
|
| 20 |
module Qpid::Proton |
| 21 |
|
| 22 |
# Associate an AMQP {Connection} and {Transport} with an {IO} |
| 23 |
# |
| 24 |
# - {#read} reads AMQP binary data from the {IO} and generates events |
| 25 |
# - {#tick} generates timing-related events |
| 26 |
# - {#event} gets events to be dispatched to {Handler::MessagingHandler}s |
| 27 |
# - {#write} writes AMQP binary data to the {IO} |
| 28 |
# |
| 29 |
# Thread safety: The {ConnectionDriver} is not thread safe but separate |
| 30 |
# {ConnectionDriver} instances can be processed concurrently. The |
| 31 |
# {Container} handles multiple connections concurrently in multiple threads. |
| 32 |
# |
| 33 |
class ConnectionDriver |
| 34 |
# Create a {Connection} and {Transport} associated with +io+ |
| 35 |
# @param io [IO] An {IO} or {IO}-like object that responds |
| 36 |
# to {IO#read_nonblock} and {IO#write_nonblock} |
| 37 |
def initialize(io) |
| 38 |
@impl = Cproton.pni_connection_driver or raise NoMemoryError |
| 39 |
@io = io |
| 40 |
@rbuf = "" # String for re-usable read buffer |
| 41 |
end |
| 42 |
|
| 43 |
# @return [Connection] |
| 44 |
def connection() |
| 45 |
@connection ||= Connection.wrap(Cproton.pni_connection_driver_connection(@impl)) |
| 46 |
end |
| 47 |
|
| 48 |
# @return [Transport] |
| 49 |
def transport() |
| 50 |
@transport ||= Transport.wrap(Cproton.pni_connection_driver_transport(@impl)) |
| 51 |
end |
| 52 |
|
| 53 |
# @return [IO] Allows ConnectionDriver to be passed directly to {IO#select} |
| 54 |
def to_io() @io; end |
| 55 |
|
| 56 |
# @return [Bool] True if the driver can read more data |
| 57 |
def can_read?() Cproton.pni_connection_driver_read_size(@impl) > 0; end |
| 58 |
|
| 59 |
# @return [Bool] True if the driver has data to write |
| 60 |
def can_write?() Cproton.pni_connection_driver_write_size(@impl) > 0; end |
| 61 |
|
| 62 |
# True if the ConnectionDriver has nothing left to do: both sides of the |
| 63 |
# transport are closed and there are no events to dispatch. |
| 64 |
def finished?() Cproton.pn_connection_driver_finished(@impl); end |
| 65 |
|
| 66 |
# Get the next event to dispatch, nil if no events available |
| 67 |
def event() |
| 68 |
e = Cproton.pn_connection_driver_next_event(@impl) |
| 69 |
Event.new(e) if e |
| 70 |
end |
| 71 |
|
| 72 |
# True if {#event} will return non-nil |
| 73 |
def event?() Cproton.pn_connection_driver_has_event(@impl); end |
| 74 |
|
| 75 |
# Iterator for all available events |
| 76 |
def each_event() |
| 77 |
while e = event |
| 78 |
yield e |
| 79 |
end |
| 80 |
end |
| 81 |
|
| 82 |
# Non-blocking read from {#io}, generate events for {#event} |
| 83 |
# IO errors are returned as transport errors by {#event}, not raised |
| 84 |
def read |
| 85 |
size = Cproton.pni_connection_driver_read_size(@impl) |
| 86 |
return if size <= 0 |
| 87 |
@io.read_nonblock(size, @rbuf) # Use the same string rbuf for reading each time |
| 88 |
Cproton.pni_connection_driver_read_copy(@impl, @rbuf) unless @rbuf.empty? |
| 89 |
rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EINTR |
| 90 |
# Try again later. |
| 91 |
rescue EOFError # EOF is not an error |
| 92 |
close_read |
| 93 |
rescue IOError, SystemCallError => e |
| 94 |
close e |
| 95 |
end |
| 96 |
|
| 97 |
# Non-blocking write to {#io} |
| 98 |
# IO errors are returned as transport errors by {#event}, not raised |
| 99 |
def write |
| 100 |
data = Cproton.pn_connection_driver_write_buffer(@impl) |
| 101 |
return unless data && data.size > 0 |
| 102 |
n = @io.write_nonblock(data) |
| 103 |
Cproton.pn_connection_driver_write_done(@impl, n) if n > 0 |
| 104 |
rescue Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EINTR |
| 105 |
# Try again later. |
| 106 |
rescue IOError, SystemCallError => e |
| 107 |
close e |
| 108 |
end |
| 109 |
|
| 110 |
# Handle time-related work, for example idle-timeout events. |
| 111 |
# May generate events for {#event} and change {#can_read?}, {#can_write?} |
| 112 |
# |
| 113 |
# @param [Time] now the current time, defaults to {Time#now}. |
| 114 |
# |
| 115 |
# @return [Time] time of the next scheduled event, or nil if there are no |
| 116 |
# scheduled events. If non-nil you must call {#tick} again no later than |
| 117 |
# this time. |
| 118 |
def tick(now=Time.now) |
| 119 |
transport = Cproton.pni_connection_driver_transport(@impl) |
| 120 |
ms = Cproton.pn_transport_tick(transport, (now.to_r * 1000).to_i) |
| 121 |
@next_tick = ms.zero? ? nil : Time.at(ms.to_r / 1000); |
| 122 |
unless @next_tick |
| 123 |
idle = Cproton.pn_transport_get_idle_timeout(transport); |
| 124 |
@next_tick = now + (idle.to_r / 1000) unless idle.zero? |
| 125 |
end |
| 126 |
@next_tick |
| 127 |
end |
| 128 |
|
| 129 |
# Time returned by the last call to {#tick} |
| 130 |
attr_accessor :next_tick |
| 131 |
|
| 132 |
# Disconnect the write side of the transport, *without* sending an AMQP |
| 133 |
# close frame. To close politely, you should use {Connection#close}, the |
| 134 |
# transport will close itself once the protocol close is complete. |
| 135 |
# |
| 136 |
def close_write error=nil |
| 137 |
set_error error |
| 138 |
Cproton.pn_connection_driver_write_close(@impl) |
| 139 |
@io.close_write rescue nil # Allow double-close |
| 140 |
end |
| 141 |
|
| 142 |
# Is the read side of the driver closed? |
| 143 |
def read_closed?() Cproton.pn_connection_driver_read_closed(@impl); end |
| 144 |
|
| 145 |
# Is the write side of the driver closed? |
| 146 |
def write_closed?() Cproton.pn_connection_driver_read_closed(@impl); end |
| 147 |
|
| 148 |
# Disconnect the read side of the transport, without waiting for an AMQP |
| 149 |
# close frame. See comments on {#close_write} |
| 150 |
def close_read error=nil |
| 151 |
set_error error |
| 152 |
Cproton.pn_connection_driver_read_close(@impl) |
| 153 |
@io.close_read rescue nil # Allow double-close |
| 154 |
end |
| 155 |
|
| 156 |
# Disconnect both sides of the transport sending/waiting for AMQP close |
| 157 |
# frames. See comments on {#close_write} |
| 158 |
def close error=nil |
| 159 |
close_write error |
| 160 |
close_read |
| 161 |
end |
| 162 |
|
| 163 |
private |
| 164 |
|
| 165 |
def set_error err |
| 166 |
transport.condition ||= Condition.convert(err, "proton:io") if err |
| 167 |
end |
| 168 |
end |
| 169 |
|
| 170 |
# A {ConnectionDriver} that feeds raw proton events to a handler. |
| 171 |
class HandlerDriver < ConnectionDriver |
| 172 |
# Combine an {IO} with a handler and provide |
| 173 |
# a simplified way to run the driver via {#process} |
| 174 |
# |
| 175 |
# @param io [IO] |
| 176 |
# @param handler [Handler::MessagingHandler] to receive raw events in {#dispatch} and {#process} |
| 177 |
def initialize(io, handler) |
| 178 |
super(io) |
| 179 |
@handler = handler |
| 180 |
@adapter = Handler::Adapter.adapt(handler) |
| 181 |
end |
| 182 |
|
| 183 |
# @return [MessagingHandler] The handler dispatched to by {#process} |
| 184 |
attr_reader :handler |
| 185 |
|
| 186 |
# Dispatch all available raw proton events from {#event} to {#handler} |
| 187 |
def dispatch() |
| 188 |
each_event do |e| |
| 189 |
case e.method # Events that affect the driver |
| 190 |
when :on_transport_tail_closed then close_read |
| 191 |
when :on_transport_head_closed then close_write |
| 192 |
when :on_transport_closed then @io.close rescue nil # Allow double-close |
| 193 |
end |
| 194 |
e.dispatch @adapter |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
# Do {#read}, {#tick}, {#write} and {#dispatch} without blocking. |
| 199 |
# @param [Time] now the current time |
| 200 |
# @return [Time] Latest time to call {#process} again for scheduled events, |
| 201 |
# or nil if there are no scheduled events |
| 202 |
def process(now=Time.now) |
| 203 |
read |
| 204 |
dispatch |
| 205 |
next_tick = tick(now) |
| 206 |
dispatch |
| 207 |
write |
| 208 |
dispatch |
| 209 |
return next_tick |
| 210 |
end |
| 211 |
end |
| 212 |
end |