Class: Faye::Server
- Inherits:
-
Object
- Object
- Faye::Server
- Includes:
- Extensible, Logging
- Defined in:
- lib/faye/protocol/server.rb,
lib/faye/protocol/socket.rb
Defined Under Namespace
Classes: Socket
Constant Summary collapse
- META_METHODS =
%w[handshake connect disconnect subscribe unsubscribe]
Constants included from Logging
Instance Attribute Summary collapse
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
Instance Method Summary collapse
- #advize(response, connection_type) ⇒ Object
- #close ⇒ Object
- #close_socket(client_id, close = true) ⇒ Object
-
#connect(message, local = false, &callback) ⇒ Object
MUST contain * clientId * connectionType MAY contain * ext * id.
-
#disconnect(message, local = false, &callback) ⇒ Object
MUST contain * clientId MAY contain * ext * id.
- #handle(message, local = false, &callback) ⇒ Object
- #handle_meta(message, local, &callback) ⇒ Object
-
#handshake(message, local = false, &callback) ⇒ Object
MUST contain * version * supportedConnectionTypes MAY contain * minimumVersion * ext * id.
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #make_response(message) ⇒ Object
- #open_socket(client_id, socket, env) ⇒ Object
- #process(messages, env, &callback) ⇒ Object
-
#subscribe(message, local = false, &callback) ⇒ Object
MUST contain * clientId * subscription MAY contain * ext * id.
-
#unsubscribe(message, local = false, &callback) ⇒ Object
MUST contain * clientId * subscription MAY contain * ext * id.
Methods included from Extensible
#add_extension, #pipe_through_extensions, #remove_extension
Constructor Details
#initialize(options = {}) ⇒ Server
Returns a new instance of Server.
13 14 15 16 17 18 19 20 |
# File 'lib/faye/protocol/server.rb', line 13 def initialize( = {}) @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
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
11 12 13 |
# File 'lib/faye/protocol/server.rb', line 11 def engine @engine end |
Instance Method Details
#advize(response, connection_type) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/faye/protocol/server.rb', line 122 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 ⇒ Object
22 23 24 |
# File 'lib/faye/protocol/server.rb', line 22 def close @engine.close end |
#close_socket(client_id, close = true) ⇒ Object
31 32 33 |
# File 'lib/faye/protocol/server.rb', line 31 def close_socket(client_id, close = true) @engine.flush_connection(client_id, close) end |
#connect(message, local = false, &callback) ⇒ Object
MUST contain * clientId
* connectionType
MAY contain * ext
* id
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/faye/protocol/server.rb', line 178 def connect(, local = false, &callback) response = make_response() client_id = ['clientId'] connection_type = ['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 ['connectionType'] == 'eventsource' ['advice'] ||= {} ['advice']['timeout'] = 0 end @engine.connect(response['clientId'], ['advice']) do |events| callback.call([response] + events) end end end |
#disconnect(message, local = false, &callback) ⇒ Object
MUST contain * clientId MAY contain * ext
* id
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/faye/protocol/server.rb', line 214 def disconnect(, local = false, &callback) response = make_response() client_id = ['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 |
#handle(message, local = false, &callback) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/faye/protocol/server.rb', line 84 def handle(, local = false, &callback) return callback.call([]) if ! info('Handling message: ? (local: ?)', , local) channel_name = ['channel'] error = ['error'] return (, local, &callback) if Channel.(channel_name) if Grammar::CHANNEL_NAME !~ channel_name error = Faye::Error.channel_invalid(channel_name) end @engine.publish() unless error response = make_response() response['error'] = error if error response['successful'] = !response['error'] callback.call([response]) end |
#handle_meta(message, local, &callback) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/faye/protocol/server.rb', line 105 def (, local, &callback) method = Channel.parse(['channel'])[1] unless META_METHODS.include?(method) response = make_response() response['error'] = Faye::Error.channel_forbidden(['channel']) response['successful'] = false return callback.call([response]) end __send__(method, , local) do |responses| responses = [responses].flatten responses.each { |r| advize(r, ['connectionType']) } callback.call(responses) end end |
#handshake(message, local = false, &callback) ⇒ Object
MUST contain * version
* supportedConnectionTypes
MAY contain * minimumVersion
* ext
* id
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/faye/protocol/server.rb', line 148 def handshake(, local = false, &callback) response = make_response() response['version'] = BAYEUX_VERSION response['error'] = Error.parameter_missing('version') if ['version'].nil? client_conns = ['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
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/faye/protocol/server.rb', line 72 def make_response() response = {} response['id'] = ['id'] if ['id'] response['clientId'] = ['clientId'] if ['clientId'] response['channel'] = ['channel'] if ['channel'] response['error'] = ['error'] if ['error'] response['successful'] = !response['error'] response end |
#open_socket(client_id, socket, env) ⇒ Object
26 27 28 29 |
# File 'lib/faye/protocol/server.rb', line 26 def open_socket(client_id, socket, env) return unless client_id and socket @engine.open_socket(client_id, Socket.new(self, socket, env)) end |
#process(messages, env, &callback) ⇒ Object
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 67 68 69 70 |
# File 'lib/faye/protocol/server.rb', line 35 def process(, env, &callback) local = env.nil? = [].flatten info('Processing messages: ? (local: ?)', , local) return callback.call([]) if .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 == .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, env) do || replies[i] = extended += 1 gather_replies.call(replies) if extended == expected end end end .each do || pipe_through_extensions(:incoming, , env) do || handle(, local, &handle_reply) end end end |
#subscribe(message, local = false, &callback) ⇒ Object
MUST contain * clientId
* subscription
MAY contain * ext
* id
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/faye/protocol/server.rb', line 234 def subscribe(, local = false, &callback) response = make_response() client_id = ['clientId'] subscription = [['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 ['subscription'].nil? response['subscription'] = ['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
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/faye/protocol/server.rb', line 264 def unsubscribe(, local = false, &callback) response = make_response() client_id = ['clientId'] subscription = [['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 ['subscription'].nil? response['subscription'] = ['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 |