def _handle_message(action: bytes, data: List[bytes]) -> bytes: from drivebuildclient.aiExchangeMessages_pb2 import Bool if action == b"isRunning": sid = SimulationID() sid.ParseFromString(data[0]) result = Bool() result.value = _is_simulation_running(sid) elif action == b"vids": sid = SimulationID() sid.ParseFromString(data[0]) result = _get_vids(sid) elif action == b"pollSensors": sid = SimulationID() sid.ParseFromString(data[0]) result = _poll_sensors(sid) elif action == b"verify": sid = SimulationID() sid.ParseFromString(data[0]) result = _verify(sid) elif action == b"requestAiFor": sid = SimulationID() sid.ParseFromString(data[0]) vid = VehicleID() vid.ParseFromString(data[1]) result = _request_ai_for(sid, vid) elif action == b"storeVerificationCycle": sid = SimulationID() sid.ParseFromString(data[0]) started = Num() started.ParseFromString(data[1]) finished = Num() finished.ParseFromString(data[2]) result = _store_verification_cycle( sid, datetime.fromtimestamp(started.num), datetime.fromtimestamp(finished.num)) elif action == b"steps": sid = SimulationID() sid.ParseFromString(data[0]) steps = Num() steps.ParseFromString(data[1]) result = Void() if _is_simulation_running(sid): _get_data(sid).scenario.bng.step(steps.num) result.message = "Simulated " + str( steps.num) + " steps in simulation " + sid.sid + "." else: result.message = "Simulation " + sid.sid + " is not running anymore." elif action == b"stop": sid = SimulationID() sid.ParseFromString(data[0]) test_result = TestResult() test_result.ParseFromString(data[1]) _control_sim(sid, test_result.result, False) result = Void() else: message = "The action \"" + action.decode() + "\" is unknown." _logger.info(message) result = Void() result.message = message return result.SerializeToString()
def extract_sid() -> Tuple[Optional[bytes], Optional[SimulationID]]: from flask import request serialized_sid_b = request.args.get("sid", default=None) if serialized_sid_b: serialized_sid = serialized_sid_b.encode() sid = SimulationID() sid.ParseFromString(serialized_sid) return serialized_sid, sid else: return None, None
def run_test_case( test_case: TestCase ) -> Tuple[Simulation, Scenario, ExtThread, SimulationID]: """ This method starts the actual simulation in a separate thread. Additionally it already calculates and attaches all information that is need by this node and the separate thread before calling _start_simulation(...). """ import dill as pickle from drivebuildclient import create_client, send_request from config import SIM_NODE_PORT, FIRST_SIM_PORT sid = SimulationID() response = send_request(create_client("localhost", SIM_NODE_PORT), b"generateSid", []) sid.ParseFromString(response) sim = Simulation(sid, pickle.dumps(test_case), FIRST_SIM_PORT + run_test_case.counter) run_test_case.counter += 1 # FIXME Add a lock? # Make sure there is no folder of previous tests having the same sid that got not propery removed bng_scenario, thread = sim._start_simulation(test_case) return sim, bng_scenario, thread, sid
def _handle_main_app_message(action: bytes, data: List[bytes]) -> bytes: from google.protobuf.message import DecodeError if action == b"runTests": user = User() try: user.ParseFromString(data[1]) result = _run_tests(data[0], user) except DecodeError: _logger.exception("Running a test failed since \"" + str(data[1]) + "\" can not be parsed to an User") result = SubmissionResult() result.message.message = "The user parameter could not be parsed." elif action == b"waitForSimulatorRequest": sid = SimulationID() sid.ParseFromString(data[0]) vid = VehicleID() vid.ParseFromString(data[1]) result = _wait_for_simulator_request(sid, vid) elif action == b"control": sid = SimulationID() sid.ParseFromString(data[0]) vid = VehicleID() vid.ParseFromString(data[1]) control = Control() control.ParseFromString(data[2]) result = _control(sid, vid, control) elif action == b"requestData": sid = SimulationID() sid.ParseFromString(data[0]) vid = VehicleID() vid.ParseFromString(data[1]) request = DataRequest() request.ParseFromString(data[2]) result = _request_data(sid, vid, request) elif action == b"requestSocket": client = create_client(MAIN_APP_HOST, MAIN_APP_PORT) client_thread = Thread(target=process_requests, args=(client, _handle_main_app_message)) client_thread.daemon = True _logger.info("_handle_main_app_message --> " + str(client.getsockname())) client_thread.start() result = Void() result.message = "Connected another client socket to the main app." elif action == b"runningTests": user = User() user.ParseFromString(data[0]) result = _get_running_tests(user) elif action == b"stop": sid = SimulationID() sid.ParseFromString(data[0]) test_result = TestResult() test_result.ParseFromString(data[1]) _control_sim(sid, test_result.result, False) result = Void() result.message = "Stopped simulation " + sid.sid + "." else: message = "The action \"" + action.decode() + "\" is unknown." _logger.info(message) result = Void() result.message = message return result.SerializeToString()