shjung 3 жил өмнө
parent
commit
e5ca12b3b7

+ 39 - 0
src/main/java/com/its/app/utils/IOUtils.java

@@ -0,0 +1,39 @@
+package com.its.app.utils;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class IOUtils {
+    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+
+    public static byte[] toByteArray(InputStream input) throws IOException {
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        copy(input, output);
+        return output.toByteArray();
+    }
+
+    public static int copy(InputStream input, OutputStream output)
+            throws IOException {
+        long count = copyLarge(input, output);
+        if (count > Integer.MAX_VALUE) {
+            return -1;
+        }
+        return (int) count;
+    }
+
+    public static long copyLarge(InputStream input, OutputStream output)
+            throws IOException {
+        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+        long count = 0;
+        int n = 0;
+        while (-1 != (n = input.read(buffer))) {
+            output.write(buffer, 0, n);
+            count += n;
+        }
+
+        return count;
+
+    }
+}

+ 30 - 0
src/main/java/com/its/vds/entity/TbVdsCtlr.java

@@ -4,15 +4,19 @@ import com.its.app.utils.SysUtils;
 import com.its.vds.domain.NET;
 import com.its.vds.xnettcp.vds.protocol.*;
 import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.ToString;
+import lombok.extern.slf4j.Slf4j;
 
 import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
+@Slf4j
 @Getter
 @Setter
 @ToString
@@ -150,4 +154,30 @@ public class TbVdsCtlr {
 
 		//reqTraffic
 	}
+
+	/**
+	 * Channel Send Data
+	 * @param sendBuff
+	 * @param delayMilliSeconds
+	 * @param packetDesc
+	 * @return
+	 */
+	public boolean sendData(ByteBuffer sendBuff, int delayMilliSeconds, String packetDesc) {
+		boolean result = false;
+		if (this.channel != null) {
+			ChannelFuture f = this.channel.writeAndFlush(sendBuff);
+			f.awaitUninterruptibly();
+			if (f.isDone() || f.isSuccess()) {
+				result = true;
+				log.info("SEND_0: [{}], {}: {} sendBytes. [{}]", this.VDS_CTLR_IP, packetDesc, sendBuff.array().length, this.VDS_CTLR_ID);
+				if (delayMilliSeconds > 0) {
+					VdsProtocol.sleep(delayMilliSeconds);
+				}
+			} else {
+				log.error("SEND_0: [{}], {}: {} sendBytes, Failed. [{}]", this.VDS_CTLR_IP, packetDesc, sendBuff.array().length, this.VDS_CTLR_ID);
+			}
+
+		}
+		return result;
+	}
 }

+ 8 - 27
src/main/java/com/its/vds/xnettcp/vds/VdsTcpClient.java

@@ -6,7 +6,6 @@ import com.its.vds.xnettcp.vds.codec.VdsTcpClientDecoder;
 import com.its.vds.xnettcp.vds.codec.VdsTcpClientEncoder;
 import com.its.vds.xnettcp.vds.handler.VdsTcpClientIdleHandler;
 import com.its.vds.xnettcp.vds.handler.VdsTcpClientInboundHandler;
-import com.its.vds.xnettcp.vds.protocol.VdsProtocol;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.*;
 import io.netty.channel.socket.SocketChannel;
