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;
}
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);
}
}
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);
}
}
}
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);
}
}
Aggregations