|
@@ -0,0 +1,103 @@
|
|
|
+package com.its.api.xnettcp.client;
|
|
|
+
|
|
|
+import com.its.api.xnettcp.client.listener.NettyTcpClientCloseListener;
|
|
|
+import com.its.api.xnettcp.client.listener.NettyTcpClientConnectListener;
|
|
|
+import io.netty.bootstrap.Bootstrap;
|
|
|
+import io.netty.channel.Channel;
|
|
|
+import io.netty.channel.ChannelFuture;
|
|
|
+import io.netty.channel.ChannelFutureListener;
|
|
|
+import lombok.Getter;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.Setter;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.util.concurrent.Callable;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Getter
|
|
|
+@Setter
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class NettyTcpClientVds implements Callable<Object> {
|
|
|
+
|
|
|
+ private final String ipAddress;
|
|
|
+ private final int port;
|
|
|
+ private final int reconnectTime;
|
|
|
+ private final NettyTcpClientVdsBootstrapFactory bootstrapFactory;
|
|
|
+ private Bootstrap clientBootstrap = null;
|
|
|
+ private ChannelFuture channelFuture = null;
|
|
|
+ private NettyTcpClientConnectListener connectListener = null;
|
|
|
+ private NettyTcpClientCloseListener closeListener = null;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object call() throws Exception {
|
|
|
+ log.info("NettyTcpClientVds start: {}, {}", this.ipAddress, this.port);
|
|
|
+ try {
|
|
|
+ if (this.clientBootstrap == null) {
|
|
|
+ this.clientBootstrap = this.bootstrapFactory.createBootstrap();
|
|
|
+ }
|
|
|
+// if (this.connectListener == null) {
|
|
|
+// this.connectListener = new NettyTcpClientConnectListener(this);
|
|
|
+// }
|
|
|
+// if (this.closeListener == null) {
|
|
|
+// this.closeListener = new NettyTcpClientCloseListener(this);
|
|
|
+// }
|
|
|
+// this.channel = this.channelFuture
|
|
|
+// .addListener(this.connectListener)
|
|
|
+// .channel()
|
|
|
+// .closeFuture()
|
|
|
+// .addListener(this.closeListener)
|
|
|
+// .channel();
|
|
|
+ connect();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("Exception: {}", e.getMessage());
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void connect() {
|
|
|
+ log.error("NettyTcpClientVds try connect: {}, {}", this.ipAddress, this.port);
|
|
|
+ if (this.channelFuture != null && this.channelFuture.channel() != null) {
|
|
|
+ this.channelFuture.channel().close();
|
|
|
+ this.channelFuture = null;
|
|
|
+ }
|
|
|
+ this.channelFuture = this.clientBootstrap.connect(new InetSocketAddress(this.ipAddress, this.port));
|
|
|
+ this.channelFuture.addListener(new ChannelFutureListener() {
|
|
|
+ // 연결에 대한 리스터 추가
|
|
|
+ @Override
|
|
|
+ public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
+ if (future.isSuccess()) {
|
|
|
+ log.error("Channel open");
|
|
|
+ channelOpen(future.channel());
|
|
|
+ } else {
|
|
|
+ log.error("Could not connect to server, {}", future.cause().toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
|
|
|
+ // 종료에 대한 리스너 추가
|
|
|
+ @Override
|
|
|
+ public void operationComplete(ChannelFuture future) throws Exception {
|
|
|
+ log.error("Channel closed, {}", future.toString());
|
|
|
+ channelClosed(future.channel());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public Channel getChannel() {
|
|
|
+ return this.channelFuture != null ? this.channelFuture.channel() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void channelOpen(Channel channel) {
|
|
|
+ log.error("channelOpen: channel {}, channelFuture.channel {}", channel, this.channelFuture.channel());
|
|
|
+ }
|
|
|
+
|
|
|
+ protected synchronized void channelClosed(Channel channel) {
|
|
|
+ log.error("channelClosed: channel {}, channelFuture.channel {}", channel, this.channelFuture.channel());
|
|
|
+ this.channelFuture.channel().close();
|
|
|
+ this.channelFuture.channel().eventLoop().schedule(this, this.getReconnectTime(), TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|