Class: Faye::Server

Inherits:
Object
  • Object
show all
Includes:
Extensible, Logging
Defined in:
lib/faye/protocol/server.rb

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes included from Logging

#log_level

Instance Method Summary collapse

Methods included from Extensible

#add_extension, #pipe_through_extensions, #remove_extension

Methods included from Logging

#log

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



9
10
11
12
13
14
15
16
# File 'lib/faye/protocol/server.rb', line 9

def initialize(options = {})
  @options    = options || {}
  engine_opts = @options[:engine] || {}
  engine_opts[:timeout] = @options[:timeout]
  @engine     = Faye::Engine.get(engine_opts)

  info 'Created new server: ?', @options
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



7
8
9
# File 'lib/faye/protocol/server.rb', line 7

def engine
  @engine
end

Instance Method Details

#advize(response, connection_type) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/faye/protocol/server.rb', line 112

def advize(response, connection_type)
  return unless [Channel::HANDSHAKE, Channel::CONNECT].include?(response['channel'])
  
  if connection_type == 'eventsource'
    interval = (@engine.timeout * 1000).floor
    timeout  = 0
  else
    interval = (@engine.interval * 1000).floor
    timeout  = (@engine.timeout * 1000).floor
  end
  
  advice = response['advice'] ||= {}
  if response['error']
    advice['reconnect'] ||= 'handshake'
  else
    advice['reconnect'] ||= 'retry'
    advice['interval']  ||= interval
    advice['timeout']   ||= timeout
  end
end

#close_socket(client_id) ⇒ Object



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

def close_socket(client_id)
  @engine.flush(client_id)
end

#connect(message, local = false, &callback) ⇒ Object

MUST contain * clientId

* connectionType

MAY contain * ext

* id


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/faye/protocol/server.rb', line 168

def connect(message, local = false, &callback)
  response        = make_response(message)
  client_id       = message['clientId']
  connection_type = message['connectionType']
  
  @engine.client_exists(client_id) do |exists|
    response['error'] = Error.client_unknown(client_id) unless exists
    response['error'] = Error.parameter_missing('clientId') if client_id.nil?
    
    unless CONNECTION_TYPES.include?(connection_type)
      response['error'] = Error.conntype_mismatch(connection_type)
    end
    
    response['error'] = Error.parameter_missing('connectionType') if connection_type.nil?
    
    response['successful'] = response['error'].nil?
    
    if !response['successful']
      response.delete('clientId')
      next callback.call(response)
    end
    
    if message['connectionType'] == 'eventsource'
      message['advice'] ||= {}
      message['advice']['timeout'] = 0
    end
    
    @engine.connect(response['clientId'], message['advice']) do |events|
      callback.call([response] + events)
    end
  end
end

#disconnect(message, local = false, &callback) ⇒ Object

MUST contain * clientId MAY contain * ext

* id


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/faye/protocol/server.rb', line 204

def disconnect(message, local = false, &callback)
  response   = make_response(message)      
  client_id  = message['clientId']
  
  @engine.client_exists(client_id) do |exists|
    response['error'] = Error.client_unknown(client_id) unless exists
    response['error'] = Error.parameter_missing('clientId') if client_id.nil?
    
    response['successful'] = response['error'].nil?
    response.delete('clientId') unless response['successful']
    
    @engine.destroy_client(client_id) if response['successful']
    callback.call(response)
  end
end

#flush_connection(messages) ⇒ Object



18
19
20
21
22
# File 'lib/faye/protocol/server.rb', line 18

def flush_connection(messages)
  client_id = Faye.client_id_from_messages(messages)
  info 'Flushing connection for ?', client_id
  @engine.flush(client_id) if client_id
end

#handle(message, local = false, &callback) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/faye/protocol/server.rb', line 80

def handle(message, local = false, &callback)
  return callback.call([]) if !message
  info 'Handling message: ? (local: ?)', message, local
  
  channel_name = message['channel']
  error        = message['error']
  
  return handle_meta(message, local, &callback) if Channel.meta?(channel_name)
  
  if Grammar::CHANNEL_NAME !~ channel_name
    error = Faye::Error.channel_invalid(channel_name)
  end
  
  @engine.publish(message) unless error
  
  response = make_response(message)
  response['error'] = error if error
  response['successful'] = !response['error']
  callback.call([response])
end

#handle_meta(message, local, &callback) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/faye/protocol/server.rb', line 101

