Ejemplo n.º 1
0
def start_pipeline_server(host,
                          port,
                          config_base,
                          background_base,
                          scripts_base,
                          cam_server_api_address,
                          hostname=None,
                          port_range=None,
                          web_server=config.DEFAULT_WEB_SERVER,
                          web_server_args={}):

    # Check if config directory exists
    if not os.path.isdir(config_base):
        _logger.error("Configuration directory '%s' does not exist." %
                      config_base)
        exit(-1)

    if not os.path.isdir(background_base):
        _logger.error("Background image directory '%s' does not exist." %
                      background_base)
        exit(-1)

    if not os.path.isdir(scripts_base):
        _logger.error("Scripts directory '%s' does not exist." % scripts_base)
        exit(-1)

    if hostname:
        _logger.warning("Using custom hostname '%s'." % hostname)

    cam_server_client = CamClient(cam_server_api_address)
    config_manager = PipelineConfigManager(
        config_provider=ConfigFileStorage(config_base))
    background_manager = BackgroundImageManager(background_base)
    user_scripts_manager = UserScriptsManager(scripts_base)
    pipeline_instance_manager = PipelineInstanceManager(config_manager,
                                                        background_manager,
                                                        user_scripts_manager,
                                                        cam_server_client,
                                                        hostname=hostname,
                                                        port_range=port_range)

    app = bottle.Bottle()

    register_pipeline_rest_interface(
        app=app, instance_manager=pipeline_instance_manager)

    try:
        bottle.run(app=app,
                   server=validate_web_server(web_server),
                   host=host,
                   port=port,
                   **web_server_args)
    finally:
        # Close the external processor when terminating the web server.
        pipeline_instance_manager.stop_all_instances()
    def test_custom_hostname(self):
        config_manager = PipelineConfigManager(
            config_provider=MockConfigStorage())
        pipeline_instance_manager = PipelineInstanceManager(
            config_manager,
            MockBackgroundManager(),
            CamClient("http://0.0.0.0:8888"),
            hostname="custom_cam_hostname")

        _, stream_address = pipeline_instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertTrue(stream_address.startswith("tcp://custom_cam_hostname"))

        pipeline_instance_manager.stop_all_instances()
Ejemplo n.º 3
0
def get_test_pipeline_manager_with_real_cam():
    config_manager = PipelineConfigManager(config_provider=MockConfigStorage())
    pipeline_instance_manager = PipelineInstanceManager(
        config_manager, MockBackgroundManager(), None,
        CamClient("http://0.0.0.0:8888"))

    return pipeline_instance_manager
Ejemplo n.º 4
0
def start_pipeline_server(host,
                          port,
                          config_base,
                          background_base,
                          cam_server_api_address,
                          hostname=None):

    proxy_instance_manager = PipelineInstanceManager()

    app = bottle.Bottle()

    register_pipeline_rest_interface(app=app,
                                     instance_manager=proxy_instance_manager)

    try:
        bottle.run(app=app, host=host, port=port)
    finally:
        # Close the external processor when terminating the web server.
        proxy_instance_manager.stop_all_instances()
Ejemplo n.º 5
0
def get_test_pipeline_manager():
    config_manager = PipelineConfigManager(config_provider=MockConfigStorage())
    pipeline_instance_manager = PipelineInstanceManager(
        config_manager, MockBackgroundManager(), None, MockCamServerClient())

    return pipeline_instance_manager
Ejemplo n.º 6
0
    def test_out_of_ports(self):
        config_manager = PipelineConfigManager(
            config_provider=MockConfigStorage())
        instance_manager = PipelineInstanceManager(config_manager,
                                                   MockBackgroundManager(),
                                                   None,
                                                   MockCamServerClient(),
                                                   port_range=(12000, 12003))

        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_0).get_stream_port(),
            12000)
        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001)
        self.assertEqual(
            instance_manager.get_instance(instance_id_2).get_stream_port(),
            12002)

        with self.assertRaisesRegex(
                Exception,
                "All ports are used. Stop some instances before opening a new stream."
        ):
            instance_manager.create_pipeline(
                configuration={"camera_name": "simulation"})

        instance_manager.stop_instance(instance_id_1)

        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001,
            "Instance_id_1 should have freeded the port 10001, but some other port was assigned."
        )

        instance_manager.stop_all_instances()

        # Check if the port rotation works as expected.
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(
            instance_manager.get_instance(instance_id_2).get_stream_port(),
            12002)
        self.assertEqual(
            instance_manager.get_instance(instance_id_0).get_stream_port(),
            12000)
        self.assertEqual(
            instance_manager.get_instance(instance_id_1).get_stream_port(),
            12001)

        instance_manager.stop_all_instances()

        old_timeout = config.MFLOW_NO_CLIENTS_TIMEOUT
        config.MFLOW_NO_CLIENTS_TIMEOUT = 1

        # Test the cleanup procedure.
        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        self.assertEqual(len(instance_manager.get_info()["active_instances"]),
                         3, "All 3 instances should be running")

        with self.assertRaisesRegex(
                Exception,
                "All ports are used. Stop some instances before opening a new stream."
        ):
            instance_manager.create_pipeline(
                configuration={"camera_name": "simulation"})

        # Wait for the instances to die.
        sleep(5)

        self.assertEqual(len(instance_manager.get_info()["active_instances"]),
                         0, "All instances should be dead by now.")

        instance_id_2, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_0, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})
        instance_id_1, _ = instance_manager.create_pipeline(
            configuration={"camera_name": "simulation"})

        # Restore previous state.
        config.MFLOW_NO_CLIENTS_TIMEOUT = old_timeout
        instance_manager.stop_all_instances()