Class: Faye::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/faye/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



3
4
5
6
7
8
# File 'lib/faye/server.rb', line 3

def initialize(options = {})
  @options  = options
  @channels = Channel::Tree.new
  @clients  = {}
  Thread.new { EventMachine.run } unless EventMachine.reactor_running?
end

Instance Method Details

#client_idsObject



205
206
207
# File 'lib/faye/server.rb', line 205

def client_ids
  @clients.keys
end

#connect(message, local = false) ⇒ Object

MUST contain * clientId

* connectionType

MAY contain * ext

* id


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/faye/server.rb', line 87

def connect(message, local = false)
  response  = { 'channel' => Channel::CONNECT,
                'id'      => message['id'] }
  
  client_id = message['clientId']
  client    = client_id ? @clients[client_id] : nil
  
  response['error'] = Error.client_unknown(client_id) if client.nil?
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
  response['error'] = Error.parameter_missing('connectionType') if message['connectionType'].nil?
  
  response['successful'] = response['error'].nil?
  return response unless response['successful']
  
  response['clientId'] = client.id
  response
end

#disconnect(message, local = false) ⇒ Object

MUST contain * clientId MAY contain * ext

* id


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/faye/server.rb', line 108

def disconnect(message, local = false)
  response  = { 'channel' => Channel::DISCONNECT,
                'id'      => message['id'] }
  
  client_id = message['clientId']
  client    = client_id ? @clients[client_id] : nil
  
  response['error'] = Error.client_unknown(client_id) if client.nil?
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
  
  response['successful'] = response['error'].nil?
  return response unless response['successful']
  
  destroy_client(client)
  
  response['clientId'] = client_id
  response
end

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



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/server.rb', line 24

def handle(message, local = false, &callback)
  client_id = message['clientId']
  channel   = message['channel']
  
  if Channel.meta?(channel)
    response = __send__(Channel.parse(channel)[1], message, local)
    
    client_id ||= response['clientId']
    response['advice'] ||= {}
    response['advice']['reconnect'] ||= @clients.has_key?(client_id) ? 'retry' : 'handshake'
    response['advice']['interval']  ||= Connection::INTERVAL * 1000
    
    response['id'] = message['id']
    
    return callback[response] unless response['channel'] == Channel::CONNECT and
                                     response['successful'] == true
    
    return connection(response['clientId']).connect do |events|
      callback[[response] + events]
    end
  end
  
  return callback[[]] if message['clientId'].nil? or Channel.service?(channel)
  
  @channels.glob(channel).each { |c| c << message }
  
  callback[ { 'channel'     => channel,
              'successful'  => true,
              'id'          => message['id']  } ]
end

#handshake(message, local = false) ⇒ Object

MUST contain * version

* supportedConnectionTypes

MAY contain * minimumVersion

* ext
* id


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/faye/server.rb', line 60

def handshake(message, local = false)
  response =  { 'channel' => Channel::HANDSHAKE,
                'version' => BAYEUX_VERSION,
                'supportedConnectionTypes' => CONNECTION_TYPES,
                'id'      => message['id'] }
  
  response['error'] = Error.parameter_missing('version') if message['version'].nil?
  
  client_conns = message['supportedConnectionTypes']
  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 response unless response['successful']
  
  response['clientId'] = generate_id
  response
end

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



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/faye/server.rb', line 10

def process(messages, local = false, &callback)
  messages = [messages].flatten
  processed, responses = 0, []
  
  messages.each do |message|
    handle(message, local) do |reply|
      reply = [reply].flatten
      responses.concat(reply)
      processed += 1
      callback[responses] if processed == messages.size
    end
  end
end

#subscribe(message, local = false) ⇒ Object

MUST contain * clientId

* subscription

MAY contain * ext

* id


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/faye/server.rb', line 131

def subscribe(message, local = false)
  response      = { 'channel'   => Channel::SUBSCRIBE,
                    'clientId'  => message['clientId'],
                    'id'        => message['id'] }
  
  client_id     = message['clientId']
  client        = client_id ? @clients[client_id] : nil
  
  subscription  = message['subscription']
  subscription  = [subscription].flatten
  
  response['error'] = Error.client_unknown(client_id) if client.nil?
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
  response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
  
  response['subscription'] = subscription.compact
  
  subscription.each do |channel|
    next if response['error']
    response['error'] = Error.channel_forbidden(channel) unless Channel.subscribable?(channel)
    response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel)
    
    next if response['error']
    channel = @channels[channel] ||= Channel.new(channel)
    client.subscribe(channel)
  end
  
  response['successful'] = response['error'].nil?
  response
end

#unsubscribe(message, local = false) ⇒ Object

MUST contain * clientId

* subscription

MAY contain * ext

* id


166
167
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
# File 'lib/faye/server.rb', line 166

def unsubscribe(message, local = false)
  response      = { 'channel'   => Channel::UNSUBSCRIBE,
                    'clientId'  => message['clientId'],
                    'id'        => message['id'] }
  
  client_id     = message['clientId']
  client        = client_id ? @clients[client_id] : nil
  
  subscription  = message['subscription']
  subscription  = [subscription].flatten
  
  response['error'] = Error.client_unknown(client_id) if client.nil?
  response['error'] = Error.parameter_missing('clientId') if client_id.nil?
  response['error'] = Error.parameter_missing('subscription') if message['subscription'].nil?
  
  response['subscription'] = subscription.compact
  
  subscription.each do |channel|
    next if response['error']
    
    if not Channel.valid?(channel)
      response['error'] = Error.channel_invalid(channel)
      next
    end
    
    channel = @channels[channel]
    client.unsubscribe(channel) if channel
  end
  
  response['successful'] = response['error'].nil?
  response
end

#update(message, client) ⇒ Object

Notifies the server of stale connections that should be deleted



200
201
202
203
# File 'lib/faye/server.rb', line 200

def update(message, client)
  return unless message == :stale_client
  destroy_client(client)
end