def handle_meta(message, local, &callback)
  method    = Channel.parse(message['channel'])[1]
  client_id = message['clientId']
  
  __send__(method, message, local) do |responses|
    responses = [responses].flatten
    responses.each { |r| advize(r, message['connectionType']) }
    callback.call(responses)
  end
end

#handshake(message, local = false, &callback) ⇒ Object

MUST contain * version

* supportedConnectionTypes

MAY contain * minimumVersion

* ext
* id


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/faye/protocol/server.rb', line 138

def handshake(message, local = false, &callback)
  response = make_response(message)
  response['version'] = BAYEUX_VERSION
  
  response['error'] = Error.parameter_missing('version') if message['version'].nil?
  
  client_conns = message['supportedConnectionTypes']
  
  response['supportedConnectionTypes'] = CONNECTION_TYPES
  
  if client_conns
    common_conns = client_conns.select { |c| CONNECTION_TYPES.include?(c) }
    response['error'] = Error.conntype_mismatch(*client_conns) if common_conns.empty?
  else
    response['error'] = Error.parameter_missing('supportedConnectionTypes')
  end
  
  response['successful'] = response['error'].nil?
  return callback.call(response) unless response['successful']
  
  @engine.create_client do |client_id|
    response['clientId'] = client_id
    callback.call(response)
  end
end

#make_response(message) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/faye/protocol/server.rb', line 68

def make_response(message)
  response = {}
  
  response['id']       = message['id']       if message['id']
  response['clientId'] = message['clientId'] if message['clientId']
  response['channel']  = message['channel']  if message['channel']
  response['error']    = message['error']    if message['error']
  
  response['successful'] = !response['error']
  response
end

#open_socket(client_id, socket) ⇒ Object



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

def open_socket(client_id, socket)
  @engine.open_socket(client_id, socket)
end

#process(messages, local = false, &callback) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/faye/protocol/server.rb', line 32

def process(messages, local = false, &callback)
  messages = [messages].flatten
  info 'Processing messages: ? (local: ?)', messages, local

  return callback.call([]) if messages.size == 0
  processed, responses = 0, []
  
  gather_replies = lambda do |replies|
    responses.concat(replies)
    processed += 1
    responses.compact!
    info 'Returning replies: ?', responses
    callback.call(responses) if processed == messages.size
  end
  
  handle_reply = lambda do |replies|
    extended, expected = 0, replies.size
    gather_replies.call(replies) if expected == 0
    
    replies.each_with_index do |reply, i|
      debug 'Processing reply: ?', reply
      pipe_through_extensions(:outgoing, reply) do |message|
        replies[i] = message
        extended  += 1
        gather_replies.call(replies) if extended == expected
      end
    end
  end
  
  messages.each do |message|
    pipe_through_extensions(:incoming, message) do |piped_message|
      handle(piped_message, local, &handle_reply)
    end
  end
end

#subscribe(message, local = false, &callback) ⇒ Object

MUST contain * clientId

* subscription

MAY contain * ext

* id


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/faye/protocol/server.rb', line 224

def subscribe(message, local = false, &callback)
  response     = make_response(message)
  client_id    = message['clientId']
  subscription = [message['subscription']].flatten
  
  @engine.client_exists(client_id) do |exists|
    response['error'] = Error.client_unknown(client_id) unless exists
    response['error'] = Error.parameter_missing('clientId') if client_id.nil?
    response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
    
    response['subscription'] = message['subscription'] || []
    
    subscription.each do |channel|
      next if response['error']
      response['error'] = Error.channel_forbidden(channel) unless local or Channel.subscribable?(channel)
      response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel)
      
      next if response['error']
      @engine.subscribe(client_id, channel)
    end
    
    response['successful'] = response['error'].nil?
    callback.call(response)
  end
end

#unsubscribe(message, local = false, &callback) ⇒ Object

MUST contain * clientId

* subscription

MAY contain * ext

* id


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/faye/protocol/server.rb', line 254

def unsubscribe(message, local = false, &callback)
  response     = make_response(message)
  client_id    = message['clientId']
  subscription = [message['subscription']].flatten
  
  @engine.client_exists(client_id) do |exists|
    response['error'] = Error.client_unknown(client_id) unless exists
    response['error'] = Error.parameter_missing('clientId') if client_id.nil?
    response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
    
    response['subscription'] = message['subscription'] || []
    
    subscription.each do |channel|
      next if response['error']
      response['error'] = Error.channel_forbidden(channel) unless local or Channel.subscribable?(channel)
      response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel)
      
      next if response['error']
      @engine.unsubscribe(client_id, channel)
    end
    
    response['successful'] = response['error'].nil?
    callback.call(response)
  end
end