Author: remi
Date: 2009-03-16 16:16:53 +0100 (Mon, 16 Mar 2009)
New Revision: 4087

Modified:
   software_suite_v2/tuxware/tuxdroidserver/trunk/util/gadgets/Framework.py
Log:
* renamed and object "__gadgetFramework" -> "__frameworkBridge"
* added web gadget generators
* added a system to kill child pid of gadgets

Modified: 
software_suite_v2/tuxware/tuxdroidserver/trunk/util/gadgets/Framework.py
===================================================================
--- software_suite_v2/tuxware/tuxdroidserver/trunk/util/gadgets/Framework.py    
2009-03-16 12:50:49 UTC (rev 4086)
+++ software_suite_v2/tuxware/tuxdroidserver/trunk/util/gadgets/Framework.py    
2009-03-16 15:16:53 UTC (rev 4087)
@@ -5,11 +5,33 @@
 
 import threading
 import time
+import os
 
 from FrameworkBridge import FrameworkBridge
 from GadgetsContainer import GadgetsContainer
 from util.logger.SimpleLogger import *
 
+GOOGLE_GADGET_BASE_CODE = '''<?xml version="1.0" encoding="UTF-8"?>
+<Module>
+<ModulePrefs
+    title="Tux Droid : %s"
+    title_url="http://www.kysoh.com";
+    height="300"
+    scaling="false"
+    author="%s"
+    author_link="http://www.kysoh.com";
+    description="%s"
+    screenshot="http://127.0.0.1:270%s";
+    thumbnail="http://127.0.0.1:270%s";>
+</ModulePrefs>
+<Content type="html"><![CDATA[
+<body>
+    <iframe src='%s' width=800 height=300 frameborder=0 scrolling=false>
+</body>
+]]></Content>
+</Module>
+'''
+
 # 
------------------------------------------------------------------------------
 # Gadget description.
 # 
------------------------------------------------------------------------------
@@ -19,8 +41,8 @@
     # Constructor of the class.
     # 
--------------------------------------------------------------------------
     def __init__(self):
-        self.__gadgetFramework = FrameworkBridge()
-        self.__gadgetFramework.setOnEventCallback(self.__onFrameworkEvent)
+        self.__frameworkBridge = FrameworkBridge()
+        self.__frameworkBridge.setOnEventCallback(self.__onFrameworkEvent)
         self.__onFrameworkStartingCallBack = None
         self.__onFrameworkStoppedCallBack = None
         self.__onContainerDeployedCallback = None
@@ -49,6 +71,15 @@
         self.__logger.resetLog()
 
     # 
--------------------------------------------------------------------------
+    # Get the pipe bridge to the java framework.
+    # 
--------------------------------------------------------------------------
+    def getFrameworkBridge(self):
+        """Get the pipe bridge to the java framework.
+        @return: A FrameworkBridge object.
+        """
+        return self.__frameworkBridge
+
+    # 
--------------------------------------------------------------------------
     # Start the gadget framework.
     # 
--------------------------------------------------------------------------
     def start(self, gadgetsPath, language, country, locutor, pitch):
@@ -63,9 +94,10 @@
         """
         def async():
             self.__startMutex.acquire()
+            self.stopAllGadgets()
             self.__logger.logInfo("[Framework] Starting : Begin")
             self.__logger.logDebug("[Framework] Stop the previous instance")
-            self.__gadgetFramework.stop()
+            self.__frameworkBridge.stop()
             self.__logger.logDebug("[Framework] Previous instance stopped")
             if self.__onFrameworkStoppedCallBack != None:
                 self.__onFrameworkStoppedCallBack()
@@ -75,7 +107,7 @@
             self.__currentGadgetLoadState = "HEAD"
             self.__containerDirectory = None
             self.__logger.logDebug("[Framework] Start the new instance")
-            self.__gadgetFramework.start()
+            self.__frameworkBridge.start()
             self.__logger.logDebug("[Framework] New instance started")
             time.sleep(0.5)
             self.__language = language
@@ -99,7 +131,7 @@
         """Stop the gadget framework.
         """
         self.__logger.logInfo("[Framework] Stop")
-        self.__gadgetFramework.stop()
+        self.__frameworkBridge.stop()
 
     # 
--------------------------------------------------------------------------
     # Get if the framework is started or not.
@@ -108,7 +140,7 @@
         """Get if the framework is started or not.
         @return: True or False.
         """
-        return self.__gadgetFramework.isStarted()
+        return self.__frameworkBridge.isStarted()
 
     # 
--------------------------------------------------------------------------
     # Update the local values of the gadget framework.
@@ -122,7 +154,7 @@
         @return: The success.
         - The framework bridge must be previously started.
         """
-        if (self.__gadgetFramework.isStarted()):
+        if (self.__frameworkBridge.isStarted()):
             self.__language = language
             self.__country = country
             self.__locutor = locutor
