def build_commands(tasks: list, source: str, destination_list: list): """ Translates tasks into commands""" if len(destination_list) != len(tasks): return None cmd_list = [] for i in range(0, len(tasks)): task_set = tasks[i] tmp_task = task_set if isinstance(task_set, list) else [task_set] tmp_cmd_list = [] for task in tmp_task: new_cmd = CommandFactory.get_command( cmd_type=task.type, src=source, dest=destination_list[i], param=[task.payoff, task.resources]) tmp_cmd_list.append(new_cmd) cmd_list.append(tmp_cmd_list) return cmd_list
def test_ack(self): source = "source" dest = "dest" param = [1, 2, 3] execute_at = datetime.datetime.now() cmd = CommandFactory.get_command(CommandFactory.CommandType.print, source, dest, param, execute_at) status = cmd.execute_command() self.assertEqual(cmd.source, source) self.assertEqual(cmd.dest, dest) self.assertEqual(cmd.param, param) self.assertEqual(cmd.execute_at, execute_at)
def test_mission(self): ground = "ground" sat1 = "sat1" sat2 = "sat2" ground_station = GroundStation(ground) satellite1 = Satellite(sat1) satellite2 = Satellite(sat2) ground_station.create_connection(satellite1) ground_station.create_connection(satellite2) param = [1, 2, 3] # Ack execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.print, src=ground, dest=sat1, param=param, execute_at=execute_at) ground_station.execute_command(cmd) '''
def test_demultiplexer(self): sat1 = "sat1" sat2 = "sat2" ground = "ground" cm_ground = CommandExecutor(ground) cm_sat1 = CommandExecutor(sat1) cm_sat2 = CommandExecutor(sat2) grn_demult = DeMultiplexer() sat1_grn, grn_sat1 = multiprocessing.Pipe(duplex=True) sat2_grn, grn_sat2 = multiprocessing.Pipe(duplex=True) ground_sat1_adapter = NetAdapter(destination=sat1, callback_func=cm_ground.enqueue_command, conn_obj=grn_sat1) sat1_adapter = NetAdapter(destination=ground, callback_func=cm_sat1.enqueue_command, conn_obj=sat1_grn) ground_sat2_adapter = NetAdapter(destination=sat2, callback_func=cm_ground.enqueue_command, conn_obj=grn_sat2) sat2_adapter = NetAdapter(destination=ground, callback_func=cm_sat2.enqueue_command, conn_obj=sat2_grn) grn_demult.add_connection(sat1, ground_sat1_adapter) grn_demult.add_connection(sat2, ground_sat2_adapter) cm_ground.set_adapter(grn_demult) cm_sat1.set_adapter(sat1_adapter) cm_sat2.set_adapter(sat2_adapter) param = [1, 2, 3] # Ack execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.print, src=ground, dest=sat1, param=param, execute_at=execute_at) cm_ground.enqueue_command(cmd) # Print execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.print, src=sat1, dest=ground, param=param, execute_at=execute_at) cm_sat1.enqueue_command(cmd) # Error execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.error, src=ground, dest=sat2, param=param, execute_at=execute_at) cm_ground.enqueue_command(cmd) # Ack execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.print, src=sat2, dest=ground, param=param, execute_at=execute_at) cm_sat2.enqueue_command(cmd) # Print execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.error, src=ground, dest=sat1, param=param, execute_at=execute_at) cm_ground.enqueue_command(cmd) # Error execute_at = -1 cmd = CommandFactory.get_command(cmd_type=CommandFactory.CommandType.error, src=sat1, dest=ground, param=param, execute_at=execute_at) cm_sat1.enqueue_command(cmd)
def test_execute_cmds(self): dest = "dest" source = "source" cmsource = CommandExecutor(source) cmddest = CommandExecutor(dest) a1p, a2p = multiprocessing.Pipe(duplex=True) adapter1 = NetAdapter(destination=dest, callback_func=cmsource.enqueue_command, conn_obj=a2p) adapter2 = NetAdapter(destination=source, callback_func=cmddest.enqueue_command, conn_obj=a1p) cmsource.set_adapter(adapter1) cmddest.set_adapter(adapter2) param = [1, 2, 3] # Ack execute_at = -1 cmd = CommandFactory.get_command( cmd_type=CommandFactory.CommandType.print, src=source, dest=dest, param=param, execute_at=execute_at) cmsource.enqueue_command(cmd) # Print execute_at = -1 cmd = CommandFactory.get_command(CommandFactory.CommandType.print, source, dest, param, execute_at) cmddest.enqueue_command(cmd) # Error execute_at = -1 cmd = CommandFactory.get_command(CommandFactory.CommandType.error, source, dest, param, execute_at) cmsource.enqueue_command(cmd) # Ack execute_at = -1 cmd = CommandFactory.get_command(CommandFactory.CommandType.error, dest, source, param, execute_at) cmddest.enqueue_command(cmd) # Print execute_at = -1 cmd = CommandFactory.get_command(CommandFactory.CommandType.print, dest, source, param, execute_at) cmsource.enqueue_command(cmd) # Error execute_at = -1 cmd = CommandFactory.get_command(CommandFactory.CommandType.error, dest, source, param, execute_at) cmddest.enqueue_command(cmd)