@@ -109,32 +108,14 @@ public class VdsTcpClient implements Callable<Object> {
         // 온도정보 요청
         ByteBuffer sendBuff;
         sendBuff = obj.getReqSynchronize().getByteBuffer();
-        ChannelFuture f = obj.getChannel().writeAndFlush(sendBuff);
-        f.awaitUninterruptibly();
-        if (f.isDone() || f.isSuccess()) {
-            log.info("SEND_0: [{}], vds_Synchronization: {} sendBytes. [{}]", obj.getVDS_CTLR_IP(), sendBuff.array().length, obj.getVDS_CTLR_ID());
-        } else {
-            log.error("SEND_0: [{}], vds_Synchronization: {} sendBytes, Failed. [{}]", obj.getVDS_CTLR_IP(), sendBuff.array().length, obj.getVDS_CTLR_ID());
-        }
-        VdsProtocol.sleep(500);
-
-        ByteBuffer sendBuff_ = obj.getReqData().getByteBuffer();
-        ChannelFuture f_ = obj.getChannel().writeAndFlush(sendBuff_);
-        f_.awaitUninterruptibly();
-        if (f_.isDone() || f_.isSuccess()) {
-            log.info("SEND_0: [{}], vds_Data: {} sendBytes. [{}]", obj.getVDS_CTLR_IP(), sendBuff_.array().length, obj.getVDS_CTLR_ID());
-        } else {
-            log.error("SEND_0: [{}], vds_Data: {} sendBytes, Failed. [{}]", obj.getVDS_CTLR_IP(), sendBuff_.array().length, obj.getVDS_CTLR_ID());
-        }
-        VdsProtocol.sleep(500);
-
-        ByteBuffer sendBuff__ = obj.getReqTemperature().getByteBuffer();
-        ChannelFuture f__ = obj.getChannel().writeAndFlush(sendBuff__);
-        f__.awaitUninterruptibly();
-        if (f__.isDone() || f__.isSuccess()) {
-            log.info("SEND_0: [{}], vds_Temperature: {} sendBytes. [{}]", obj.getVDS_CTLR_IP(), sendBuff__.array().length, obj.getVDS_CTLR_ID());
-        } else {
-            log.error("SEND_0: [{}], vds_Temperature: {} sendBytes, Failed. [{}]", obj.getVDS_CTLR_IP(), sendBuff__.array().length, obj.getVDS_CTLR_ID());
+        boolean result = obj.sendData(sendBuff, 500, "vds_Synchronization");
+        if (result) {
+            ByteBuffer sendBuff_ = obj.getReqData().getByteBuffer();
+            result = obj.sendData(sendBuff_, 500, "vds_Data");
+            if (result) {
+                ByteBuffer sendBuff__ = obj.getReqTemperature().getByteBuffer();
+                result = obj.sendData(sendBuff__, 500, "vds_Temperature");
+            }
         }
     }
 

+ 7 - 5
src/main/java/com/its/vds/xnettcp/vds/codec/VdsTcpClientEncoder.java

@@ -1,6 +1,7 @@
 package com.its.vds.xnettcp.vds.codec;
 
 import com.its.app.utils.NettyUtils;
+import com.its.app.utils.SysUtils;
 import com.its.vds.entity.TbVdsCtlr;
 import com.its.vds.global.AppRepository;
 import com.its.vds.xnettcp.vds.handler.VdsTcpClientIdleHandler;
@@ -32,8 +33,6 @@ public class VdsTcpClientEncoder extends MessageToByteEncoder<Object> {
             return;
         }
 
-        MDC.put("id", obj.getLogKey());
-
         if (!channel.isOpen() || !channel.isActive()) {
             log.error("VdsTcpClientEncoder.encode: {}, isOpen: {}, isActive: {}. [{}]", ipAddress, channel.isOpen(), channel.isActive(), obj.getLogKey());
             VdsTcpClientIdleHandler.disconnectChannel(channel);
@@ -46,13 +45,16 @@ public class VdsTcpClientEncoder extends MessageToByteEncoder<Object> {
             return;
         }
 
+        MDC.put("id", obj.getLogKey());
+
         ByteBuffer buffer = (ByteBuffer)msg;
         byte[] sendBytes = buffer.array();
         outByteBuf.writeBytes(sendBytes);
         log.info("SEND_P: [{}], SEND.{}, {} Bytes. [{}]", obj.getVDS_CTLR_IP(), VdsProtocol.getOpCodeName(sendBytes[VdsReqFrameHead.OPCODE_POS]), sendBytes.length, obj.getLogKey());
-//        if (this.serverConfig.getTcpSendDump().equals("true")) {
-//            log.info("SEND: [{}], {} Bytes. {}", ipAddress, sendBytes.length, SysUtils.byteArrayToHex(sendBytes));
-//        }
+        if (obj.isDump()) {
+            log.info("SEND: [{}], {} Bytes. {}", ipAddress, sendBytes.length, SysUtils.byteArrayToHex(sendBytes));
+        }
+
         MDC.remove(obj.getLogKey());
         MDC.clear();
     }

+ 1 - 0
src/main/java/com/its/vds/xnettcp/vds/protocol/VdsProtocol.java

@@ -41,6 +41,7 @@ public class VdsProtocol {
 //    Data Field	0 ~ 254 BYTE	    OP-Code 별로 유동적임
 
     public static final ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
+    public static boolean isDleStuffing = true;
     public static final int MIN_PACKET_SIZE = 11;
 
     public static final byte vds_DLE    = (byte)0x10;

+ 81 - 0
src/main/java/com/its/vds/xnettcp/vds/protocol/VdsReqFramePacket.java

@@ -1,9 +1,12 @@
 package com.its.vds.xnettcp.vds.protocol;
 
+import com.google.common.primitives.Bytes;
 import lombok.Getter;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.List;
 
 @Getter
 public class VdsReqFramePacket {
@@ -41,6 +44,84 @@ public class VdsReqFramePacket {
      * @return
      */
     public ByteBuffer getByteBuffer() {
+        if (VdsProtocol.isDleStuffing) {
+            return getDleBuffer();
+        }
+        return getBuffer();
+    }
+
+    private ByteBuffer getDleBuffer() {
+        List<Byte> list = new ArrayList<Byte>();
+        int bodySize = 0;
+        if (this.body != null) {
+            bodySize = this.body.length;
+        }
+
+        // HEAD
+        list.add(this.head.getDle());
+        list.add(this.head.getStx());
+
+        byte groupNo1 = (byte)((this.head.getGroupNo() >> 8 ) & 0xFF);
+        byte groupNo2 = (byte)((this.head.getGroupNo()      ) & 0xFF);
+        if (VdsProtocol.byteOrder == ByteOrder.LITTLE_ENDIAN) {
+            groupNo2 = (byte)((this.head.getGroupNo() >> 8 ) & 0xFF);
+            groupNo1 = (byte)((this.head.getGroupNo()      ) & 0xFF);
+        }
+        list.add(groupNo1);
+        if (groupNo1 == VdsProtocol.vds_DLE) {
+            list.add(groupNo1);
+        }
+        list.add(groupNo2);
+        if (groupNo2 == VdsProtocol.vds_DLE) {
+            list.add(groupNo2);
+        }
+
+        byte controllerNo1 = (byte)((this.head.getControllerNo() >> 8 ) & 0xFF);
+        byte controllerNo2 = (byte)((this.head.getControllerNo()      ) & 0xFF);
+        if (VdsProtocol.byteOrder == ByteOrder.LITTLE_ENDIAN) {
+            controllerNo2 = (byte)((this.head.getControllerNo() >> 8 ) & 0xFF);
+            controllerNo1 = (byte)((this.head.getControllerNo()      ) & 0xFF);
+        }
+        list.add(controllerNo1);
+        if (controllerNo1 == VdsProtocol.vds_DLE) {
+            list.add(controllerNo1);
+        }
+        list.add(controllerNo2);
+        if (controllerNo2 == VdsProtocol.vds_DLE) {
+            list.add(controllerNo2);
+        }
+
+        list.add(this.head.getOpCode());
+
+        // BODY
+        if (bodySize > 0) {
+            for (byte b: this.body) {
+                list.add(b);
+                if (b == VdsProtocol.vds_DLE) {
+                    list.add(b);
+                }
+            }
+        }
+
+        // TAIL
+        list.add(this.tail.getDle());
+        list.add(this.tail.getEtx());
+        byte crc1 = (byte)((this.tail.getCrc() & 0xFF00) >> 8);
+        byte crc2 = (byte)((this.tail.getCrc() & 0x00FF));
+        if (VdsProtocol.byteOrder == ByteOrder.LITTLE_ENDIAN) {
+            crc2 = (byte)((this.tail.getCrc() >> 8 ) & 0xFF);
+            crc1 = (byte)((this.tail.getCrc()      ) & 0xFF);
+        }
+        list.add(crc1);
+        list.add(crc2);
+
+        byte[] arr = Bytes.toArray(list);
+        ByteBuffer byteBuffer = ByteBuffer.allocate(arr.length);
+        byteBuffer.order(this.byteOrder);
+        byteBuffer.put(arr);
+        return byteBuffer;
+    }
+    private ByteBuffer getBuffer() {
         int bodySize = 0;
         if (this.body != null) {
             bodySize = this.body.length;

+ 31 - 2
src/test/java/com/its/app/VdsCommServerApplicationTests.java

@@ -1,5 +1,6 @@
 package com.its.app;
 
+import com.google.common.primitives.Bytes;
 import com.its.app.utils.BcdConverter;
 import com.its.app.utils.SysUtils;
 import com.its.vds.VdsCommServerApplication;
@@ -9,6 +10,9 @@ import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @Slf4j
 @SpringBootTest(classes = VdsCommServerApplication.class)
 public class VdsCommServerApplicationTests {
@@ -47,8 +51,8 @@ public class VdsCommServerApplicationTests {
         byte[] recvBytes = new byte[readableBytes];
         recvBytes[ 0] = (byte)0x10;
         recvBytes[ 1] = (byte)0x02;
-        recvBytes[ 2] = (byte)0x00;
-        recvBytes[ 3] = (byte)0x00;
+        recvBytes[ 2] = (byte)0x10;
+        recvBytes[ 3] = (byte)0x10;
         recvBytes[ 4] = (byte)0x00;
         recvBytes[ 5] = (byte)0x14;
         recvBytes[ 6] = (byte)0x01;
@@ -135,6 +139,31 @@ public class VdsCommServerApplicationTests {
                 break;
             }
         }
+    }
+
+    @Test
+    void Test3() {
+        int readableBytes = 10;
+        byte[] recvBytes = new byte[readableBytes];
+        recvBytes[ 0] = (byte)0x10;
+        recvBytes[ 1] = (byte)0x02;
+        recvBytes[ 2] = (byte)0x10;
+        recvBytes[ 3] = (byte)0x10;
+        recvBytes[ 4] = (byte)0x00;
+        recvBytes[ 5] = (byte)0x14;
+        recvBytes[ 6] = (byte)0x01;
+        recvBytes[ 7] = (byte)0x01;
+        recvBytes[ 8] = (byte)0x10;
+        recvBytes[ 9] = (byte)0x03;
+        log.error("{}", SysUtils.byteArrayToHex(recvBytes));
+        List<Byte> list = new ArrayList<Byte>();
+        for (byte b : recvBytes) {
+            list.add(b);
+        }
+
+        log.error("LENGTH: {}", list.size());
 
+        byte[] arr = Bytes.toArray(list);
+        log.error("{}", SysUtils.byteArrayToHex(arr));
     }
 }