@@ -296,7 +328,7 @@
         - If a parameter is wrong the default one is set.
         - If a parameter is missing the default one is set.
         """
-        if not self.__gadgetFramework.isStarted():
+        if not self.__frameworkBridge.isStarted():
             return False
         cmd = "GADGET"
         subCmd = "START_BY_UUID"
@@ -304,13 +336,96 @@
         for key in parameters:
             args.append(key)
             args.append(parameters[key])
-        ret = self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        ret = self.__frameworkBridge.sendCommand(cmd, subCmd, args)
         if ret[0] == "FAILED":
             return False
         else:
             return True
 
     # 
--------------------------------------------------------------------------
+    # Generate a web gadget structure.
+    # 
--------------------------------------------------------------------------
+    def generateWebGadgetStructureByUuid(self, gadgetUuid, gadgetCommand,
+        parameters = {}):
+        """Generate a web gadget structure.
+        @param gadgetUuid: Gadget uuid.
+        @param gadgetCommand: Gadget command.
+        @param parameters: Parameters dictionary.
+        """
+        if not self.__frameworkBridge.isStarted():
+            return None
+        gadget = self.getGadgetsContainer().getGadgetByUuid(gadgetUuid)
+        if gadget == None:
+            return None
+        import copy
+        structure = copy.deepcopy(gadget.getStructure())
+        del structure['commands']
+        # Serialize enum values
+        for key in structure['parameters']:
+            param = structure['parameters'][key]
+            if param['type'] == 'enum':
+                values = param['enum_values'].split(",")
+                enums = {}
+                for i, value in enumerate(values):
+                    dName = "enum_%.2d" % i
+                    if value != '':
+                        enums[dName] = value
+                param['enum_values'] = enums
+        # Insert the command
+        structure['command'] = gadgetCommand
+        # Fill the parameters
+        for key in parameters:
+            if key in gadget.getParametersName():
+                for sKey in structure['parameters']:
+                    param = structure['parameters'][sKey]
+                    if param['name'] == key:
+                        param['default_value'] = parameters[key]
+                        param['category'] = 'internals'
+        return structure
+
+    # 
--------------------------------------------------------------------------
+    # Get the url for a web gadget.
+    # 
--------------------------------------------------------------------------
+    def getWebGadgetUrl(self, gadgetUuid, gadgetCommand,
+        parameters = {}):
+        """Get the url for a web gadget.
+        @param gadgetUuid: Gadget uuid.
+        @param gadgetCommand: Gadget command.
+        @param parameters: Parameters dictionary.
+        """
+        params = ""
+        for key in parameters:
+            params += "%s=%s|" % (key, parameters[key])
+        url = 
"http://127.0.0.1:270/gadget_framework/web_gadget?uuid=%s&command=%s&parameters=%s";
 % (
+            gadgetUuid, gadgetCommand, params)
+        return url
+
+    # 
--------------------------------------------------------------------------
+    # Generate the code of a google gadget.
+    # 
--------------------------------------------------------------------------
+    def generateGoogleGadgetCode(self, gadgetUuid, gadgetCommand,
+        parameters = {}):
+        """Generate the code of a google gadget.
+        @param gadgetUuid: Gadget uuid.
+        @param gadgetCommand: Gadget command.
+        @param parameters: Parameters dictionary.
+        """
+        if not self.__frameworkBridge.isStarted():
+            return None
+        gadget = self.getGadgetsContainer().getGadgetByUuid(gadgetUuid)
+        if gadget == None:
+            return None
+        url = self.getWebGadgetUrl(gadgetUuid, gadgetCommand, parameters)
+        result = GOOGLE_GADGET_BASE_CODE % (
+            gadget.getDescription().getName(),
+            gadget.getDescription().getAuthor(),
+            gadget.getDescription().getDescription(),
+            gadget.getDescription().getIconFile(),
+            gadget.getDescription().getIconFile(),
+            url)
+        return result
+
+    # 
--------------------------------------------------------------------------
     # Start a gadget by it name.
     # 
--------------------------------------------------------------------------
     def startGadgetByName(self, gadgetName, gadgetCommand, parameters = {}):
@@ -324,7 +439,7 @@
         - If a parameter is wrong the default one is set.
         - If a parameter is missing the default one is set.
         """
-        if not self.__gadgetFramework.isStarted():
+        if not self.__frameworkBridge.isStarted():
             return False
         cmd = "GADGET"
         subCmd = "START_BY_NAME"
@@ -332,13 +447,41 @@
         for key in parameters:
             args.append(key)
             args.append(parameters[key])
-        ret = self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        ret = self.__frameworkBridge.sendCommand(cmd, subCmd, args)
         if ret[0] == "FAILED":
             return False
         else:
             return True
 
     # 
