diff 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
line wrap: on
line diff
--- a/libervia.tac	Wed Apr 20 02:47:22 2011 +0200
+++ b/libervia.tac	Sat May 07 23:50:55 2011 +0200
@@ -109,9 +109,12 @@
         if JID(from_jid).userhost() != _jid.userhost() and JID(to_jid) != _jid.userhost():
             error("Trying to get history from a different jid, maybe a hack attempt ?")
             return {}
-        return self.sat_host.bridge.getHistory(from_jid, to_jid, size) 
+        return self.sat_host.bridge.getHistory(from_jid, to_jid, size)
 
-
+    def jsonrpc_launchTarotGame(self, other_players):
+        """Create a room, invite the other players and start a Tarot game"""
+        profile = self.session.sat_profile
+        self.sat_host.bridge.tarotGameLaunch(other_players, profile)
 
 class Register(jsonrpc.JSONRPC):
     """This class manage the registration procedure with SàT
@@ -206,6 +209,7 @@
         self.__cleanWaiting(profile)
         _session = request.getSession()
         _session.sat_profile = profile
+        self.sat_host.prof_connected.add(profile)
         d = defer.Deferred()
         self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback)
         d.addCallback(self._fillMblogNodes, _session)
@@ -250,6 +254,7 @@
         self.register=None
         self.sat_host=sat_host
         self.signalDeferred = {}
+        self.queue = {} #XXX: gof: don't forgot to purge queue on session end
 
     def plugRegister(self, register):
         self.register = register
@@ -259,6 +264,12 @@
         @return: (signal, *signal_args)"""
         _session = self.request.getSession()
         profile = _session.sat_profile
+        if profile in self.queue: #if we have signals to send in queue
+            if self.queue[profile]:
+                return self.queue[profile].pop(0)
+            else:
+                #the queue is empty, we delete the profile from queue
+                del self.queue[profile]
         self.signalDeferred[profile] = defer.Deferred()
         return self.signalDeferred[profile]
     
@@ -267,10 +278,15 @@
         function must have profile as last argument"""
         def genericCb(*args):
             profile = args[-1]
+            if not profile in self.sat_host.prof_connected:
+                return
             if profile in self.signalDeferred:
                 self.signalDeferred[profile].callback((function_name,args[:-1]))
+                del self.signalDeferred[profile]
             else:
-                print("Warning: signal [%s] can't be sent" % function_name)
+                if not self.queue.has_key(profile):
+                    self.queue[profile] = []
+                self.queue[profile].append((function_name, args[:-1]))
         return genericCb
     
     def connected(self, profile):
@@ -313,6 +329,7 @@
         _register = Register(self)
         self.signal_handler.plugRegister(_register)
         self.sessions = {} #key = session value = user
+        self.prof_connected = set() #Profiles connected
         ## bridge ##
         try:
             self.bridge=DBusBridgeFrontend()
@@ -322,7 +339,7 @@
             sys.exit(1)
         self.bridge.register("connected", self.signal_handler.connected)
         self.bridge.register("connectionError", self.signal_handler.connectionError)
-        for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'presenceUpdate']:
+        for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'roomJoined']:
             self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name))
         root.putChild('json_signal_api', self.signal_handler)
         root.putChild('json_api', MethodHandler(self))