Mercurial > libervia-web
comparison libervia.tac @ 24:28e203f13144
server side: misc stuff
- roomJoined signal is now managed
- profiles currently connected are saved
- signals are now filetered (ignored if the profile has not an active session)
- a queue manage signals, to not loose them when the session is active, but the jsonrpc call is not active yet
- launchTarotGame method added
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 07 May 2011 23:50:55 +0200 |
parents | 8f4b1a8914c3 |
children | 7684e3ceb12d |
comparison
equal
deleted
inserted
replaced
23:0ce2a57b34ca | 24:28e203f13144 |
---|---|
107 error("No jid saved for this profile") | 107 error("No jid saved for this profile") |
108 return {} | 108 return {} |
109 if JID(from_jid).userhost() != _jid.userhost() and JID(to_jid) != _jid.userhost(): | 109 if JID(from_jid).userhost() != _jid.userhost() and JID(to_jid) != _jid.userhost(): |
110 error("Trying to get history from a different jid, maybe a hack attempt ?") | 110 error("Trying to get history from a different jid, maybe a hack attempt ?") |
111 return {} | 111 return {} |
112 return self.sat_host.bridge.getHistory(from_jid, to_jid, size) | 112 return self.sat_host.bridge.getHistory(from_jid, to_jid, size) |
113 | 113 |
114 | 114 def jsonrpc_launchTarotGame(self, other_players): |
115 """Create a room, invite the other players and start a Tarot game""" | |
116 profile = self.session.sat_profile | |
117 self.sat_host.bridge.tarotGameLaunch(other_players, profile) | |
115 | 118 |
116 class Register(jsonrpc.JSONRPC): | 119 class Register(jsonrpc.JSONRPC): |
117 """This class manage the registration procedure with SàT | 120 """This class manage the registration procedure with SàT |
118 It provide an api for the browser, check password and setup the web server""" | 121 It provide an api for the browser, check password and setup the web server""" |
119 | 122 |
204 """Set everything when a user just logged | 207 """Set everything when a user just logged |
205 and return "LOGGED" to the requester""" | 208 and return "LOGGED" to the requester""" |
206 self.__cleanWaiting(profile) | 209 self.__cleanWaiting(profile) |
207 _session = request.getSession() | 210 _session = request.getSession() |
208 _session.sat_profile = profile | 211 _session.sat_profile = profile |
212 self.sat_host.prof_connected.add(profile) | |
209 d = defer.Deferred() | 213 d = defer.Deferred() |
210 self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback) | 214 self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback) |
211 d.addCallback(self._fillMblogNodes, _session) | 215 d.addCallback(self._fillMblogNodes, _session) |
212 if finish: | 216 if finish: |
213 request.write('LOGGED') | 217 request.write('LOGGED') |
248 def __init__(self, sat_host): | 252 def __init__(self, sat_host): |
249 Resource.__init__(self) | 253 Resource.__init__(self) |
250 self.register=None | 254 self.register=None |
251 self.sat_host=sat_host | 255 self.sat_host=sat_host |
252 self.signalDeferred = {} | 256 self.signalDeferred = {} |
257 self.queue = {} #XXX: gof: don't forgot to purge queue on session end | |
253 | 258 |
254 def plugRegister(self, register): | 259 def plugRegister(self, register): |
255 self.register = register | 260 self.register = register |
256 | 261 |
257 def jsonrpc_getSignals(self): | 262 def jsonrpc_getSignals(self): |
258 """Keep the connection alive until a signal is received, then send it | 263 """Keep the connection alive until a signal is received, then send it |
259 @return: (signal, *signal_args)""" | 264 @return: (signal, *signal_args)""" |
260 _session = self.request.getSession() | 265 _session = self.request.getSession() |
261 profile = _session.sat_profile | 266 profile = _session.sat_profile |
267 if profile in self.queue: #if we have signals to send in queue | |
268 if self.queue[profile]: | |
269 return self.queue[profile].pop(0) | |
270 else: | |
271 #the queue is empty, we delete the profile from queue | |
272 del self.queue[profile] | |
262 self.signalDeferred[profile] = defer.Deferred() | 273 self.signalDeferred[profile] = defer.Deferred() |
263 return self.signalDeferred[profile] | 274 return self.signalDeferred[profile] |
264 | 275 |
265 def getGenericCb(self, function_name): | 276 def getGenericCb(self, function_name): |
266 """Return a generic function which send all params to signalDeferred.callback | 277 """Return a generic function which send all params to signalDeferred.callback |
267 function must have profile as last argument""" | 278 function must have profile as last argument""" |
268 def genericCb(*args): | 279 def genericCb(*args): |
269 profile = args[-1] | 280 profile = args[-1] |
281 if not profile in self.sat_host.prof_connected: | |
282 return | |
270 if profile in self.signalDeferred: | 283 if profile in self.signalDeferred: |
271 self.signalDeferred[profile].callback((function_name,args[:-1])) | 284 self.signalDeferred[profile].callback((function_name,args[:-1])) |
285 del self.signalDeferred[profile] | |
272 else: | 286 else: |
273 print("Warning: signal [%s] can't be sent" % function_name) | 287 if not self.queue.has_key(profile): |
288 self.queue[profile] = [] | |
289 self.queue[profile].append((function_name, args[:-1])) | |
274 return genericCb | 290 return genericCb |
275 | 291 |
276 def connected(self, profile): | 292 def connected(self, profile): |
277 assert(self.register) #register must be plugged | 293 assert(self.register) #register must be plugged |
278 request = self.register.getWaitingRequest(profile) | 294 request = self.register.getWaitingRequest(profile) |
311 root = File("output/") | 327 root = File("output/") |
312 self.signal_handler = SignalHandler(self) | 328 self.signal_handler = SignalHandler(self) |
313 _register = Register(self) | 329 _register = Register(self) |
314 self.signal_handler.plugRegister(_register) | 330 self.signal_handler.plugRegister(_register) |
315 self.sessions = {} #key = session value = user | 331 self.sessions = {} #key = session value = user |
332 self.prof_connected = set() #Profiles connected | |
316 ## bridge ## | 333 ## bridge ## |
317 try: | 334 try: |
318 self.bridge=DBusBridgeFrontend() | 335 self.bridge=DBusBridgeFrontend() |
319 except BridgeExceptionNoService: | 336 except BridgeExceptionNoService: |
320 print(u"Can't connect to SàT backend, are you sure it's launched ?") | 337 print(u"Can't connect to SàT backend, are you sure it's launched ?") |
321 import sys | 338 import sys |
322 sys.exit(1) | 339 sys.exit(1) |
323 self.bridge.register("connected", self.signal_handler.connected) | 340 self.bridge.register("connected", self.signal_handler.connected) |
324 self.bridge.register("connectionError", self.signal_handler.connectionError) | 341 self.bridge.register("connectionError", self.signal_handler.connectionError) |
325 for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'presenceUpdate']: | 342 for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'roomJoined']: |
326 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) | 343 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
327 root.putChild('json_signal_api', self.signal_handler) | 344 root.putChild('json_signal_api', self.signal_handler) |
328 root.putChild('json_api', MethodHandler(self)) | 345 root.putChild('json_api', MethodHandler(self)) |
329 root.putChild('register_api', _register) | 346 root.putChild('register_api', _register) |
330 root.putChild('blog', MicroBlog(self)) | 347 root.putChild('blog', MicroBlog(self)) |