Class: Faye::Dispatcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging, Publisher
Defined in:
lib/faye/protocol/dispatcher.rb

Defined Under Namespace

Classes: Envelope

Constant Summary collapse

MAX_REQUEST_SIZE =
2048
DEFAULT_RETRY =
5.0
UP =
1
DOWN =
2

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publisher

#unbind

Constructor Details

#initialize(client, endpoint, options) ⇒ Dispatcher

Returns a new instance of Dispatcher.


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/faye/protocol/dispatcher.rb', line 22

def initialize(client, endpoint, options)
  super()

  @client     = client
  @endpoint   = String === endpoint ? URI(endpoint) : endpoint
  @alternates = options[:endpoints] || {}

  @cookies       = CookieJar::Jar.new
  @disabled      = []
  @envelopes     = {}
  @headers       = {}
  @retry         = options[:retry] || DEFAULT_RETRY
  @scheduler     = options[:scheduler] || Faye::Scheduler
  @state         = 0
  @transports    = {}
  @ws_extensions = []

  @proxy = options[:proxy] || {}
  @proxy = { :origin => @proxy } if String === @proxy

  [*options[:websocket_extensions]].each do |extension|
    add_websocket_extension(extension)
  end

  @tls = { :verify_peer => true }.merge(options[:tls] || {})

  @alternates.each do |type, url|
    @alternates[type] = URI(url)
  end

  @max_request_size = MAX_REQUEST_SIZE
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id


18
19
20
# File 'lib/faye/protocol/dispatcher.rb', line 18

def client_id
  @client_id
end

#cookiesObject (readonly)

Returns the value of attribute cookies


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def cookies
  @cookies
end

#endpointObject (readonly)

Returns the value of attribute endpoint


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def headers
  @headers
end

#max_request_sizeObject (readonly)

Returns the value of attribute max_request_size


20
21
22
# File 'lib/faye/protocol/dispatcher.rb', line 20

def max_request_size
  @max_request_size
end

#proxyObject (readonly)

Returns the value of attribute proxy


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def proxy
  @proxy
end

#retryObject (readonly)

Returns the value of attribute retry


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def retry
  @retry
end

#timeoutObject

Returns the value of attribute timeout


18
19
20
# File 'lib/faye/protocol/dispatcher.rb', line 18

def timeout
  @timeout
end

#tlsObject (readonly)

Returns the value of attribute tls


19
20
21
# File 'lib/faye/protocol/dispatcher.rb', line 19

def tls
  @tls
end

#transportsObject (readonly)

Returns the value of attribute transports


20
21
22
# File 'lib/faye/protocol/dispatcher.rb', line 20

def transports
  @transports
end

#ws_extensionsObject (readonly)

Returns the value of attribute ws_extensions


20
21
22
# File 'lib/faye/protocol/dispatcher.rb', line 20

def ws_extensions
  @ws_extensions
end

Instance Method Details

#add_websocket_extension(extension) ⇒ Object


59
60
61
# File 'lib/faye/protocol/dispatcher.rb', line 59

def add_websocket_extension(extension)
  @ws_extensions << extension
end

#closeObject


71
72
73
74
75
# File 'lib/faye/protocol/dispatcher.rb', line 71

def close
  transport = @transport
  @transport = nil
  transport.close if transport
end

#connection_typesObject


77
78
79
# File 'lib/faye/protocol/dispatcher.rb', line 77

def connection_types
  Transport.connection_types
end

#disable(feature) ⇒ Object


63
64
65
# File 'lib/faye/protocol/dispatcher.rb', line 63

def disable(feature)
  @disabled << feature
end

#endpoint_for(connection_type) ⇒ Object


55
56
57
# File 'lib/faye/protocol/dispatcher.rb', line 55

def endpoint_for(connection_type)
  @alternates[connection_type] || @endpoint
end

#handle_error(message, immediate = false) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/faye/protocol/dispatcher.rb', line 141

def handle_error(message, immediate = false)
  return unless envelope = @envelopes[message['id']]
  return unless request = envelope.request

  request.callback do |req|
    req.close if req.respond_to?(:close)
  end

  scheduler = envelope.scheduler
  scheduler.fail!

  EventMachine.cancel_timer(envelope.timer)
  envelope.request = envelope.timer = nil

  if immediate
    send_envelope(envelope)
  else
    envelope.timer = EventMachine.add_timer(scheduler.interval) do
      envelope.timer = nil
      send_envelope(envelope)
    end
  end

  return if @state == DOWN
  @state = DOWN
  @client.trigger('transport:down')
end

#handle_response(reply) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/faye/protocol/dispatcher.rb', line 128

def handle_response(reply)
  if reply.has_key?('successful') and envelope = @envelopes.delete(reply['id'])
    envelope.scheduler.succeed!
    EventMachine.cancel_timer(envelope.timer) if envelope.timer
  end

  trigger(:message, reply)

  return if @state == UP
  @state = UP
  @client.trigger('transport:up')
end

#select_transport(transport_types) ⇒ Object


81
82
83
84
85
86
87
88
89
90
# File 'lib/faye/protocol/dispatcher.rb', line 81

def select_transport(transport_types)
  Transport.get(self, transport_types, @disabled) do |transport|
    debug('Selected ? transport for ?', transport.connection_type, transport.endpoint)

    next if transport == @transport
    @transport.close if @transport

    @transport = transport
  end
end

#send_message(message, timeout, options = {}) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/faye/protocol/dispatcher.rb', line 92

def send_message(message, timeout, options = {})
  id       = message['id']
  attempts = options[:attempts]
  deadline = options[:deadline] && Time.now.to_f + options[:deadline]
  envelope = @envelopes[id]

  unless envelope
    scheduler = @scheduler.new(message, :timeout => timeout, :interval => @retry, :attempts => attempts, :deadline => deadline)
    envelope  = @envelopes[id] = Envelope.new(message, scheduler, nil, nil)
  end

  send_envelope(envelope)
end

#set_header(name, value) ⇒ Object


67
68
69
# File 'lib/faye/protocol/dispatcher.rb', line 67

def set_header(name, value)
  @headers[name.to_s] = value.to_s
end