class Faye::Transport
Attributes
connection_type[RW]
endpoint[R]
Public Class Methods
new(dispatcher, endpoint)
click to toggle source
Calls superclass method
# File lib/faye/transport/transport.rb, line 10 def initialize(dispatcher, endpoint) super() @dispatcher = dispatcher @endpoint = endpoint @outbox = [] end
Private Class Methods
get(dispatcher, allowed, disabled, &callback)
click to toggle source
# File lib/faye/transport/transport.rb, line 115 def get(dispatcher, allowed, disabled, &callback) endpoint = dispatcher.endpoint select = lambda do |(conn_type, klass), resume| conn_endpoint = dispatcher.endpoint_for(conn_type) if disabled.include?(conn_type) next resume.call end unless allowed.include?(conn_type) klass.usable?(dispatcher, conn_endpoint) { |u| } next resume.call end klass.usable?(dispatcher, conn_endpoint) do |is_usable| next resume.call unless is_usable transport = klass.respond_to?(:create) ? klass.create(dispatcher, conn_endpoint) : klass.new(dispatcher, conn_endpoint) callback.call(transport) end end error = lambda do raise "Could not find a usable connection type for #{ endpoint }" end Faye.async_each(@transports, select, error) end
register(type, klass)
click to toggle source
# File lib/faye/transport/transport.rb, line 144 def register(type, klass) @transports << [type, klass] klass.connection_type = type end
Public Instance Methods
batching?()
click to toggle source
# File lib/faye/transport/transport.rb, line 17 def batching? true end
close()
click to toggle source
# File lib/faye/transport/transport.rb, line 21 def close end
connection_type()
click to toggle source
# File lib/faye/transport/transport.rb, line 28 def connection_type self.class.connection_type end
encode(messages)
click to toggle source
# File lib/faye/transport/transport.rb, line 24 def encode(messages) '' end
send_message(message)
click to toggle source
# File lib/faye/transport/transport.rb, line 32 def send_message(message) client_id = @dispatcher.client_id debug('Client ? sending message to ? via ?: ?', client_id, @endpoint, connection_type, message) unless batching? promise = EventMachine::DefaultDeferrable.new promise.succeed(request([message])) return promise end @outbox << message flush_large_batch @promise ||= EventMachine::DefaultDeferrable.new if message['channel'] == Channel::HANDSHAKE add_timeout(:publish, 0.01) { flush } return @promise end if message['channel'] == Channel::CONNECT @connection_message = message end add_timeout(:publish, Engine::MAX_DELAY) { flush } @promise end
Private Instance Methods
flush()
click to toggle source
# File lib/faye/transport/transport.rb, line 61 def flush remove_timeout(:publish) if @outbox.size > 1 and @connection_message @connection_message['advice'] = {'timeout' => 0} end @promise.succeed(request(@outbox)) @promise = nil @connection_message = nil @outbox = [] end
flush_large_batch()
click to toggle source
# File lib/faye/transport/transport.rb, line 75 def flush_large_batch string = encode(@outbox) return if string.size < @dispatcher.max_request_size last = @outbox.pop flush @outbox.push(last) if last end
handle_error(messages, immediate = false)
click to toggle source
# File lib/faye/transport/transport.rb, line 92 def handle_error(messages, immediate = false) client_id = @dispatcher.client_id debug('Client ? failed to send to ? via ?: ?', client_id, @endpoint, connection_type, messages) messages.each do |message| @dispatcher.handle_error(message, immediate) end end
receive(replies)
click to toggle source
# File lib/faye/transport/transport.rb, line 83 def receive(replies) replies = [replies].flatten client_id = @dispatcher.client_id debug('Client ? received from ? via ?: ?', client_id, @endpoint, connection_type, replies) replies.each do |reply| @dispatcher.handle_response(reply) end end