def on_disable_button_clicked(self, *args):
        """
        Disables the selected client but keeps it in connection_tree_view. Triggered when disable button clicked.

        :param args:
        :return:
        """
        path = self.view["connection_tree_view1"].get_cursor()[0]
        if path is not None:
            for index in path:
                address = self.network_manager_model.connected_ip_port[index]
                if self.network_manager_model.get_connected_status(address) == "connected":
                    # self.view["disable_btn1"].set_label("Enable")
                    logger.info("Disable client: {0}".format(address))
                else:
                    # self.view["disable_btn1"].set_label("Disable")
                    logger.info("Enable client: {0}".format(address))
                global_monitoring_manager.disable(address)
            self.view["connection_tree_view1"].set_cursor(path)
Exemplo n.º 2
0
def server_interaction_worker(queue_dict, execution_engine, state_machine_id):
    from rafcon.core.singleton import global_variable_manager as gvm
    from rafcon.core.execution.execution_status import StateMachineExecutionStatus
    from rafcon.core.singleton import state_machine_manager
    from monitoring import server
    from monitoring.monitoring_manager import global_monitoring_manager

    sm = state_machine_manager.state_machines[state_machine_id]
    print("current state machine", sm)
    root_state = sm.root_state
    sleep_time = 0.99

    for key, sv in sm.root_state.scoped_variables.items():
        if sv.name == "bottles":
            sv.default_value = 3

    #######################################################
    print("\n\n\nserver TEST1 stop - start - wait_for_stop")
    #######################################################

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_1"
    print("received: ", queue_element)

    # wait before Decimate Bottles
    execution_engine.run_to_selected_state("GLSUJY/NDIVLD",
                                           state_machine_id=state_machine_id)
    # check when run to is finished; run_to is issued from the server thread
    while gvm.get_variable("sing_counter") < 1:
        print("wait for client")
        time.sleep(sleep_time)
    print("starting tests\n\n")

    print("put: started")
    queue_dict[SERVER_TO_CLIENT].put("started")

    # step1: stop start test
    while not execution_engine.finished_or_stopped():
        print("wait until state machine finished!")
        time.sleep(sleep_time)
    print("put: stop_received")
    queue_dict[SERVER_TO_CLIENT].put("stop_received")

    # step2: wait until state machine started
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        print("execution_engine.status.execution_mode: ",
              execution_engine.status.execution_mode)
        # this time has to be very small otherwise the state machine start and finish during one sleep period
        # each state has a sleep of 0.01s
        time.sleep(0.01)
    print("put: started")
    queue_dict[SERVER_TO_CLIENT].put("restarted")

    # stop the state machine locally
    execution_engine.stop()  # reset state machine before the next test
    execution_engine.join()

    reset_global_variable_manager(gvm)
    queue_dict[SERVER_TO_CLIENT].put("successfully stopped the state machine")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print("\n\n\nserver TEST2 disconnect -> run sm -> connect -> run sm")
    #######################################################

    print("server: starting test 2")
    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_2"
    print("received: ", queue_element)

    # step 1
    # test disconnect by client run
    print("disconnect/connect by client test")
    queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run")
    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "disconnected_and_executed"
    print("received: ", queue_element)

    # the client executed a start command, which must not do anything as the client disconnected beforehand

    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0

    # step 2
    queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run")
    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "reconnected_and_executed"
    print("received: ", queue_element)

    execution_engine.join()
    print("server sm finished")
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")

    execution_engine.stop()
    execution_engine.join()
    reset_global_variable_manager(gvm)
    print("server: disconnect/connect by client test successful\n\n")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print("\n\n\nserver TEST3 disable -> run sm -> enable -> run sm ")
    #######################################################

    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_3"
    print("server received: ", queue_element)

    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
        # while not server.network_manager_model.get_connected_status(address) == "disabled":
        #     time.sleep(0.01)
    queue_dict[SERVER_TO_CLIENT].put("you_are_disabled")

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "reached end"
    print("server received: ", queue_element)

    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0

    # TODO: reconnect is not properly implemented
    # for address in server.network_manager_model.connected_ip_port:
    #     global_monitoring_manager.reconnect(address)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "enabled_again"
    print("server received: ", queue_element)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "started_execution"
    print("server received: ", queue_element)

    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print("server sm finished")
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    execution_engine.join()
    reset_global_variable_manager(gvm)
    print("server: dis- /enabled by server test successful\n\n")

    # queue_dict[KILL_SERVER_QUEUE].get()
    # os._exit(0)
    # return

    #######################################################
    print(
        "server TEST4 change client ID in config -> apply -> check connected client ID"
    )
    #######################################################
    reset_global_variable_manager(gvm)

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "start_test_4"
    print("server received: ", queue_element)

    queue_dict[SERVER_TO_CLIENT].put("ready_to_change_config")

    queue_element = queue_dict[CLIENT_TO_SERVER].get()
    assert queue_element == "on_apply_button_clicked"
    print("server received: ", queue_element)

    client_id = []
    for address in server.network_manager_model.connected_ip_port:
        client_id.append(
            server.network_manager_model.get_connected_id(address))
    assert "apply_test_client_id" in client_id
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    print("apply config test successful\n\n")

    execution_engine.stop()
    execution_engine.join()
    queue_dict[KILL_SERVER_QUEUE].get()
    os._exit(0)