--------------------------------------------------------------------------
+    # Kill sub-process of a gadget.
+    # 
--------------------------------------------------------------------------
+    def __killSubProcessOfAGadget(self, gadgetUuid):
+        """Kill sub-process of a gadget.
+        @param gadgetUuid: Gadget uuid.
+        """
+        if not self.__frameworkBridge.isStarted():
+            return
+        gadget = self.getGadgetsContainer().getGadgetByUuid(gadgetUuid)
+        if gadget == None:
+            return
+        workingPath = gadget.getDescription().getWorkingPath()
+        if not os.path.isdir(workingPath):
+            return
+        for path in os.listdir(workingPath):
+            if path.find(".pid") != -1:
+                pidFile = os.path.join(workingPath, path)
+                pid = open(pidFile, 'r').read()
+                if os.name == 'nt':
+                    os.popen("TASKKILL /PID %s /F" % pid)
+                else:
+                    os.system("kill -9 " + str(pid))
+                try:
+                    os.remove(pidFile)
+                except:
+                    pass
+
+    # 
--------------------------------------------------------------------------
     # Stop a gadget by it uuid.
     # 
--------------------------------------------------------------------------
     def stopGadgetByUuid(self, gadgetUuid):
@@ -346,15 +489,16 @@
         @param gadgetUuid: Gadget uuid.
         @return: The success.
         """
-        if not self.__gadgetFramework.isStarted():
+        if not self.__frameworkBridge.isStarted():
             return False
         cmd = "GADGET"
         subCmd = "STOP_BY_UUID"
         args = [gadgetUuid,]
-        ret = self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        ret = self.__frameworkBridge.sendCommand(cmd, subCmd, args)
         if ret[0] != "SUCCESS":
             return False
         else:
+            self.__killSubProcessOfAGadget(gadgetUuid)
             return True
 
     # 
--------------------------------------------------------------------------
@@ -365,15 +509,18 @@
         @param gadgetName: Gadget name.
         @return: The success.
         """
-        if not self.__gadgetFramework.isStarted():
+        if not self.__frameworkBridge.isStarted():
             return False
         cmd = "GADGET"
         subCmd = "STOP_BY_NAME"
         args = [gadgetName,]
-        ret = self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        ret = self.__frameworkBridge.sendCommand(cmd, subCmd, args)
         if ret[0] != "SUCCESS":
             return False
         else:
+            gadget = self.getGadgetsContainer().getGadgetByName(gadgetName)
+            if gadget != None:
+                
self.__killSubProcessOfAGadget(gadget.getDescription().getUuid())
             return True
 
     # 
--------------------------------------------------------------------------
@@ -383,14 +530,16 @@
         """Stop all started gadgets.
         @return: The success.
         """
-        if not self.__gadgetFramework.isStarted():
+        if not self.__frameworkBridge.isStarted():
             return False
         cmd = "GADGET"
         subCmd = "STOP_ALL"
-        ret = self.__gadgetFramework.sendCommand(cmd, subCmd)
+        ret = self.__frameworkBridge.sendCommand(cmd, subCmd)
         if ret[0] != "SUCCESS":
             return False
         else:
+            for gadget in self.getGadgetsContainer().getGadgets():
+                
self.__killSubProcessOfAGadget(gadget.getDescription().getUuid())
             return True
 
     # 
--------------------------------------------------------------------------
@@ -470,6 +619,7 @@
                     self.__currentGadgetLoad['description']['icon_file'] = 
args[6]
                     self.__currentGadgetLoad['description']['help_file'] = 
args[7]
                     self.__currentGadgetLoad['description']['tgf_file'] = 
args[8]
+                    self.__currentGadgetLoad['description']['original'] = 
"true"
                     self.__logger.logDebug("[Framework] Parse gadget 
description : End")
                     return
             elif self.__currentGadgetLoadState == "GETPARAMETERS":
@@ -579,7 +729,7 @@
         subCmd = "START"
         args = [gadgetsPath]
         self.__logger.logInfo("[Framework] Load a gadget directory (%s)" % 
gadgetsPath)
-        return self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        return self.__frameworkBridge.sendCommand(cmd, subCmd, args)
 
     # 
--------------------------------------------------------------------------
     #
@@ -590,7 +740,7 @@
         self.__logger.logInfo("[Framework] Set the locales (%s, %s, %s, %s)" % 
(
                 language, country, locutor, pitch))
         args = [language, country, locutor, str(pitch)]
-        return self.__gadgetFramework.sendCommand(cmd, subCmd, args)
+        return self.__frameworkBridge.sendCommand(cmd, subCmd, args)
 
     # 
--------------------------------------------------------------------------
     #
@@ -599,4 +749,4 @@
         cmd = "CONTAINER"
         subCmd = "RESTART"
         self.__logger.logInfo("[Framework] Restart the container")
-        return self.__gadgetFramework.sendCommand(cmd, subCmd)
+        return self.__frameworkBridge.sendCommand(cmd, subCmd)


------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to