def setUp(self):
        """Set up test environment."""
        self._tr = TaskRunner()
        self.server = UPnPServer(self._tr, SERVER_PRODUCT_NAME,
                                 SERVER_ROOT_DEVICE_CONFIG)

        self.service1 = SwitchPower("SwitchPower")
        self.service2 = URLService("URLService")
        self.server.add_service(self.service1)
        self.server.add_service(self.service2)

        self.client = UPnPClient(self._tr)
        self.thread = threading.Thread(target=self._tr.run_forever)
        self.thread.start()
        time.sleep(0.1)  # wait for client to discover server
예제 #2
0
    def setUp(self):
        """Set up test environment."""
        self._tr = TaskRunner()
        self.server = UPnPServer(self._tr,
                            SERVER_PRODUCT_NAME,
                            SERVER_ROOT_DEVICE_CONFIG)

        self.service1 = SwitchPower("SwitchPower")
        self.service2 = URLService("URLService")
        self.server.add_service(self.service1)
        self.server.add_service(self.service2)

        self.client = UPnPClient(self._tr)
        self.thread = threading.Thread(target=self._tr.run_forever)
        self.thread.start()
        time.sleep(0.1) # wait for client to discover server
예제 #3
0
    def __init__(self):

        # Task Runner
        self._task_runner = TaskRunner()

        # UPnP Server
        self._server = UPnPServer(self._task_runner, "Product 1.0")

        # Switch Power Service
        service_id = "SwitchPower"
        service = SwitchPower(service_id)
        self._server.add_service(service)

        # URL Service
        service_id_2 = "URLService"
        service_2 = URLService(service_id_2)
        self._server.add_service(service_2)

        # Bookmark Service
        service_3 = BookmarkService()
        self._server.add_service(service_3)

        # Console Namespace
        name_space = {}
        root_device = self._server.get_root_device()
        name_space[root_device.name] = root_device
        name_space[service_id] = service
        name_space[service_id_2] = service_2
        name_space[service_3.get_short_service_id()] = service_3
        name_space['add_service'] = self._server.add_service
        name_space['get_service'] = self._server.get_service
        name_space['get_services'] = self._server.get_service_ids
        name_space['announce'] = self._server.announce

        self._console = ObjectConsole(self,
                                      name_space,
                                      run="_run",
                                      stop="_stop",
                                      name="UPnP Server")
    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")
예제 #5
0
class UPnPTestCase(unittest.TestCase):

    """TestCase for UPnPServer and UPnPClient."""

    def setUp(self):
        """Set up test environment."""
        self._tr = TaskRunner()
        self.server = UPnPServer(self._tr,
                            SERVER_PRODUCT_NAME,
                            SERVER_ROOT_DEVICE_CONFIG)

        self.service1 = SwitchPower("SwitchPower")
        self.service2 = URLService("URLService")
        self.server.add_service(self.service1)
        self.server.add_service(self.service2)

        self.client = UPnPClient(self._tr)
        self.thread = threading.Thread(target=self._tr.run_forever)
        self.thread.start()
        time.sleep(0.1) # wait for client to discover server

    def tearDown(self):
        """Clean up test environment."""
        self.client.close()
        self.server.close()
        self._tr.stop()
        self.thread.join()

    def test_device_discovery(self):
        """
        Test if UPnPClient is able to discover the UPnPDevice
        hosted by UPnPServer.
        """
        root = self.server.get_root_device()
        self.assertTrue( root.uuid in self.client.get_device_uuids())

    def test_switchpower(self):
        """Test discovery and use of SwitchPowerService."""

        stub = self.client.get_services_by_short_id("SwitchPower")[0]

        # Test service_id
        self.assertEqual(self.service1.get_service_id(),
                         stub.get_service_id())
        # Test service_type
        self.assertEqual(self.service1.get_service_type(),
                         stub.get_service_type())

        # Subscribe
        evr = _EventReceiver()
        stub.subscribe(evr.handler)

        # Update SwitchPowerService by using Stub.
        value = stub.GetStatus()
        stub.SetTarget(not value)
        new_value = stub.GetStatus()
        self.assertEqual(not value, new_value,
                         evr.get_last_event()[2])

        # Update SwitchPowerService directly.
        self.service1.set_target(not new_value)
        time.sleep(0.1) # wait for notification
        self.assertEqual(not new_value, evr.get_last_event()[2])

        # Unsubscribe
        stub.unsubscribe(evr.handler)

    def test_urlservice(self):
        """Test discovery and use of URLService."""
        stub = self.client.get_services_by_short_id("URLService")[0]

        # Subscribe
        evr = _EventReceiver()
        stub.subscribe(evr.handler)

        # Update URLService by using Stub.
        stub.GetURL()
        new_url = "http://p2p-next.org"
        stub.SetURL(new_url)
        service_url = self.service2.get_url()
        url = stub.GetURL()
        self.assertEqual(new_url, service_url, url)
        self.assertEqual(url, evr.get_last_event()[2])

        # Update URLService directly
        url2 = "http://itek.norut.no"
        self.service2.set_url(url2)
        time.sleep(0.1) # wait for notification
        self.assertEqual(url2, evr.get_last_event()[2])

        # Unsubscribe
        stub.unsubscribe(evr.handler)
