diff libervia.tac @ 2:669c531a857e

signals handling and first draft of microblogging - server side: signal handling throught json_signal_api page - browser side: - signal handling throught a json rpc call loop - first draft of microblog panel - ContactPanel put in a separate module
author Goffi <goffi@goffi.org>
date Tue, 08 Feb 2011 21:18:31 +0100
parents 0a7c685faa53
children 154d4caa57f4
line wrap: on
line diff
--- a/libervia.tac	Mon Jan 31 20:31:25 2011 +0100
+++ b/libervia.tac	Tue Feb 08 21:18:31 2011 +0100
@@ -182,13 +182,31 @@
         Resource.__init__(self)
         self.register=None
         self.sat_host=sat_host
-    
+        self.signalDeferred = None
+
     def plugRegister(self, register):
         self.register = register
+
+    def jsonrpc_getSignals(self):
+        """Keep the connection alive until a signal is received, then send it
+        @return: (signal, *signal_args)"""
+        self.signalDeferred = defer.Deferred()
+        return self.signalDeferred
+    
+    def getGenericCb(self, function_name):
+        """Return a generic function which send all params to signalDeferred.callback"""
+        def genericCb(*args):
+            if self.signalDeferred:
+                self.signalDeferred.callback((function_name,args))
+            else:
+                print("Warning: signal [%s] can't be sent" % function_name)
+        return genericCb
+    
+    def genericCallback(self, *args):
+        """"This method send all params to signalDeferred"""
+        if self.signalDeferred:
+            print ("Sending signals %s")
         
-    def presenceUpdate(self, entity, show, priority, statuses, profile):
-        print "update de",entity
-
     def connected(self, profile):
         assert(self.register) #register must be plugged
         request = self.register.getWaitingRequest(profile)
@@ -205,13 +223,27 @@
                 _error_t = "UNKNOWN"
             self.register._logginError(profile, request, _error_t)
 
+    def render(self, request):
+        """
+        Render method wich reject access if user is not identified
+        """
+        _session = request.getSession()
+        parsed = jsonrpclib.loads(request.content.read())
+        try:
+            profile = _session.sat_profile
+        except AttributeError:
+            #user is not identified, we return a jsonrpc fault
+            fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia
+            return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc'))
+        self.request = request
+        return jsonrpc.JSONRPC.render(self, request)
 
 class Libervia(service.Service):
    
     def __init__(self):
         root = File("output/") 
         self.signal_handler = SignalHandler(self)
-        root.putChild('test', self.signal_handler)
+        root.putChild('json_signal_api', self.signal_handler)
         _register = Register(self)
         self.signal_handler.plugRegister(_register)
         root.putChild('json_api', MethodHandler(self))
@@ -225,9 +257,10 @@
             print(u"Can't connect to SàT backend, are you sure it's launched ?")
             import sys
             sys.exit(1)
-        self.bridge.register("presenceUpdate", self.signal_handler.presenceUpdate)
         self.bridge.register("connected", self.signal_handler.connected)
         self.bridge.register("connectionError", self.signal_handler.connectionError)
+        for signal_name in ['presenceUpdate']:
+            self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name))
 
     def startService(self):
         reactor.listenTCP(8080, self.site)