|
@@ -0,0 +1,445 @@
|
|
|
|
+package com.persagy.iottransfer.communication.mina.tcp.server;
|
|
|
|
+
|
|
|
|
+import com.persagy.iottransfer.communication.entity.Packet;
|
|
|
|
+import com.persagy.iottransfer.communication.entity.PacketEntity;
|
|
|
|
+import com.persagy.iottransfer.communication.mina.codec.MinaCodecFactory;
|
|
|
|
+import com.persagy.iottransfer.communication.util.*;
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
+import lombok.Builder;
|
|
|
|
+import lombok.Data;
|
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.mina.core.session.IoSession;
|
|
|
|
+import org.apache.mina.filter.codec.ProtocolCodecFactory;
|
|
|
|
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
|
|
|
|
+import org.apache.mina.filter.ssl.SslFilter;
|
|
|
|
+import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
|
|
|
|
+
|
|
|
|
+import java.net.InetSocketAddress;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Data
|
|
|
|
+@Builder
|
|
|
|
+@NoArgsConstructor
|
|
|
|
+@AllArgsConstructor
|
|
|
|
+public class TCPServerManager implements IServerManager {
|
|
|
|
+ String ip;
|
|
|
|
+ int port;
|
|
|
|
+ String encoding;
|
|
|
|
+ String ssl_protocol;
|
|
|
|
+ String ssl_keystoretype;
|
|
|
|
+ String ssl_algorithm;
|
|
|
|
+ byte[] ssl_file_km;
|
|
|
|
+ String ssl_password_km;
|
|
|
|
+ byte[] ssl_file_tm;
|
|
|
|
+ String ssl_password_tm;
|
|
|
|
+ String aes_password;
|
|
|
|
+ boolean compress = false;
|
|
|
|
+ int Max_size = 1000;
|
|
|
|
+
|
|
|
|
+ boolean separate;
|
|
|
|
+ boolean separateBytes;
|
|
|
|
+ byte prefix;
|
|
|
|
+ byte suffix;
|
|
|
|
+ byte[] prefixBytes;
|
|
|
|
+ byte[] suffixBytes;
|
|
|
|
+
|
|
|
|
+ Integer IdleTime = null;
|
|
|
|
+
|
|
|
|
+ IServerHandler handlerManager;
|
|
|
|
+ ILogger logger;
|
|
|
|
+
|
|
|
|
+ public final Map<String, Date> connectDateMap = new HashMap<String, Date>();
|
|
|
|
+ public final PacketBuffer<PacketEntity> receList = new PacketBuffer<PacketEntity>();
|
|
|
|
+
|
|
|
|
+ NioSocketAcceptor acceptor;
|
|
|
|
+
|
|
|
|
+ public final Map<String, IoSession> socketMap = new HashMap<String, IoSession>();
|
|
|
|
+
|
|
|
|
+ public final Map<String, String> project2IoSessionMap = new HashMap<String, String>();
|
|
|
|
+ int remark_count = 0;
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, int Max_size) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, int Max_size, byte prefix, byte suffix) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = false;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.suffix = suffix;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, int Max_size, byte[] prefixBytes,
|
|
|
|
+ byte[] suffixBytes) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = true;
|
|
|
|
+ this.prefixBytes = prefixBytes;
|
|
|
|
+ this.suffixBytes = suffixBytes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, int Max_size) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, int Max_size, byte prefix,
|
|
|
|
+ byte suffix) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = false;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.suffix = suffix;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, int Max_size, byte[] prefixBytes,
|
|
|
|
+ byte[] suffixBytes) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = true;
|
|
|
|
+ this.prefixBytes = prefixBytes;
|
|
|
|
+ this.suffixBytes = suffixBytes;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, Integer IdleTime, int Max_size) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = false;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, Integer IdleTime, int Max_size,
|
|
|
|
+ byte prefix, byte suffix) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = false;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.suffix = suffix;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, boolean compress, Integer IdleTime, int Max_size,
|
|
|
|
+ byte[] prefixBytes, byte[] suffixBytes) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = true;
|
|
|
|
+ this.prefixBytes = prefixBytes;
|
|
|
|
+ this.suffixBytes = suffixBytes;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String aes_password, boolean compress,
|
|
|
|
+ Integer IdleTime, int Max_size) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.aes_password = aes_password;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = false;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String aes_password, boolean compress,
|
|
|
|
+ Integer IdleTime, int Max_size, byte prefix, byte suffix) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.aes_password = aes_password;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = false;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.suffix = suffix;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String aes_password, boolean compress,
|
|
|
|
+ Integer IdleTime, int Max_size, byte[] prefixBytes, byte[] suffixBytes) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.aes_password = aes_password;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = true;
|
|
|
|
+ this.prefixBytes = prefixBytes;
|
|
|
|
+ this.suffixBytes = suffixBytes;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String ssl_protocol, String ssl_keystoretype,
|
|
|
|
+ String ssl_algorithm, byte[] ssl_file_km, String ssl_password_km, byte[] ssl_file_tm,
|
|
|
|
+ String ssl_password_tm, boolean compress, Integer IdleTime, int Max_size) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.ssl_protocol = ssl_protocol;
|
|
|
|
+ this.ssl_keystoretype = ssl_keystoretype;
|
|
|
|
+ this.ssl_algorithm = ssl_algorithm;
|
|
|
|
+ this.ssl_file_km = ssl_file_km;
|
|
|
|
+ this.ssl_password_km = ssl_password_km;
|
|
|
|
+ this.ssl_file_tm = ssl_file_tm;
|
|
|
|
+ this.ssl_password_tm = ssl_password_tm;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = false;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String ssl_protocol, String ssl_keystoretype,
|
|
|
|
+ String ssl_algorithm, byte[] ssl_file_km, String ssl_password_km, byte[] ssl_file_tm,
|
|
|
|
+ String ssl_password_tm, boolean compress, Integer IdleTime, int Max_size, byte prefix, byte suffix) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.ssl_protocol = ssl_protocol;
|
|
|
|
+ this.ssl_keystoretype = ssl_keystoretype;
|
|
|
|
+ this.ssl_algorithm = ssl_algorithm;
|
|
|
|
+ this.ssl_file_km = ssl_file_km;
|
|
|
|
+ this.ssl_password_km = ssl_password_km;
|
|
|
|
+ this.ssl_file_tm = ssl_file_tm;
|
|
|
|
+ this.ssl_password_tm = ssl_password_tm;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = false;
|
|
|
|
+ this.prefix = prefix;
|
|
|
|
+ this.suffix = suffix;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public TCPServerManager(String ip, int port, String encoding, String ssl_protocol, String ssl_keystoretype,
|
|
|
|
+ String ssl_algorithm, byte[] ssl_file_km, String ssl_password_km, byte[] ssl_file_tm,
|
|
|
|
+ String ssl_password_tm, boolean compress, Integer IdleTime, int Max_size, byte[] prefixBytes,
|
|
|
|
+ byte[] suffixBytes) {
|
|
|
|
+ this.ip = ip;
|
|
|
|
+ this.port = port;
|
|
|
|
+ this.encoding = encoding;
|
|
|
|
+ this.ssl_protocol = ssl_protocol;
|
|
|
|
+ this.ssl_keystoretype = ssl_keystoretype;
|
|
|
|
+ this.ssl_algorithm = ssl_algorithm;
|
|
|
|
+ this.ssl_file_km = ssl_file_km;
|
|
|
|
+ this.ssl_password_km = ssl_password_km;
|
|
|
|
+ this.ssl_file_tm = ssl_file_tm;
|
|
|
|
+ this.ssl_password_tm = ssl_password_tm;
|
|
|
|
+ this.compress = compress;
|
|
|
|
+ this.Max_size = Max_size;
|
|
|
|
+
|
|
|
|
+ this.separate = true;
|
|
|
|
+ this.separateBytes = true;
|
|
|
|
+ this.prefixBytes = prefixBytes;
|
|
|
|
+ this.suffixBytes = suffixBytes;
|
|
|
|
+
|
|
|
|
+ this.IdleTime = IdleTime;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setHandlerManager(IServerHandler handlerManager) {
|
|
|
|
+ this.handlerManager = handlerManager;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setLogger(ILogger logger) {
|
|
|
|
+ this.logger = logger;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String Name() {
|
|
|
|
+ return "TCP Server " + this.ip + ":" + this.port;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void Start() {
|
|
|
|
+ if (this.acceptor == null) {
|
|
|
|
+ try {
|
|
|
|
+ NioSocketAcceptor acceptorInner = new NioSocketAcceptor();
|
|
|
|
+ ProtocolCodecFactory factory;
|
|
|
|
+ if (this.separate) {
|
|
|
|
+ if (this.separateBytes) {
|
|
|
|
+ factory = new MinaCodecFactory(this.ip, this.port, this.encoding, this.aes_password,
|
|
|
|
+ this.compress, this.prefixBytes, this.suffixBytes);
|
|
|
|
+ } else {
|
|
|
|
+ factory = new MinaCodecFactory(this.ip, this.port, this.encoding, this.aes_password,
|
|
|
|
+ this.compress, this.prefix, this.suffix);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ factory = new MinaCodecFactory(this.ip, this.port, this.encoding, this.aes_password, this.compress);
|
|
|
|
+ }
|
|
|
|
+ if (this.ssl_file_km != null || this.ssl_file_tm != null) {
|
|
|
|
+ SslFilter sslFilter = new SslFilter(
|
|
|
|
+ MySslContextFactory.getContext(this.ssl_protocol, this.ssl_keystoretype, this.ssl_algorithm,
|
|
|
|
+ this.ssl_file_km, this.ssl_file_tm, this.ssl_password_km, this.ssl_password_tm));
|
|
|
|
+ sslFilter.setUseClientMode(false);
|
|
|
|
+ if (this.ssl_file_tm != null) {
|
|
|
|
+ sslFilter.setNeedClientAuth(true);
|
|
|
|
+ }
|
|
|
|
+ acceptorInner.getFilterChain().addFirst("sslFilter", sslFilter);
|
|
|
|
+ }
|
|
|
|
+ acceptorInner.getFilterChain().addLast("protocol", new ProtocolCodecFilter(factory));
|
|
|
|
+ acceptorInner.setHandler(new TCPServerIoHandler(this));
|
|
|
|
+ acceptorInner.bind(new InetSocketAddress(this.ip, this.port));
|
|
|
|
+ if (this.IdleTime != null) {
|
|
|
|
+ acceptorInner.getSessionConfig().setBothIdleTime(this.IdleTime);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.acceptor = acceptorInner;
|
|
|
|
+
|
|
|
|
+ log.warn(this.Name() + "\tStart");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(ExceptionUtil.Instance().GetMessage(e));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void Stop() {
|
|
|
|
+ for (int i = 0; i < this.remark_count; i++) {
|
|
|
|
+ this.receList.offer(new PacketEntity(), Integer.MAX_VALUE);
|
|
|
|
+ }
|
|
|
|
+ if (this.acceptor != null) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.acceptor.unbind();
|
|
|
|
+ this.acceptor.dispose();
|
|
|
|
+ this.acceptor = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void AllowJustSend_UDP(boolean allow) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized String[] GetClientAddresss() {
|
|
|
|
+ return this.connectDateMap.keySet().toArray(new String[0]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized Date GetConnectTime(String clientAddress) {
|
|
|
|
+ if (this.connectDateMap.containsKey(clientAddress)) {
|
|
|
|
+ return this.connectDateMap.get(clientAddress);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void SetConnectTime(String clientAddress, Date date) {
|
|
|
|
+ this.connectDateMap.remove(clientAddress);
|
|
|
|
+ if (date != null) {
|
|
|
|
+ this.connectDateMap.put(clientAddress, date);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void AppendToSend(String clientAddress, Packet MyPackage) {
|
|
|
|
+ IoSession session = this.socketMap.get(clientAddress);
|
|
|
|
+ if (session != null) {
|
|
|
|
+ session.write(MyPackage);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public void AppendToSendByProject(String project, Packet MyPackage) {
|
|
|
|
+ String clientAddresss = this.project2IoSessionMap.get(project);
|
|
|
|
+ if(clientAddresss != null){
|
|
|
|
+ IoSession session = this.socketMap.get(clientAddresss);
|
|
|
|
+ if (session != null) {
|
|
|
|
+ session.write(MyPackage);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public synchronized boolean ContainsSocket(String clientAddress) {
|
|
|
|
+ return this.socketMap.containsKey(clientAddress);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void AppendToRece(String clientAddress, Packet MyPackage) {
|
|
|
|
+ PacketEntity entity = new PacketEntity();
|
|
|
|
+ entity.address = clientAddress;
|
|
|
|
+ entity.content = MyPackage;
|
|
|
|
+ this.receList.offer(entity, this.Max_size);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public PacketEntity PopRece() {
|
|
|
|
+ PacketEntity entity = this.receList.poll();
|
|
|
|
+ return entity;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public PacketEntity takeRece() throws InterruptedException {
|
|
|
|
+ PacketEntity entity = this.receList.take();
|
|
|
|
+ return entity;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public int ReceBufferSize() {
|
|
|
|
+ return this.receList.BufferSize();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void closeClient(String clientAddress) {
|
|
|
|
+ if (this.socketMap.containsKey(clientAddress)) {
|
|
|
|
+ IoSession session = this.socketMap.get(clientAddress);
|
|
|
|
+ session.close(true);
|
|
|
|
+ this.socketMap.remove(clientAddress);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public synchronized void hasRemark(int count) {
|
|
|
|
+ remark_count = count;
|
|
|
|
+ }
|
|
|
|
+}
|