def test_sumo_version():
    from smarts.core.utils.sumo import traci, SUMO_PATH
    from smarts.core.utils import networking

    load_params = [
        "--start",
        "--quit-on-end",
        "--net-file=scenarios/loop/map.net.xml",
        "--no-step-log",
        "--no-warnings=1",
    ]

    sumo_port = networking.find_free_port()
    sumo_cmd = [
        os.path.join(SUMO_PATH, "bin", "sumo"),
        "--remote-port=%s" % sumo_port,
        *load_params,
    ]

    sumo_proc = subprocess.Popen(
        sumo_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
    )

    traci_conn = traci.connect(
        sumo_port, numRetries=10, proc=sumo_proc, waitBetweenRetries=0.1
    )

    assert (
        traci_conn.getVersion()[0] >= 20
    ), "TraCI API version must be >= 20 (SUMO 1.5.0)"
Beispiel #2
0
def connection():
    """This connects to an existing SUMO server instance."""
    traci_conn = traci.connect(
        SUMO_PORT, numRetries=100, proc=None, waitBetweenRetries=0.1
    )
    traci_conn.setOrder(2)
    return traci_conn
Beispiel #3
0
def connect(port, order=None):
    traci_conn = traci.connect(port,
                               numRetries=100,
                               proc=None,
                               waitBetweenRetries=0.1)
    if order is not None:
        traci_conn.setOrder(order)
    return traci_conn
Beispiel #4
0
def start_sumo_server():
    sumo_binary = "sumo"
    sumo_cmd = [
        os.path.join(SUMO_PATH, "bin", sumo_binary),
        "--net-file=scenarios/loop/map.net.xml",
        "--num-clients=3",
        "--remote-port=%s" % PORT,
    ]
    sumo_proc = subprocess.Popen(
        sumo_cmd,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )

    time.sleep(0.1)
    traci_conn = traci.connect(PORT,
                               numRetries=100,
                               proc=sumo_proc,
                               waitBetweenRetries=0.01)
    return traci_conn
Beispiel #5
0
    def _initialize_traci_conn(self, num_retries=5):
        # TODO: inline sumo or process pool
        # the retries are to deal with port collisions
        #   since the way we start sumo here has a race condition on
        #   each spawned process claiming a port
        for _ in range(num_retries):
            self._close_traci_and_pipes()

            sumo_port = self._sumo_port
            if sumo_port is None:
                sumo_port = networking.find_free_port()

            sumo_binary = "sumo" if self._headless else "sumo-gui"
            sumo_cmd = [
                os.path.join(SUMO_PATH, "bin", sumo_binary),
                "--remote-port=%s" % sumo_port,
                *self._base_sumo_load_params(),
            ]

            self._log.debug("Starting sumo process:\n\t %s", sumo_cmd)
            self._sumo_proc = subprocess.Popen(
                sumo_cmd,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                close_fds=True,
            )
            time.sleep(0.05)  # give SUMO time to start
            try:
                with suppress_output(stdout=False):
                    self._traci_conn = traci.connect(
                        sumo_port,
                        numRetries=100,
                        proc=self._sumo_proc,
                        waitBetweenRetries=0.05,
                    )  # SUMO must be ready within 5 seconds

                try:
                    assert (self._traci_conn.getVersion()[0] >=
                            20), "TraCI API version must be >= 20 (SUMO 1.5.0)"
                # We will retry since this is our first sumo command
                except FatalTraCIError:
                    logging.debug("Connection closed. Retrying...")
                    self._close_traci_and_pipes()
                    continue
                except TraCIException as e:
                    logging.debug(
                        f"Unknown connection issue has occurred: {e}")
                    self._close_traci_and_pipes()
            except ConnectionRefusedError:
                logging.debug(
                    "Connection refused. Tried to connect to unpaired TraCI client."
                )
                self._close_traci_and_pipes()
                continue
            break

        try:
            # It is mandatory to set order when using multiple clients.
            self._traci_conn.setOrder(0)
            self._traci_conn.getVersion()
        except Exception as e:
            logging.error(f"""Failed to initialize SUMO
                Your scenario might not be configured correctly or
                you were trying to initialize many SUMO instances at
                once and we were not able to assign unique port
                numbers to all SUMO processes.
                Check {self._log_file} for hints""")
            self._handle_traci_disconnect(e)
            raise e

        self._log.debug("Finished starting sumo process")