Class: Faye::Client

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

Constant Summary collapse

UNCONNECTED =
1
CONNECTING =
2
CONNECTED =
3
DISCONNECTED =
4
HANDSHAKE =
'handshake'
RETRY =
'retry'
NONE =
'none'
CONNECTION_TIMEOUT =
60.0

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(endpoint = nil, options = {}) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/faye/protocol/client.rb', line 21

def initialize(endpoint = nil, options = {})
  info('New client created for ?', endpoint)
  
  @endpoint  = endpoint || RackAdapter::DEFAULT_ENDPOINT
  @options   = options
  
  @transport = Transport.get(self, MANDATORY_CONNECTION_TYPES)
  @state     = UNCONNECTED
  @channels  = Channel::Set.new
  
  @namespace = Namespace.new
  @response_callbacks = {}
  
  @advice = {
    'reconnect' => RETRY,
    'interval'  => 1000.0 * (@options[:interval] || Engine::INTERVAL),
    'timeout'   => 1000.0 * (@options[:timeout]  || CONNECTION_TIMEOUT)
  }
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



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

def endpoint
  @endpoint
end

Instance Method Details

#connect(&block) ⇒ Object

Request Response MUST include: * channel MUST include: * channel

* clientId                           * successful
* connectionType                     * clientId

MAY include: * ext MAY include: * error

* id                                 * advice
                                     * ext
                                     * id
                                     * timestamp


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/faye/protocol/client.rb', line 111

def connect(&block)
  return if @advice['reconnect'] == NONE or
            @state == DISCONNECTED
  
  return handshake { connect(&block) } if @state == UNCONNECTED
  
  callback(&block)
  return unless @state == CONNECTED
  
  info('Calling deferred actions for ?', @client_id)
  set_deferred_status(:succeeded)
  set_deferred_status(:deferred)
  
  return unless @connect_request.nil?
  @connect_request = true
  
  info('Initiating connection for ?', @client_id)
  
  send({
    'channel'         => Channel::CONNECT,
    'clientId'        => @client_id,
    'connectionType'  => @transport.connection_type
    
  }) do
    cycle_connection
  end
end

#disconnectObject

Request Response MUST include: * channel MUST include: * channel

* clientId                           * successful

MAY include: * ext * clientId

* id                  MAY include:   * error
                                     * ext
                                     * id


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/faye/protocol/client.rb', line 146

def disconnect
  return unless @state == CONNECTED
  @state = DISCONNECTED
  
  info('Disconnecting ?', @client_id)
  
  send({
    'channel'   => Channel::DISCONNECT,
    'clientId'  => @client_id
  })
  
  info('Clearing channel listeners for ?', @client_id)
  @channels = Channel::Set.new
end

#handshake(&block) ⇒ Object

Request MUST include: * channel

* version
* supportedConnectionTypes

MAY include: * minimumVersion

* ext
* id

Success Response Failed Response MUST include: * channel MUST include: * channel

* version                                    * successful
* supportedConnectionTypes                   * error
* clientId                    MAY include:   * supportedConnectionTypes
* successful                                 * advice

MAY include: * minimumVersion * version

* advice                                     * minimumVersion
* ext                                        * ext
* id                                         * id
* authSuccessful


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/faye/protocol/client.rb', line 69

def handshake(&block)
  return if @advice['reconnect'] == NONE
  return if @state != UNCONNECTED
  
  @state = CONNECTING
  
  info('Initiating handshake with ?', @endpoint)
  
  send({
    'channel'     => Channel::HANDSHAKE,
    'version'     => BAYEUX_VERSION,
    'supportedConnectionTypes' => [@transport.connection_type]
    
  }) do |response|
    
    if response['successful']
      @state     = CONNECTED
      @client_id = response['clientId']
      @transport = Transport.get(self, response['supportedConnectionTypes'])
      
      info('Handshake successful: ?', @client_id)
      
      subscribe(@channels.keys, true)
      block.call if block_given?
      
    else
      info('Handshake unsuccessful')
      EventMachine.add_timer(@advice['interval'] / 1000.0) { handshake(&block) }
      @state = UNCONNECTED
    end
  end
