Search in sources :

Example 1 with CliSession

use of act.cli.CliSession in project actframework by actframework.

the class SessionScope method get.

@Override
public <T> T get(String key) {
    ActionContext actionContext = ActionContext.current();
    if (null != actionContext) {
        H.Session session = actionContext.session();
        T t = session.cached(key);
        if (null != t) {
            session.cache(key, t, TTL);
        }
        return t;
    }
    CliContext cliContext = CliContext.current();
    if (null != cliContext) {
        CliSession cliSession = cliContext.session();
        return cliSession.attribute(key);
    }
    return null;
}
Also used : H(org.osgl.http.H) ActionContext(act.app.ActionContext) CliContext(act.cli.CliContext) CliSession(act.cli.CliSession)

Example 2 with CliSession

use of act.cli.CliSession in project actframework by actframework.

the class SessionScope method put.

@Override
public <T> void put(String key, T t) {
    ActionContext actionContext = ActionContext.current();
    if (null != actionContext) {
        actionContext.session().cache(key, t, TTL);
    }
    CliContext cliContext = CliContext.current();
    if (null != cliContext) {
        CliSession cliSession = cliContext.session();
        cliSession.attribute(key, t);
    }
}
Also used : ActionContext(act.app.ActionContext) CliContext(act.cli.CliContext) CliSession(act.cli.CliSession)

Example 3 with CliSession

use of act.cli.CliSession in project actframework by actframework.

the class CliServer method run.

@Override
public void run() {
    while (running()) {
        Socket socket;
        try {
            socket = serverSocket.accept();
            InetAddress addr = socket.getInetAddress();
            if (!addr.isLoopbackAddress() && !addr.isSiteLocalAddress() && !addr.isLinkLocalAddress()) {
                logger.warn("remote connection request rejected: " + addr.getHostAddress());
                socket.close();
                continue;
            }
            CliSession session = new CliSession(socket, this);
            sessions.put(session.id(), session);
            executor.submit(session);
        } catch (Exception e) {
            if (isDestroyed()) {
                return;
            }
            logger.error(e, "Error processing CLI session");
            stop();
            return;
        } finally {
        // Note we cannot close socket here. The ownership
        // of socket has been transferred to the CliSession
        // and it is up to the session to manage the socket
        // IO.close(socket);
        }
    }
}
Also used : InetAddress(java.net.InetAddress) CliSession(act.cli.CliSession) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ConfigurationException(org.osgl.exception.ConfigurationException)

Example 4 with CliSession

use of act.cli.CliSession in project actframework by actframework.

the class CliServer method start.

void start() {
    if (running()) {
        return;
    }
    try {
        serverSocket = new ServerSocket(port);
        running.set(true);
        // start server thread
        executor.submit(this);
        // start expiration monitor thread
        executor.submit(new Runnable() {

            @Override
            public void run() {
                monitorThread = Thread.currentThread();
                int expiration = app().config().cliSessionExpiration();
                while (running()) {
                    List<CliSession> toBeRemoved = new ArrayList<>();
                    for (CliSession session : sessions.values()) {
                        if (session.expired(expiration)) {
                            toBeRemoved.add(session);
                        }
                    }
                    for (CliSession session : toBeRemoved) {
                        session.stop("Your session has expired");
                        sessions.remove(session.id());
                    }
                    try {
                        Thread.sleep(60 * 1000);
                    } catch (InterruptedException e) {
                        return;
                    }
                    app().checkUpdates(false);
                }
            }
        });
        app().jobManager().on(SysEventId.ACT_START, new Runnable() {

            @Override
            public void run() {
                Act.LOGGER.info("CLI server started on port: %s", port);
            }
        });
    } catch (IOException e) {
        Throwable t = e.getCause();
        if (null != t && t.getMessage().contains("Address already in use")) {
            throw new ConfigurationException("Cannot start app, port[%s] already occupied. Possible cause: another app instance is running", port);
        }
        throw new ConfigurationException(e, "Cannot start CLI server on port: %s", port);
    }
}
Also used : ConfigurationException(org.osgl.exception.ConfigurationException) ServerSocket(java.net.ServerSocket) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) CliSession(act.cli.CliSession)

Aggregations

CliSession (act.cli.CliSession)4 ActionContext (act.app.ActionContext)2 CliContext (act.cli.CliContext)2 IOException (java.io.IOException)2 ServerSocket (java.net.ServerSocket)2 ConfigurationException (org.osgl.exception.ConfigurationException)2 InetAddress (java.net.InetAddress)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 H (org.osgl.http.H)1