diff libervia.tac @ 127:e19a8de8b3de

radio collective first draft
author Goffi <goffi@goffi.org>
date Sun, 22 Jan 2012 19:38:05 +0100
parents f9d63624699f
children 2849ec993d89
line wrap: on
line diff
--- a/libervia.tac	Thu Jan 05 00:43:13 2012 +0100
+++ b/libervia.tac	Sun Jan 22 19:38:05 2012 +0100
@@ -43,10 +43,9 @@
 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService
 from email.mime.text import MIMEText
 from logging import debug, info, warning, error
-import re
-import glob
-import os.path
-import sys
+import re, glob
+import os.path, sys
+import tempfile, shutil, uuid
 from server_side.blog import MicroBlog
 from zope.interface import Interface, Attribute, implements
 
@@ -598,8 +597,6 @@
 
     def connectionError(self, error_type, profile):
         assert(self.register) #register must be plugged
-        import pdb
-        pdb.set_trace()
         request = self.register.getWaitingRequest(profile)
         if request: #The user is trying to log in
             if error_type == "AUTH_ERROR":
@@ -622,13 +619,36 @@
         self.request = request
         return jsonrpc.JSONRPC.render(self, request)
 
+class UploadManager(Resource):
+    """This class manage the upload of a file
+    It redirect the stream to SàT core backend"""
+    isLeaf = True
+
+    def __init__(self, sat_host):
+        self.sat_host=sat_host
+        self.upload_dir = tempfile.mkdtemp()
+        self.sat_host.addCleanup(shutil.rmtree, self.upload_dir)
+
+    def render(self, request):
+        """
+        Render method with some hacks:
+           - if login is requested, try to login with form data
+           - except login, every method is jsonrpc
+           - user doesn't need to be authentified for isRegistered, but must be for all other methods
+        """
+        filename = str(uuid.uuid4())
+        with open(os.path.join(self.upload_dir, filename),'w') as f:
+            f.write(request.args['song'][0])
+        return "OK"
 
 class Libervia(service.Service):
    
     def __init__(self):
+        self._cleanup = []
         root = ProtectedFile(LIBERVIA_DIR) 
         self.signal_handler = SignalHandler(self)
         _register = Register(self)
+        _upload = UploadManager(self)
         self.signal_handler.plugRegister(_register)
         self.sessions = {} #key = session value = user
         self.prof_connected = set() #Profiles connected
@@ -647,13 +667,15 @@
             self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name))
         #plugins
         for signal_name in ['personalEvent', 'roomJoined', 'roomUserJoined', 'roomUserLeft', 'tarotGameStarted', 'tarotGameNew', 'tarotGameChooseContrat',
-                            'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn', 'tarotGameScore']:
+                            'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn', 'tarotGameScore', 
+                            'radiocolStarted']:
             self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name), "plugin")
         self.media_dir = self.bridge.getConfig('','media_dir')
         self.local_dir = self.bridge.getConfig('','local_dir')
         root.putChild('json_signal_api', self.signal_handler)
         root.putChild('json_api', MethodHandler(self))
         root.putChild('register_api', _register)
+        root.putChild('upload', _upload)
         root.putChild('blog', MicroBlog(self))
         root.putChild('css', ProtectedFile("server_css/"))
         root.putChild(os.path.dirname(MEDIA_DIR), ProtectedFile(self.media_dir))
@@ -661,8 +683,21 @@
         self.site = server.Site(root)
         self.site.sessionFactory = LiberviaSession
 
+    def addCleanup(self, callback, *args, **kwargs):
+        """Add cleaning method to call when service is stopped
+        cleaning method will be called in reverse order of they insertion
+        @param callback: callable to call on service stop
+        @param *args: list of arguments of the callback
+        @param **kwargs: list of keyword arguments of the callback"""
+        self._cleanup.insert(0, (callback, args, kwargs))
+
     def startService(self):
         reactor.listenTCP(8080, self.site)
+
+    def stopService(self):
+        print "launching cleaning methods"
+        for callback, args, kwargs in self._cleanup:
+            callback(*args, **kwargs)
     
     def run(self):
         reactor.run()