Esempio n. 1
0
class UPnPConsole:

    """This class wraps ObjectConsole to implement a 
    custom UPnP console."""

    def __init__(self):
        
        self._task_runner = TaskRunner()
        self._server = UPnPServer(self._task_runner, 
                            SERVER_PRODUCT_NAME,
                            SERVER_ROOT_DEVICE_CONFIG)
        self._client = UPnPClient(self._task_runner)

        # Add a couple of services
        self._server.add_service(SwitchPower("SwitchPower"))
        self._server.add_service(URLService("URLService"))
        self._server.add_service(BookmarkService())
        
        # Console Namespace
        namespace = {}
        namespace['S'] = self._server
        namespace['C'] = self._client

        self._console = ObjectConsole(self, namespace,
                                      run="_run",
                                      stop="_stop",
                                      name="UPnP")


    def _run(self):
        """Run the TaskRunner."""
        self._task_runner.run_forever()

    def _stop(self):
        """
        Internal: Stop the UPnPClient, UPnPServer and the 
        TaskRunner.
        """
        self._client.close()
        self._server.close()
        self._task_runner.stop()

    def run(self):
        """Runs the UPnP Console."""
        self._console.run()
Esempio n. 2
0
    def __init__(self):
        
        self._task_runner = TaskRunner()
        self._server = UPnPServer(self._task_runner, 
                            SERVER_PRODUCT_NAME,
                            SERVER_ROOT_DEVICE_CONFIG)
        self._client = UPnPClient(self._task_runner)

        # Add a couple of services
        self._server.add_service(SwitchPower("SwitchPower"))
        self._server.add_service(URLService("URLService"))
        self._server.add_service(BookmarkService())
        
        # Console Namespace
        namespace = {}
        namespace['S'] = self._server
        namespace['C'] = self._client

        self._console = ObjectConsole(self, namespace,
                                      run="_run",
                                      stop="_stop",
                                      name="UPnP")
Esempio n. 3
0
 def _get_task_runner(self):
     """Get referance to TaskRunner instance"""
     if self._task_runner == None:
         from BaseLib.UPnP.common import TaskRunner
         self._task_runner = TaskRunner()        
     return self._task_runner
Esempio n. 4
0
class _UPnP:
    """
    UPnP class holds instances of TaskRunner, UPnPServer 
    and UPnPClient.
    """

    def __init__(self):
        self._task_runner = None
        self._task_runner_thread = None
        self._upnp_server = None
        self._upnp_client = None

        # Real close methods
        self._real_client_close = None
        self._real_server_close = None

    def _get_task_runner(self):
        """Get referance to TaskRunner instance"""
        if self._task_runner == None:
            from BaseLib.UPnP.common import TaskRunner
            self._task_runner = TaskRunner()        
        return self._task_runner

    def start(self, stop_event):
        """Starts the Task Runner Thread"""
        if self._task_runner_thread == None:
            task_runner = self._get_task_runner()
            run = lambda: task_runner.run_forever(stop_event=stop_event)
            self._task_runner_thread = threading.Thread(
                group=None, 
                target=run,
                name= "TaskRunnerThread")
            self._task_runner_thread.setDaemon(True)
            self._task_runner_thread.start()
            
    def is_running(self):
        """Return true if the TaskRunner is executing in a thread."""
        if self._task_runner_thread != None and \
                self._task_runner_thread.is_alive(): 
            return True
        else: 
            return False
 
    def stop(self):
        """
        Stops both client and server, 
        before stopping the Task Runner Thread.
        """
        self._wrap_client_close()
        self._wrap_server_close()
        if self._task_runner:
            self._task_runner.stop()            
            self._task_runner = None
            self._task_runner_thread.join()
            self._task_runner_thread = None
                
    def get_upnp_server(self):
        """Get a referance to the UPnPServer instance."""
        if self._upnp_server == None:
            task_runner = self._get_task_runner()
            from BaseLib.UPnP.upnpserver import UPnPServer
            self._upnp_server = UPnPServer(task_runner, 
                                           SERVER_PRODUCT_NAME, 
                                           SERVER_ROOT_DEVICE_CONFIG,
                                           logger=_LOGGER)     
            # Wrap close method
            self._real_server_close = self._upnp_server.close
            self._upnp_server.close = self._wrap_server_close
        return self._upnp_server

    def get_upnp_client(self):
        """Get a referance to the UPnPClient intance."""
        if self._upnp_client == None:
            task_runner = self._get_task_runner()
            from BaseLib.UPnP.upnpclient import UPnPClient
            self._upnp_client = UPnPClient(task_runner, 
                                           logger=_LOGGER)     
            # Wrap close method
            self._real_client_close = self._upnp_client.close
            self._upnp_client.close = self._wrap_client_close
        return self._upnp_client

    def _wrap_client_close(self):
        """Internal method: Replaces the close() method of the 
        UPnPClient instance."""
        if self._upnp_client != None:
            self._upnp_client = None
            self._real_client_close()

    def _wrap_server_close(self):
        """Internal method: Replaces the close() method of the 
        UPnPServer instance."""
        if self._upnp_server != None:
            self._upnp_server = None
            self._real_server_close()