package com.ld.io.netty;
|
|
import com.ld.io.api.IoSession;
|
import com.ld.io.api.IoSessionListener;
|
import com.ld.io.api.IoSessionQuery;
|
import io.netty.channel.Channel;
|
import io.netty.channel.ChannelHandlerContext;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.net.InetSocketAddress;
|
import java.util.*;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
public class NettySessionFactory implements IoSessionQuery {
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
private static Map<String, NettySession> sessionsMap = new ConcurrentHashMap<>(50);
|
private IoSessionListener sessionListener;
|
|
NettySessionFactory(IoSessionListener sessionListener) {
|
this.sessionListener = sessionListener;
|
}
|
|
@Override
|
public List<IoSession> getAllSession() {
|
Set<Map.Entry<String, NettySession>> entries = sessionsMap.entrySet();
|
List<IoSession> list = new ArrayList<>(entries.size());
|
for (Map.Entry<String, NettySession> entry : entries) {
|
list.add(entry.getValue());
|
}
|
return list;
|
}
|
|
@Override
|
public IoSession getSession(String id) {
|
if ("".equals(id) || id == null) {
|
return null;
|
}
|
return sessionsMap.get(id);
|
}
|
|
@Override
|
public IoSession getSessionByBusinessKey(String businessKey) {
|
if ("".equals(businessKey) || businessKey == null) {
|
return null;
|
}
|
return sessionsMap.entrySet().stream().map(Map.Entry::getValue)
|
.filter(session -> businessKey.equals(session.getBusinessKey())).findFirst().orElse(null);
|
}
|
|
public IoSession getSession(NettyChannel nettyChannel) {
|
return sessionsMap.get(nettyChannel.getCtx().channel().id().asLongText());
|
}
|
|
IoSession getSession(ChannelHandlerContext ctx) {
|
return sessionsMap.get(ctx.channel().id().asLongText());
|
}
|
|
void create(NettyChannel nettyChannel) {
|
Channel channel = nettyChannel.getCtx().channel();
|
String id = channel.id().asLongText();
|
InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
|
|
NettySession nettySession = new NettySession();
|
nettySession.setId(id);
|
nettySession.setAddress(address.getAddress().getHostAddress());
|
nettySession.setPort(address.getPort());
|
nettySession.setCreateTime(new Date());
|
nettySession.setNettyChannel(nettyChannel);
|
nettySession.setNettySessionFactory(this);
|
|
sessionsMap.put(id, nettySession);
|
logger.info("Add new session to IoSessionFactory, nettySession=" + nettySession.toString());
|
fireCreateEvent(nettySession);
|
}
|
|
void destroy(ChannelHandlerContext ctx) {
|
this.destroy(ctx.channel().id().asLongText());
|
}
|
|
private void destroy(String id) {
|
NettySession nettySession = sessionsMap.get(id);
|
if (nettySession != null) {
|
destroy(nettySession);
|
}
|
}
|
|
void destroy(NettySession nettySession) {
|
logger.info("Destroy session by ctx, id={}, businessKey={}", nettySession.getId(), nettySession.getBusinessKey());
|
nettySession.getNettyChannel().destroy();
|
sessionsMap.remove(nettySession.getId());
|
fireDestroyEvent(nettySession);
|
}
|
|
private void fireCreateEvent(NettySession nettySession) {
|
if (sessionListener != null) {
|
try {
|
sessionListener.onCreate(nettySession);
|
} catch (Exception e) {
|
logger.error("Create session error, bizKey=" + nettySession.getBusinessKey(), e);
|
}
|
|
}
|
}
|
|
private void fireDestroyEvent(NettySession nettySession) {
|
if (sessionListener != null) {
|
try {
|
sessionListener.onDestroy(nettySession);
|
} catch (Exception e) {
|
logger.error("Destroy session error, bizKey=" + nettySession.getBusinessKey(), e);
|
}
|
|
}
|
}
|
|
|
}
|