Exemple #1
0
    def unregister(self, client):
        """
        Unregister `client` if it exists.

        Args:
            client (CloudNodeProtocol): Client to be unregistered.
        
        Returns:
            bool: Returns whether unregistration was successful.
        """
        messages = []

        success = False
        for repo_id, repo_clients in self.clients.items():
            state.start_state(repo_id)
            for client_type, clients in repo_clients.items():
                if client in clients:
                    print("Unregistered client {}".format(client.peer))
                    self.clients[repo_id][client_type].remove(client)
                    success = True
                    if client_type == ClientType.DASHBOARD:
                        state.reset_state(repo_id)
                    elif state.state["busy"]:
                        state.state["num_nodes_chosen"] -= 1
                        if state.state["num_nodes_chosen"] == 0:
                            state.reset_state(repo_id)
                            message = self._make_no_nodes_left_message(repo_id)
                            messages.append(message)

            state.stop_state()
        return success, messages
def serve_tfjs_model(session_id, filename):
    """
    Serves the TFJS model to the user.

    TODO: Should do this through ngnix for a boost in performance. Should also
    have some auth token -> session id mapping (security fix in the future).

    Args:
        filename (str): The filename to serve.
    """
    if not state.start_state_by_session_id(session_id):
        return "No active session!\n"
    try:
        if not state.state["busy"]:
            return "No active session!\n"
        if state.state["library_type"] != LibraryType.JS.value:
            return "Current session is not for JAVASCRIPT!"
        folder_path = os.path.join(app.root_path,
                                   state.state['tfjs_model_path'])
    except Exception as e:
        print("Exception getting TFJS model: " + str(e))
        state.stop_state()
        return
    state.stop_state()
    return send_from_directory(folder_path, filename)
Exemple #3
0
    def onMessage(self, payload, isBinary):
        """
        Processes the payload received by a connected node.

        Messages are ignored unless the message is of type "REGISTER" or the
        node has already been registered (by sending a "REGISTER" type message).

        """
        print("Got payload!")
        if isBinary:
            print("Binary message not supported.")
            return

        try:
            received_message = validate_new_message(payload)
        except Exception as e:
            if isinstance(e, json.decoder.JSONDecodeError):
                error_message = "Error while converting JSON."
            else:
                error_message = "Error deserializing message: {}"
                error_message = error_message.format(e)
            message = {
                "error": True,
                "error_message": error_message,
                "type": ErrorType.DESERIALIZATION.value
            }
            self.sendMessage(json.dumps(message).encode(), isBinary)
            print(error_message)
            return

        # Process message
        try:
            state.start_state(received_message.repo_id)
            results = process_new_message(received_message, self.factory, self)
            state.stop_state()
        except Exception as e:
            state.stop_state()
            error_message = "Error processing new message: " + str(e)
            print(error_message)
            raise e
            results = make_error_results(error_message, ErrorType.OTHER)

        print(results)

        if results["action"] == ActionType.BROADCAST:
            self._broadcastMessage(
                payload=results["message"],
                client_list=results["client_list"],
                isBinary=isBinary,
            )
        elif results["action"] == ActionType.UNICAST:
            message = json.dumps(results["message"]).encode()
            self.sendMessage(message, isBinary)
def reset_state(repo_id):
    """
    Resets the state of the cloud node.
    """
    state.start_state(repo_id)
    try:
        state.reset_state(repo_id)
    except Exception as e:
        print("Exception resetting state: " + str(e))
        state.stop_state()
        return
    state.stop_state()
    return "State reset successfully!"
def get_state(repo_id):
    """
    Get the state of the cloud node.
    """
    state.start_state(repo_id)
    try:
        state_dict = repr(state.state)
    except Exception as e:
        print("Exception getting state: " + str(e))
        state.stop_state()
        return

    state.stop_state()
    return state_dict
def get_status(repo_id):
    """
    Returns the status of the Cloud Node.

    The dashboard-api is the only hitting this endpoint, so it should be
    secured.
    """
    state.start_state(repo_id)
    try:
        status = jsonify({"Busy": state.state["busy"]})
    except Exception as e:
        print("Exception getting status: " + str(e))
        state.stop_state()
        return
    state.stop_state()
    return status
def serve_keras_model(session_id):
    if not state.start_state_by_session_id(session_id):
        return "No active session!\n"
    try:
        if not state.state["busy"]:
            return "No active session!\n"
        if state.state["library_type"] != LibraryType.PYTHON.value:
            return "Current session is not for PYTHON!"
        app_path = os.path.join(app.root_path, state.state['h5_model_path'])
    except Exception as e:
        print("Exception getting Keras model: " + str(e))
        state.stop_state()
        return

    state.stop_state()
    return send_file(app_path)
def serve_mlmodel(session_id):
    """
    Serves the mlmodel model to the user.

    TODO: Should do this through ngnix for a boost in performance. Should also
    have some auth token -> session id mapping (security fix in the future).
    """
    if not state.start_state_by_session_id(session_id):
        return "No active session!\n"
    try:
        if not state.state["busy"]:
            return "No active session!\n"
        if state.state["library_type"] != LibraryType.IOS_IMAGE.value:
            return "Current session is not for IOS!"
        app_path = os.path.join(app.root_path, state.state['mlmodel_path'])
    except Exception as e:
        print("Exception getting mlmodel: " + str(e))
        state.stop_state()
        return
    state.stop_state()
    return send_file(app_path)
def reset_state(repo_id, api_key):
    state.start_state(repo_id)
    state.state["test"] = True
    yield
    state.reset_state(repo_id)
    state.stop_state()