changeset 156:46835a4e2551

server side: disconnection
author Goffi <goffi@goffi.org>
date Sat, 05 Jan 2013 16:06:33 +0100
parents 9c4550b1df3c
children b17ec3a6a112
files libervia.tac
diffstat 1 files changed, 32 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libervia.tac	Sat Jan 05 16:05:22 2013 +0100
+++ b/libervia.tac	Sat Jan 05 16:06:33 2013 +0100
@@ -148,6 +148,12 @@
         sat_session.jid = JID(self.sat_host.bridge.getParamA("JabberID", "Connection", profile_key=profile))
         return sat_session.jid.full()
         
+    def jsonrpc_disconnect(self):
+        """Disconnect the profile"""
+        sat_session = ISATSession(self.session)
+        profile = sat_session.profile
+        self.sat_host.bridge.disconnect(profile)
+    
     def jsonrpc_getContacts(self):
         """Return all passed args."""
         profile = ISATSession(self.session).profile
@@ -549,6 +555,7 @@
         self.sat_host.prof_connected.add(profile)
         
         def onExpire():
+            info ("Session expired (profile=%s)" % (profile,))
             try:
                 #We purge the queue
                 del self.sat_host.signal_handler.queue[profile]
@@ -611,10 +618,18 @@
                 #the queue is empty, we delete the profile from queue
                 del self.queue[profile]
         _session.lock() #we don't want the session to expire as long as this connection is active
-        def unlock(ignore):
+        def unlock(signal, profile):
             _session.unlock()
+            try:
+                source_defer = self.signalDeferred[profile]
+                if source_defer.called and source_defer.result[0] == "disconnected":
+                    info(u"[%s] disconnected" % (profile,))
+                    _session.expire()
+            except IndexError:
+                error("Deferred result should be a tuple with fonction name first")
+            
         self.signalDeferred[profile] = defer.Deferred()
-        self.request.notifyFinish().addBoth(unlock)
+        self.request.notifyFinish().addBoth(unlock, profile)
         return self.signalDeferred[profile]
     
     def getGenericCb(self, function_name):
@@ -639,6 +654,20 @@
         if request:
             self.register._logged(profile, request)
 
+    def disconnected(self, profile):
+        if not profile in self.sat_host.prof_connected:
+            error("'disconnected' signal received for a not connected profile")
+            return
+        self.sat_host.prof_connected.remove(profile)
+        if profile in self.signalDeferred:
+            self.signalDeferred[profile].callback(("disconnected",))
+            del self.signalDeferred[profile]
+        else:
+            if not self.queue.has_key(profile):
+                self.queue[profile] = []
+            self.queue[profile].append(("disconnected",))
+        
+
     def connectionError(self, error_type, profile):
         assert(self.register) #register must be plugged
         request = self.register.getWaitingRequest(profile)
@@ -750,6 +779,7 @@
             print(u"Can't connect to SàT backend, are you sure it's launched ?")
             sys.exit(1)
         self.bridge.register("connected", self.signal_handler.connected)
+        self.bridge.register("disconnected", self.signal_handler.disconnected)
         self.bridge.register("connectionError", self.signal_handler.connectionError)
         self.bridge.register("actionResult", self.action_handler.actionResultCb) 
         #core