예제 #6
0
        """Mock Event Dispatcher."""
        def __init__(self):
            pass

        def dispatch(self, sid, event_key, callback_url, variables):
            """Mock method."""
            print "Notify", sid, event_key, callback_url, variables

    class MockServiceManager:
        """Mock Service Manager."""
        def __init__(self):
            self._ed = MockEventDispatcher()

        def get_event_dispatcher(self):
            """Mock method."""
            return self._ed

        def get_base_url(self):
            """Mock method."""
            return "http://myhost:44444"

        def get_logger(self):
            """Mock method."""
            return None

    SM = MockServiceManager()
    from Tribler.UPnP.services import SwitchPower
    SERVICE = SwitchPower('SwitchPower')
    SERVICE.set_service_manager(SM)
    print SERVICE.get_xml_description()
class UPnPTestCase(unittest.TestCase):
    """TestCase for UPnPServer and UPnPClient."""
    def setUp(self):
        """Set up test environment."""
        self._tr = TaskRunner()
        self.server = UPnPServer(self._tr, SERVER_PRODUCT_NAME,
                                 SERVER_ROOT_DEVICE_CONFIG)

        self.service1 = SwitchPower("SwitchPower")
        self.service2 = URLService("URLService")
        self.server.add_service(self.service1)
        self.server.add_service(self.service2)

        self.client = UPnPClient(self._tr)
        self.thread = threading.Thread(target=self._tr.run_forever)
        self.thread.start()
        time.sleep(0.1)  # wait for client to discover server

    def tearDown(self):
        """Clean up test environment."""
        self.client.close()
        self.server.close()
        self._tr.stop()
        self.thread.join()

    def test_device_discovery(self):
        """
        Test if UPnPClient is able to discover the UPnPDevice
        hosted by UPnPServer.
        """
        root = self.server.get_root_device()
        self.assertTrue(root.uuid in self.client.get_device_uuids())

    def test_switchpower(self):
        """Test discovery and use of SwitchPowerService."""

        stub = self.client.get_services_by_short_id("SwitchPower")[0]

        # Test service_id
        self.assertEqual(self.service1.get_service_id(), stub.get_service_id())
        # Test service_type
        self.assertEqual(self.service1.get_service_type(),
                         stub.get_service_type())

        # Subscribe
        evr = _EventReceiver()
        stub.subscribe(evr.handler)

        # Update SwitchPowerService by using Stub.
        value = stub.GetStatus()
        stub.SetTarget(not value)
        new_value = stub.GetStatus()
        self.assertEqual(not value, new_value, evr.get_last_event()[2])

        # Update SwitchPowerService directly.
        self.service1.set_target(not new_value)
        time.sleep(0.1)  # wait for notification
        self.assertEqual(not new_value, evr.get_last_event()[2])

        # Unsubscribe
        stub.unsubscribe(evr.handler)

    def test_urlservice(self):
        """Test discovery and use of URLService."""
        stub = self.client.get_services_by_short_id("URLService")[0]

        # Subscribe
        evr = _EventReceiver()
        stub.subscribe(evr.handler)

        # Update URLService by using Stub.
        stub.GetURL()
        new_url = "http://p2p-next.org"
        stub.SetURL(new_url)
        service_url = self.service2.get_url()
        url = stub.GetURL()
        self.assertEqual(new_url, service_url, url)
        self.assertEqual(url, evr.get_last_event()[2])

        # Update URLService directly
        url2 = "http://itek.norut.no"
        self.service2.set_url(url2)
        time.sleep(0.1)  # wait for notification
        self.assertEqual(url2, evr.get_last_event()[2])

        # Unsubscribe
        stub.unsubscribe(evr.handler)
예제 #8
0
if __name__ == '__main__':

    class MockEventDispatcher:
        """Mock Event Dispatcher."""
        def __init__(self):
            pass
        def dispatch(self, sid, event_key, callback_url, variables):
            """Mock method."""
            print "Notify", sid, event_key, callback_url, variables

    class MockServiceManager:
        """Mock Service Manager."""
        def __init__(self):
            self._ed = MockEventDispatcher()
        def get_event_dispatcher(self):
            """Mock method."""
            return self._ed
        def get_base_url(self):
            """Mock method."""
            return "http://myhost:44444"
        def get_logger(self):
            """Mock method."""
            return None

    SM = MockServiceManager()
    from Tribler.UPnP.services import SwitchPower
    SERVICE = SwitchPower('SwitchPower')
    SERVICE.set_service_manager(SM)
    print SERVICE.get_xml_description()