Class: Faye::Transport

Inherits:
Object
  • Object
show all
Includes:
Logging, Publisher, Timeouts
Defined in:
lib/faye/transport/transport.rb

Direct Known Subclasses

WebSocket, Transport::Http, Transport::Local

Defined Under Namespace

Classes: Http, Local, WebSocket

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Timeouts

#add_timeout, #remove_all_timeouts, #remove_timeout

Methods included from Publisher

#unbind

Constructor Details

#initialize(client, endpoint) ⇒ Transport

Returns a new instance of Transport.



10
11
12
13
14
15
# File 'lib/faye/transport/transport.rb', line 10

def initialize(client, endpoint)
  super()
  @client   = client
  @endpoint = endpoint
  @outbox   = []
end

Class Attribute Details

.connection_typeObject

Returns the value of attribute connection_type.



103
104
105
# File 'lib/faye/transport/transport.rb', line 103

def connection_type
  @connection_type
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



8
9
10
# File 'lib/faye/transport/transport.rb', line 8

def endpoint
  @endpoint
end

Class Method Details

.get(client, allowed, disabled, &callback) ⇒ Object



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
# File 'lib/faye/transport/transport.rb', line 105

def get(client, allowed, disabled, &callback)
  endpoint = client.endpoint

  select = lambda do |(conn_type, klass), resume|
    conn_endpoint = client.endpoints[conn_type] || endpoint

    if disabled.include?(conn_type)
      next resume.call
    end

    unless allowed.include?(conn_type)
      klass.usable?(client, conn_endpoint) { |u| }
      next resume.call
    end

    klass.usable?(client, conn_endpoint) do |is_usable|
      next resume.call unless is_usable
      transport = klass.respond_to?(:create) ? klass.create(client, conn_endpoint) : klass.new(client, 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) ⇒ Object



134
135
136
137
# File 'lib/faye/transport/transport.rb', line 134

def register(type, klass)
  @transports << [type, klass]
  klass.connection_type = type
end

Instance Method Details

#batching?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/faye/transport/transport.rb', line 17

def batching?
  true
end

#closeObject



21
22
# File 'lib/faye/transport/transport.rb', line 21

def close
end

#connection_typeObject



28
29
30
# File 'lib/faye/transport/transport.rb', line 28

def connection_type
  self.class.connection_type
end

#encode(envelopes) ⇒ Object



24
25
26
# File 'lib/faye/transport/transport.rb', line 24

def encode(envelopes)
  ''
end

#flushObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/faye/transport/transport.rb', line 53

def flush
  remove_timeout(:publish)

  if @outbox.size > 1 and @connection_message
    @connection_message['advice'] = {'timeout' => 0}
  end

  request(@outbox)

  @connection_message = nil
  @outbox = []
end

#flush_large_batchObject



66
67
68
69
70
71
72
# File 'lib/faye/transport/transport.rb', line 66

def flush_large_batch
  string = encode(@outbox)
  return if string.size < @client.max_request_size
  last = @outbox.pop
  flush
  @outbox.push(last) if last
end

#handle_error(envelopes, immediate = false) ⇒ Object



82
83
84
# File 'lib/faye/transport/transport.rb', line 82

def handle_error(envelopes, immediate = false)
  envelopes.each { |e| e.set_deferred_status(:failed, immediate) }
end

#receive(envelopes, responses) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/faye/transport/transport.rb', line 74

def receive(envelopes, responses)
  envelopes.each { |e| e.set_deferred_status(:succeeded) }
  responses = [responses].flatten
  client_id = @client.instance_eval { @client_id }
  debug('Client ? received from ?: ?', client_id, @endpoint, responses)
  responses.each { |response| @client.receive_message(response) }
end

#send(envelope) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/faye/transport/transport.rb', line 32

def send(envelope)
  message = envelope.message
  client_id = @client.instance_eval { @client_id }
  debug('Client ? sending message to ?: ?', client_id, @endpoint, message)

  return request([envelope]) unless batching?

  @outbox << envelope

  if message['channel'] == Channel::HANDSHAKE
    return add_timeout(:publish, 0.01) { flush }
  end

  if message['channel'] == Channel::CONNECT
    @connection_message = message
  end

  flush_large_batch
  add_timeout(:publish, Engine::MAX_DELAY) { flush }
end