How to use the imjoy.connection.decorator.socketio_handler function in imjoy

To help you get started, we’ve selected a few imjoy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oeway / ImJoy-Engine / imjoy / services / terminal.py View on Github external
@sio_on("terminal_input", namespace=NAME_SPACE)
async def on_terminal_input(engine, sid, data):
    """Write to the terminal as if you are typing in a real terminal."""
    if sys.platform == "win32":
        return "Terminal is not available on Windows yet."

    logger = engine.logger
    registered_sessions = engine.store.registered_sessions
    terminal_session = engine.store.terminal_session
    if sid not in registered_sessions:
        return
    try:
        if "fd" in terminal_session:
            os.write(terminal_session["fd"], data["input"].encode())
        else:
            return "Terminal session is closed"
    except Exception as exc:  # pylint: disable=broad-except
github oeway / ImJoy-Engine / imjoy / services / terminal.py View on Github external
@sio_on("start_terminal", namespace=NAME_SPACE)
async def on_start_terminal(engine, sid, kwargs):
    """Handle new terminal client connected."""
    if sys.platform == "win32":
        return {"success": False, "error": "Terminal is not available on Windows yet."}
    logger = engine.logger
    registered_sessions = engine.store.registered_sessions
    terminal_session = engine.store.terminal_session
    try:
        if sid not in registered_sessions:
            logger.debug("Client %s is not registered", sid)
            return {"success": False, "error": "client not registered."}

        if "child_pid" in terminal_session and "fd" in terminal_session:
            process_exists = True
            psutil = get_psutil()
            if psutil is not None:
github oeway / ImJoy-Engine / imjoy / runners / subprocess.py View on Github external
@sio_on("disconnect_client_session")
async def disconnect_client_session(engine, sid):
    """Disconnect client session."""
    logger = engine.logger
    clients = engine.store.clients
    plugin_sessions = engine.store.plugin_sessions
    registered_sessions = engine.store.registered_sessions
    if sid in registered_sessions:
        logger.info("Disconnecting client session %s", sid)
        obj = registered_sessions[sid]
        client_id, session_id = obj["client"], obj["session"]
        del registered_sessions[sid]
        if client_id in clients and sid in clients[client_id]:
            clients[client_id].remove(sid)
            if not clients[client_id]:
                del clients[client_id]
        if session_id in plugin_sessions:
github oeway / ImJoy-Engine / imjoy / services / file_server.py View on Github external
@sio_on("get_file_path")
async def on_get_file_path(engine, sid, kwargs):
    """Return file path."""
    logger = engine.logger
    generated_urls = engine.store.generated_urls
    registered_sessions = engine.store.registered_sessions
    logger.info("Generating file url: %s", kwargs)
    if sid not in registered_sessions:
        logger.debug("Client %s is not registered", sid)
        return {"success": False, "error": "client has not been registered"}

    url = kwargs["url"]
    urlid = urlparse(url).path.replace("/file/", "")
    if urlid in generated_urls:
        file_info = generated_urls[urlid]
        return {"success": True, "path": file_info["path"]}
    return {"success": False, "error": "url not found."}
github oeway / ImJoy-Engine / imjoy / runners / subprocess.py View on Github external
@sio_on("disconnect_plugin")
async def disconnect_plugin(engine, sid):
    """Disconnect plugin."""
    logger = engine.logger
    plugins = engine.store.plugins
    plugin_sessions = engine.store.plugin_sessions
    plugin_sids = engine.store.plugin_sids
    plugin_signatures = engine.store.plugin_signatures
    if sid in plugin_sids:
        logger.info("Disconnecting plugin session %s", sid)
        pid = plugin_sids[sid]["id"]
        if pid in plugins:
            logger.info("Cleaning up plugin %s", pid)
            if plugins[pid]["signature"] in plugin_signatures:
                logger.info(
                    "Cleaning up plugin signature %s", plugins[pid]["signature"]
                )
github oeway / ImJoy-Engine / imjoy / runners / subprocess.py View on Github external
@sio_on("kill_plugin_process")
async def on_kill_plugin_process(engine, sid, kwargs):
    """Kill plugin process."""
    logger = engine.logger
    plugins = engine.store.plugins
    registered_sessions = engine.store.registered_sessions
    if sid not in registered_sessions:
        logger.debug("Client %s is not registered", sid)
        return {"success": False, "error": "client has not been registered."}
    if "all" not in kwargs:
        return {
            "success": False,
            "error": 'You must provide the pid of the plugin process or "all=true".',
        }
    if kwargs["all"]:
        logger.info("Killing all the plugins")
        await kill_all_plugins(engine, sid)
github oeway / ImJoy-Engine / imjoy / connection / handler.py View on Github external
@sio_on("get_engine_status")
async def on_get_engine_status(engine, sid, _):
    """Return engine status."""
    logger = engine.logger
    plugins = engine.store.plugins
    registered_sessions = engine.store.registered_sessions
    if sid not in registered_sessions:
        logger.debug("Client %s is not registered", sid)
        return {"success": False, "error": "client has not been registered."}
    psutil = get_psutil()
    if psutil is None:
        return {"success": False, "error": "psutil is not available."}
    current_process = psutil.Process()
    children = current_process.children(recursive=True)
    pid_dict = {}
    for plugin in plugins.values():
        if plugin["process_id"] is not None:
github oeway / ImJoy-Engine / imjoy / runners / subprocess.py View on Github external
@sio_on("register_client")
async def on_register_client(engine, sid, kwargs):
    """Register client."""
    logger = engine.logger
    conn_data = engine.store
    logger.info("Registering client: %s", kwargs)
    client_id = kwargs.get("id", str(uuid.uuid4()))
    workspace = kwargs.get("workspace", "default")
    session_id = kwargs.get("session_id", str(uuid.uuid4()))
    base_url = kwargs.get("base_url", engine.opt.base_url)
    if base_url.endswith("/"):
        base_url = base_url[:-1]

    token = kwargs.get("token")
    if token != engine.opt.token:
        logger.error("Wrong connection token (%s)", token)
        print("========>> Connection token: {} <<========".format(engine.opt.token))
github oeway / ImJoy-Engine / imjoy / services / file_server.py View on Github external
@sio_on("get_file_url")
async def on_get_file_url(engine, sid, kwargs):
    """Return file url."""
    logger = engine.logger
    generated_url_files = engine.store.generated_url_files
    generated_urls = engine.store.generated_urls
    registered_sessions = engine.store.registered_sessions
    logger.info("Generating file url: %s", kwargs)
    if sid not in registered_sessions:
        logger.debug("Client %s is not registered", sid)
        return {"success": False, "error": "client has not been registered"}

    path = os.path.abspath(os.path.expanduser(kwargs["path"]))
    if not os.path.exists(path):
        return {"success": False, "error": "file does not exist."}
    file_info = {"path": path}
    if os.path.isdir(path):
github oeway / ImJoy-Engine / imjoy / connection / handler.py View on Github external
@sio_on("disconnect")
async def disconnect(engine, sid):
    """Disconnect client."""
    logger = engine.logger
    # services and runners can register disconnect_* handlers that will be called here
    for event, handler in engine.conn.sio.handlers[NAME_SPACE].items():
        if not event.startswith("disconnect_"):
            continue
        await handler(sid)
    logger.info("Client(%s) disconnected", sid)