Exemplo n.º 3
0
def synchronize_with_clients_threads(queue_dict, execution_engine):
    from rafcon.core.singleton import global_variable_manager as gvm
    from rafcon.core.execution.execution_status import StateMachineExecutionStatus
    from rafcon.core.singleton import state_machine_manager
    active_sm = state_machine_manager.get_active_state_machine()
    root_state = active_sm.root_state
    sleep_time = 0.01

    # check when run to is finished
    while gvm.get_variable("sing_counter") < 1:
        time.sleep(sleep_time)
    """
    TEST1
    """
    queue_dict[SERVER_TO_CLIENT].put("start")
    print "starting tests\n\n"

    # stop start test
    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)

    queue_dict[SERVER_TO_CLIENT].put("stop received")
    # wait until state machine started
    while execution_engine.status.execution_mode is not StateMachineExecutionStatus.STARTED:
        time.sleep(sleep_time)

    while not execution_engine.finished_or_stopped():
        time.sleep(sleep_time)

    reset_global_variable_manager(gvm)
    execution_engine.stop()  # reset state machine before the next test
    if execution_engine.status.execution_mode is not StateMachineExecutionStatus.STOPPED:
        execution_engine.join()
    queue_dict[SERVER_TO_CLIENT].put(
        "start received and successfully ran the state machine")
    print "server: stop start test successful\n\n"
    """
    TEST2 disconnect -> run sm -> connect -> run sm
    """
    from monitoring import server
    from monitoring.monitoring_manager import global_monitoring_manager
    # test disconnect by client run
    print "disconnect/connect by client test"
    queue_dict[SERVER_TO_CLIENT].put("disconnect_me_and_run")
    queue_dict[CLIENT_TO_SERVER].get()
    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0
    queue_dict[SERVER_TO_CLIENT].put("reconnect_me_and_run")
    queue_dict[CLIENT_TO_SERVER].get()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print "server sm finished"
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    reset_global_variable_manager(gvm)
    print "server: disconnect/connect by client test successful\n\n"
    """
    TEST3 disable -> run sm -> enable -> run sm
    """
    # test dis- enabled by server run
    print "dis- /enabled test by server"
    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
        # while not server.network_manager_model.get_connected_status(address) == "disabled":
        #     time.sleep(0.01)
    queue_dict[SERVER_TO_CLIENT].put("you are disabled")
    queue_dict[CLIENT_TO_SERVER].get()
    print "sm on client executed and stopped"
    assert gvm.get_variable("count_counter") == 0
    assert gvm.get_variable("sing_counter") == 0
    assert gvm.get_variable("decimate_counter") == 0
    for address in server.network_manager_model.connected_ip_port:
        global_monitoring_manager.disable(address)
    queue_dict[SERVER_TO_CLIENT].put("you are enabled")
    queue_dict[CLIENT_TO_SERVER].get()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    print "server sm finished"
    assert gvm.get_variable("count_counter") == 3
    assert gvm.get_variable("sing_counter") == 3
    assert gvm.get_variable("decimate_counter") == 3
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    execution_engine.stop()
    if not execution_engine.finished_or_stopped():
        execution_engine.join()
    reset_global_variable_manager(gvm)
    print "server: dis- /enabled by server test successful\n\n"
    """
    TEST4 change client ID in config -> apply -> check connected client ID
    """
    print "apply config test"
    queue_dict[SERVER_TO_CLIENT].put("ready to change config")
    queue_dict[CLIENT_TO_SERVER].get()
    client_id = []
    for address in server.network_manager_model.connected_ip_port:
        client_id.append(
            server.network_manager_model.get_connected_id(address))
    assert "apply_test_client_id" in client_id
    queue_dict[SERVER_TO_CLIENT].put("succeeded")
    print "apply config test successful\n\n"

    execution_engine.stop()
    execution_engine.join()
    queue_dict[KILL_SERVER_QUEUE].get()
    os._exit(0)