Class: RComet::RackAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rcomet/rack_adapter.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = nil, &block) ⇒ RackAdapter

Returns a new instance of RackAdapter.



22
23
24
25
26
27
28
29
30
31
# File 'lib/rcomet/rack_adapter.rb', line 22

def initialize(app = nil, options = nil, &block)
  @app      = app if app.respond_to?(:call)
  @options  = [app, options].grep(Hash).first
  @channels = RComet::ChannelSet.new
  @users    = Hash.new
  @timeout  = nil
  
  instance_eval(&block) if block
  return @app  
end

Instance Method Details

#call(env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rcomet/rack_adapter.rb', line 33

def call(env)
  request = Rack::Request.new(env)
  
  if request.params.empty?
    [404, {'Content-Type' => 'text/html'}, ""]
  else
    messages = JSON.parse(request.params['message'])
    jsonp    = request.params['jsonp'] || JSONP_CALLBACK
    get      = request.get?

    process( jsonp, messages, get )
  end
end

#channelObject



47
48
49
# File 'lib/rcomet/rack_adapter.rb', line 47

def channel
  @channels
end

#connect(message) ⇒ Object



110
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
138
139
140
141
142
# File 'lib/rcomet/rack_adapter.rb', line 110

def connect( message )
  # Initialize response
  response = { 
    'channel' => RComet::Channel::CONNECT,
    'clientId' => message['clientId']
  }
  response << { 'id' => message['id'] } if message.has_key?('id')

  # Get user for clientId
  user = @users[message['clientId']]
  if user
    # Ok, connect user
    user.connected = true
    time = Time.new
    response << {
      'successful'  => true,
      'timestamp'   =>"#{time.hour}:#{time.min}:#{time.sec} #{time.year}"
    }
    
    if user.has_channel?
      response = user.wait( response )
    end
    user.connected = false
  else
    # User does not exist!
    response << {
      'successful'  => false,
      'error'       => "402:#{message['clientId']}:Unknown Client ID"
    }
  end
  
  return response
end

#disconnect(message) ⇒ Object



144
145
146
147
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/rcomet/rack_adapter.rb', line 144

def disconnect( message )
  # Initialize response message
  response = {
    'channel'     => RComet::Channel::DISCONNECT,
    'clientId'    => message['clientId']
  }
  response << { 'id' => message['id'] } if message.has_key?('id')
  
  # Get user for clientId
  user = @users[message['clientId']]
  if user
    # Ok, disconnect user
    user.connected = false
    @users.delete( message['clientId'] )
    
    # Complete reponse
    response << {
      'successful'  => true
    }
  else
    # User does nit exist!
    response << {
      'successful'  => false,
      'error'       => "402:#{message['clientId']}:Unknown Client ID"
    }
  end
  
  return response
end

#handle(message) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rcomet/rack_adapter.rb', line 256

def handle( message )
  # Initialize response
  response = { 
    'channel' => message['channel']
  }
  response << { 'id' => message['id'] } if message.has_key?('id')

  c = channel[message['channel']]
  if c.nil?
    response << {
      'successful'  => false,
      'error'       => "404:#{message['channel']}:Unknown Channel"
    }
  else
    response << {
      'successful'  => true
    }
  end
        
  Thread.new do
    unless c.nil?
      if c.handler.nil?
        c.data( message['data'] )
      else
        c.handler.call( message )
      end
    end
    return
  end
  
  return response      
end

#handshake(message) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rcomet/rack_adapter.rb', line 91

def handshake( message )
  begin
    user = User.new(self)
  end while @users.has_key?(user.id)
  @users[user.id] = user
  
  response = {
    'channel'                   => RComet::Channel::HANDSHAKE,
    'version'                   => RComet::BAYEUX_VERSION,
    'minimumVersion'            => RComet::BAYEUX_VERSION,
    'supportedConnectionTypes'  => ['long-polling','callback-polling'],
    'clientId'                  => user.id,
    'successful'                => true
  }
  response << { 'id' => message['id'] } if message.has_key?('id')
  
  return response
end

#process(jsonp, messages, get) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rcomet/rack_adapter.rb', line 56

def process( jsonp, messages, get )
  replies = []
  messages.each do |message|
    reply = nil
    case message['channel'] 
      when RComet::Channel::HANDSHAKE
        reply = handshake( message )
      when RComet::Channel::CONNECT
        reply = connect( message )
      when RComet::Channel::DISCONNECT
        reply = disconnect( message )
      when RComet::Channel::SUBSCRIBE
        reply = subscribe( message )
      when RComet::Channel::UNSUBSCRIBE
        reply = unsubscribe( message )
      else
        reply = handle( message )
    end
    if reply.class == Array
      replies.concat( reply )
    else
      replies << reply
    end
  end
  
  response = JSON.generate(replies)
  type = {'Content-Type' => 'text/json'}
  if get
    response = "#{jsonp}(#{response});"
    type = {'Content-Type' => 'text/javascript'}
  end
  
  [200, type, [response]]
end

#subscribe(message) ⇒ Object



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
209
210
211
212
213
214
215
216
217
# File 'lib/rcomet/rack_adapter.rb', line 174

def subscribe( message )
  response = {
    'channel'     => RComet::Channel::SUBSCRIBE,
    'clientId'    => message['clientId']
  }
  response << { 'id' => message['id'] } if message.has_key?('id')
  
  # Get user for clientId
  user = @users[message['clientId']]
  if user
    # Get channel
    channel = @channels[message['subscription']]
    if channel
      user.subscribe( channel )
      response << {
        'successful'    => true,
        'subscription'  => message['subscription']
      }
      
      unless channel.data.nil?
        response = [response]
        response << { 
          'channel'   => message['subscription'],
          'id'        => (message['id'].to_i+1).to_s,
          'data'      => channel.data
        }
      end
    else
      #Channel doesn't exist
      response << {
        'successful'    => false,
        'subscription'  => message['subscription'],
        'error'         => "404:#{message['subscription']}:Unknown Channel"
      }
    end
  else
    response << {
      'successful'  => false,
      'error'       => "402:#{message['clientId']}:Unknown Client ID"
    }
  end

  return response
end

#timeout(&block) ⇒ Object



51
52
53
54
# File 'lib/rcomet/rack_adapter.rb', line 51

def timeout( &block )
  @handler = block if block_given?
  @handler
end

#unsubscribe(message) ⇒ Object



219
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
247
248
249
250
251
252
253
254
# File 'lib/rcomet/rack_adapter.rb', line 219

def unsubscribe( message )
  # Initialize response
  response = {
    'channel'     => RComet::Channel::UNSUBSCRIBE,
    'clientId'    => message['clientId']
  }
  response << { 'id' => message['id'] } if message.has_key?('id')

  # Get user for clientId
  user = @users[message['clientId']]
  if user
    # Get channel
    channel = @channels[message['subscription']]
    if channel
      user.unsubscribe( channel )
      response << {
        'successful'    => true,
        'subscription'  => message['subscription']
      }
    else
      #Channel doesn't exist
      response << {
        'successful'    => false,
        'subscription'  => message['subscription'],
        'error'         => "404:#{message['subscription']}:Unknown Channel"
      }
    end
  else
    response << {
      'successful'  => false,
      'error'       => "402:#{message['clientId']}:Unknown Client ID"
    }
  end
  
  return response
end