com.gateway.connector.tcp.TcpSessionManager Maven / Gradle / Ivy
package com.gateway.connector.tcp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gateway.connector.Connection;
import com.gateway.connector.Session;
import com.gateway.connector.api.ExchangeSession;
import com.gateway.connector.api.listener.SessionListener;
import io.netty.channel.ChannelHandlerContext;
public class TcpSessionManager extends ExchangeTcpSessionManager {
private final static Logger logger = LoggerFactory.getLogger(TcpSessionManager.class);
@Override
public synchronized Session createSession(String userName, String sessionId, ChannelHandlerContext ctx) {
Session session = sessions.get(sessionId);
if (session != null) {
logger.info("session " + sessionId + " exist!");
/**
* Connection(1)Channel,Connection(2)
* session.closectx, Connection(2)Connection(1)Channel
* Connection(3),Session
* Connection(3)Connection(1/2)Channel
* **/
// sessionsession
session.close();
logger.info("session " + sessionId + " have been closed!");
}
logger.info("create new session " + sessionId + ", ctx -> " + ctx.toString());
session = new ExchangeSession();
session.setSessionId(sessionId);
session.setValid(true);
session.setMaxInactiveInterval(this.getMaxInactiveInterval());
session.setCreationTime(System.currentTimeMillis());
session.setLastAccessedTime(System.currentTimeMillis());
session.setSessionManager(this);
session.setUserName(userName);
session.setConnection(createTcpConnection(session, ctx));
logger.info("create new session " + sessionId + " successful!");
for (SessionListener listener : sessionListeners) {
session.addSessionListener(listener);
}
logger.debug("add listeners to session " + sessionId + " successful! " + sessionListeners);
return session;
}
protected Connection createTcpConnection(Session session, ChannelHandlerContext ctx) {
Connection conn = new TcpConnection(ctx);
conn.setConnectionId(session.getSessionId());
conn.setSession(session);
return conn;
}
}