end

#publish(channel, data) ⇒ Object

Request Response MUST include: * channel MUST include: * channel

* data                               * successful

MAY include: * clientId MAY include: * id

* id                                 * error
* ext                                * ext


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/faye/protocol/client.rb', line 254

def publish(channel, data)
  unless Grammar::CHANNEL_NAME =~ channel
    raise "Cannot publish: '#{channel}' is not a valid channel name"
  end
  
  connect {
    info('Client ? queueing published message to ?: ?', @client_id, channel, data)
    
    send({
      'channel'   => channel,
      'data'      => data,
      'clientId'  => @client_id
    })
  }
end

#receive_message(message) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/faye/protocol/client.rb', line 270

def receive_message(message)
  pipe_through_extensions(:incoming, message) do |message|
    if message
      handle_advice(message['advice']) if message['advice']
      
      callback = @response_callbacks[message['id']]
      if callback
        @response_callbacks.delete(message['id'])
        callback.call(message)
      end
      
      deliver_message(message)
    end
  end
end

#stateObject



41
42
43
44
45
46
47
48
# File 'lib/faye/protocol/client.rb', line 41

def state
  case @state
  when UNCONNECTED  then :UNCONNECTED
  when CONNECTING   then :CONNECTING
  when CONNECTED    then :CONNECTED
  when DISCONNECTED then :DISCONNECTED
  end
end

#subscribe(channels, force = false, &block) ⇒ Object

Request Response MUST include: * channel MUST include: * channel

* clientId                           * successful
* subscription                       * clientId

MAY include: * ext * subscription

* id                  MAY include:   * error
                                     * advice
                                     * ext
                                     * id
                                     * timestamp


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
200
201
202
203
204
205
206
207
208
# File 'lib/faye/protocol/client.rb', line 171

def subscribe(channels, force = false, &block)
  if Array === channels
    return channels.each do |channel|
      subscribe(channel, force, &block)
    end
  end
  
  subscription = Subscription.new(self, channels, block)
  
  if not force and @channels.has_subscription?(channels)
    @channels.subscribe([channels], block)
    subscription.set_deferred_status(:succeeded)
    return subscription
  end
  
  connect {
    info('Client ? attempting to subscribe to ?', @client_id, channels)
    
    send({
      'channel'       => Channel::SUBSCRIBE,
      'clientId'      => @client_id,
      'subscription'  => channels
      
    }) do |response|
      if response['successful']
        
        channels = [response['subscription']].flatten
        info('Subscription acknowledged for ? to ?', @client_id, channels)
        @channels.subscribe(channels, block)
        
        subscription.set_deferred_status(:succeeded)
      else
        subscription.set_deferred_status(:failed, Error.parse(response['error']))
      end
    end
  }
  subscription
end

#unsubscribe(channels, &block) ⇒ Object

Request Response MUST include: * channel MUST include: * channel

* clientId                           * successful
* subscription                       * clientId

MAY include: * ext * subscription

* id                  MAY include:   * error
                                     * advice
                                     * ext
                                     * id
                                     * timestamp


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

def unsubscribe(channels, &block)
  if Array === channels
    return channels.each do |channel|
      unsubscribe(channel, &block)
    end
  end
  
  dead = @channels.unsubscribe(channels, block)
  return unless dead
  
  connect {
    info('Client ? attempting to unsubscribe from ?', @client_id, channels)
    
    send({
      'channel'       => Channel::UNSUBSCRIBE,
      'clientId'      => @client_id,
      'subscription'  => channels
      
    }) do |response|
      if response['successful']
        
        channels = [response['subscription']].flatten
        info('Unsubscription acknowledged for ? from ?', @client_id, channels)
      end
    end
  }
end