shjung hai 1 ano
pai
achega
e234b6e742

+ 5 - 1
src/main/java/com/sig/comm/server/dto/NetState.java

@@ -39,13 +39,17 @@ public class NetState {
         this.connectTime = new Date();
         setLastCommTime();
     }
+    public void loginOk() {
+        //this.netState = NET.CONNECTED;
+        this.netState = NET.DATA_TRANS;
+    }
     public void disConnect() {
         this.netState = NET.CLOSED;
         this.channel = null;
         this.disconnectTime = new Date();
     }
     public boolean isActive() {
-        return this.channel == null ? false : this.channel.isActive();
+        return this.channel != null && this.channel.isActive();
     }
     public String getConnectTimeString() {
         if (this.connectTime == null) {

+ 18 - 10
src/main/java/com/sig/comm/server/tcp/handler/SigCommServerInboundMessageHandler.java

@@ -10,7 +10,8 @@ import com.sig.comm.server.process.dbms.DbmsDataType;
 import com.sig.comm.server.process.work.WorkData;
 import com.sig.comm.server.process.work.WorkDataProcess;
 import com.sig.comm.server.service.AppRepositoryService;
-import com.sig.comm.server.tcp.protocol.SigFramePacket;
+import com.sig.comm.server.tcp.protocol.AbstractSigPacket;
+import com.sig.comm.server.tcp.protocol.SigLogin;
 import com.sig.comm.server.tcp.protocol.eSigOpCode;
 import io.netty.buffer.ByteBuf;
 import io.netty.channel.ChannelHandler;
@@ -42,21 +43,28 @@ public class SigCommServerInboundMessageHandler extends SimpleChannelInboundHand
         }
 
         center.getNetState().setLastCommTime();
-        SigFramePacket packet = new SigFramePacket(recvTime, ctx.channel(), byteBuf);
 
-        eSigOpCode opCode = eSigOpCode.getValue(packet.getOpCode());
-        if (opCode == null) {
-            log.error("{}, Received Packet: Invalid opCode: {}. Will be closed.", center.getRegionCd(), packet.getOpCode());
-            closeChannel(ctx);
-            return;
-        }
+        try {
+            AbstractSigPacket packet = AbstractSigPacket.deserialize(recvTime, ctx.channel(), byteBuf);
+            eSigOpCode opCode = eSigOpCode.getValue(packet.getOpCode());
+            if (packet.getOpCode() == eSigOpCode.SIG_LOGOUT.getValue()) {
+                // Login Request
+                SigLogin login = (SigLogin)packet;
+                if (center.getRegionCd().equals(login.getRegionCd())) {
 
-        if (opCode == eSigOpCode.SIG_LOGIN) {
+                }
+                center.getNetState().loginOk();
+            }
             this.workDataProcess.add(new WorkData(opCode, packet));
+            log.info("{}, Receive packet: {} Bytes.", center.getRegionCd(), packet.getMsgLen());
+        }
+        catch (Exception e) {
+            log.error("{}.++channelRead0: Frame packet deserialize failed. Will be closed.: {}", center.getRegionCd(), e.getMessage());
+            closeChannel(ctx);
         }
-        log.info("{}, Receive packet: {} Bytes.", center.getRegionCd(), packet.getPackets().length);
     }
 
+
     @Override
     public void channelActive(ChannelHandlerContext ctx) throws Exception {
         // 여기는 절대 들어 와서는 안되는 로직임....

+ 61 - 12
src/main/java/com/sig/comm/server/tcp/protocol/AbstractSigPacket.java

@@ -4,28 +4,77 @@ import io.netty.buffer.ByteBuf;
 import io.netty.channel.Channel;
 import lombok.Getter;
 import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
 
-import java.util.concurrent.TimeUnit;
-
+@Slf4j
 @Getter
 @Setter
 public abstract class AbstractSigPacket {
 
-    protected final long tvSec;
-    protected final long tvNsec;
-
+    protected final long recvTime;
     protected final Channel channel;
-    protected final ByteBuf byteBuf;
+    protected final int msgLen;
+
+    protected final byte stx;
     protected final byte opCode;
+    protected final String commDate;
+    protected final short year;
+    protected final short month;
+    protected final short day;
+    protected final short hour;
+    protected final short min;
+    protected final short sec;
+    protected final int sequence;
+    protected final int regionId;
+    protected final int dataLength;
+    protected final int count;
 
-    public AbstractSigPacket(Channel channel, ByteBuf byteBuf) {
+    protected final byte chkSum;
+    protected final byte etx;
+    public AbstractSigPacket(long recvTime, Channel channel, ByteBuf byteBuf) {
+        this.recvTime = recvTime;
         this.channel = channel;
-        this.tvSec = TimeUnit.SECONDS.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
-        this.tvNsec = System.nanoTime();
-        this.opCode = (byte)0x00;
-        this.byteBuf = byteBuf;
+        this.msgLen = byteBuf.readableBytes();
+
+        log.error("ReadableBytes: {} Bytes.", byteBuf.readableBytes());
+        log.error("readerIndex: {}", byteBuf.readerIndex());
+        log.error("writerIndex: {}", byteBuf.writerIndex());
+
+        this.stx = byteBuf.readByte();
+        this.opCode = byteBuf.readByte();
+        this.year = byteBuf.readUnsignedByte();
+        this.month = byteBuf.readUnsignedByte();
+        this.day = byteBuf.readUnsignedByte();
+        this.hour = byteBuf.readUnsignedByte();
+        this.min = byteBuf.readUnsignedByte();
+        this.sec = byteBuf.readUnsignedByte();
+        this.commDate = String.format("%4d%02d%02d%02d%02d%02d", this.year+2000, this.month, this.day, this.hour, this.min, this.sec);
+        this.sequence = byteBuf.readUnsignedShort();
+        this.regionId = byteBuf.readUnsignedShort();
+        this.dataLength = byteBuf.readUnsignedShort();
+        this.count = byteBuf.readUnsignedShort();
+
+        log.error("{}/{}/{} {}:{}:{}", this.year+2000, this.month, this.day, this.hour, this.min, this.sec);
+        log.error("{}", this.commDate);
+        log.error("Sequence: {}, RegionId: {}, DataLength: {}, Count: {}", this.sequence, this.regionId, this.dataLength, this.count);
+
+        this.chkSum = byteBuf.getByte(this.msgLen-2);
+        this.etx = byteBuf.getByte(this.msgLen-1);
     }
 
-    public abstract boolean parse();
+    public static AbstractSigPacket deserialize(long recvTime, Channel channel, ByteBuf byteBuf) throws Exception {
+        byte byteOpCode = byteBuf.getByte(SigProtocolConst.SIG_OPCODE_POS);
+        eSigOpCode opCode = eSigOpCode.getValue(byteOpCode);
+        if (opCode == null) {
+            throw new Exception("Unknown OpCode: " + byteOpCode);
+        }
 
+        if (opCode == eSigOpCode.SIG_LOGIN) {
+            return new SigLogin(recvTime, channel, byteBuf);
+        }
+        if (opCode == eSigOpCode.SIG_PHASE_CHANGE) {
+            return new SigPhaseChange(recvTime, channel, byteBuf);
+        }
+        throw new Exception("Not Found OpCode deserialize Object: " + byteOpCode + " : " + opCode);
+    }
 }

+ 53 - 30
src/main/java/com/sig/comm/server/tcp/protocol/SigFramePacket.java

@@ -5,25 +5,24 @@ import io.netty.channel.Channel;
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
 
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
 @Slf4j
 @Getter
 public class SigFramePacket {
     protected final long recvTime;
     protected final Channel channel;
-    private byte[] packets;
-    private int msgLen;
-
-    private byte stx;
-    private byte opCode;
-    private short year, month, day, hour, min, sec;
-    private int sequence;
-    private int regionId;
-    private int dataLength;
-    private int count;
-    private ByteBuffer data;
+    private final int msgLen;
+
+    private final byte stx;
+    private final byte opCode;
+    private final String commDate;
+    private final short year, month, day, hour, min, sec;
+    private final int sequence;
+    private final int regionId;
+    private final int dataLength;
+    private final int count;
+
+    private final byte chkSum;
+    private final byte etx;
 //    pr
 //    STX        - 1 byte
 //    OpCode     - 1 byte
@@ -40,21 +39,45 @@ public class SigFramePacket {
         this.channel = channel;
         this.msgLen = byteBuf.readableBytes();
 
-        this.stx = byteBuf.getByte(0);
-        this.opCode = byteBuf.getByte(1);
-        this.year = byteBuf.getUnsignedByte(2);
-        this.month = byteBuf.getUnsignedByte(3);
-        this.day = byteBuf.getUnsignedByte(4);
-        this.hour = byteBuf.getUnsignedByte(5);
-        this.min = byteBuf.getUnsignedByte(6);
-        this.sec = byteBuf.getUnsignedByte(7);
-        this.sequence = byteBuf.getUnsignedShort(8);
-        this.regionId = byteBuf.getUnsignedShort(10);
-        this.dataLength = byteBuf.getUnsignedShort(12);
-        this.count = byteBuf.getUnsignedShort(14);
-        ByteBuf body = byteBuf.readBytes(16);
-        log.
-        this.data = ByteBuffer.allocate(dataLength);
-        this.data.order(ByteOrder.BIG_ENDIAN);
+        log.error("ReadableBytes: {} Bytes.", byteBuf.readableBytes());
+        log.error("readerIndex: {}", byteBuf.readerIndex());
+        log.error("writerIndex: {}", byteBuf.writerIndex());
+
+        this.stx = byteBuf.readByte();
+        this.opCode = byteBuf.readByte();
+        this.year = byteBuf.readUnsignedByte();
+        this.month = byteBuf.readUnsignedByte();
+        this.day = byteBuf.readUnsignedByte();
+        this.hour = byteBuf.readUnsignedByte();
+        this.min = byteBuf.readUnsignedByte();
+        this.sec = byteBuf.readUnsignedByte();
+        this.commDate = String.format("%4d%02d%02d%02d%02d%02d", this.year+2000, this.month, this.day, this.hour, this.min, this.sec);
+        this.sequence = byteBuf.readUnsignedShort();
+        this.regionId = byteBuf.readUnsignedShort();
+        this.dataLength = byteBuf.readUnsignedShort();
+        this.count = byteBuf.readUnsignedShort();
+
+        log.error("{}/{}/{} {}:{}:{}", this.year+2000, this.month, this.day, this.hour, this.min, this.sec);
+        log.error("{}", this.commDate);
+        log.error("Sequence: {}, RegionId: {}, DataLength: {}, Count: {}", this.sequence, this.regionId, this.dataLength, this.count);
+
+        // SIG_LOGIN
+        if (this.dataLength != 32 + 32 + 32) {
+            log.error("SIG_LOGIN Data Received. But Data Length Error: Required {} Bytes, Received {} Bytes.", 64, this.dataLength);
+        }
+        byte[] loginIdArr = new byte[32];
+        byteBuf.readBytes(loginIdArr);
+        byte[] loginPswdArr = new byte[32];
+        byteBuf.readBytes(loginPswdArr);
+        byte[] loginCdArr = new byte[32];
+        byteBuf.readBytes(loginCdArr);
+        String loginId = new String(loginIdArr);
+        String loginPswd = new String(loginPswdArr);
+        String loginCd = new String(loginCdArr);
+
+        this.chkSum = byteBuf.readByte();
+        this.etx = byteBuf.readByte();
+
+        log.error("{}, {}, {}", loginId, loginPswd, loginCd);
     }
 }

+ 36 - 4
src/main/java/com/sig/comm/server/tcp/protocol/SigLogin.java

@@ -1,8 +1,40 @@
 package com.sig.comm.server.tcp.protocol;
 
-public class SigLogin implements ISigPacket {
-    @Override
-    public boolean process(byte[] packet) {
-        return true;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@Getter
+@Setter
+public class SigLogin extends AbstractSigPacket {
+
+    private String loginId;
+    private String loginPswd;
+    private String regionCd;
+
+    public SigLogin(long recvTime, Channel channel, ByteBuf byteBuf) throws Exception {
+        super(recvTime, channel, byteBuf);
+
+        int dataSize = 32 + 32 + 32;
+        if (this.dataLength != dataSize) {
+            log.error("SIG_LOGIN Data Received. But Data Length Error: Required {} Bytes, Received {} Bytes.", dataSize, this.dataLength);
+            throw new Exception("SIG_LOGIN Data Length Error: Required " + dataSize + " Bytes, Received " + this.dataLength + " Bytes.");
+        }
+
+        byte[] loginIdArr = new byte[32];
+        byteBuf.readBytes(loginIdArr);
+        byte[] loginPswdArr = new byte[32];
+        byteBuf.readBytes(loginPswdArr);
+        byte[] loginCdArr = new byte[32];
+        byteBuf.readBytes(loginCdArr);
+
+        this.loginId = new String(loginIdArr);
+        this.loginPswd = new String(loginPswdArr);
+        this.regionCd = new String(loginCdArr);
+
+        log.error("{}, {}, {}", this.loginId, this.loginPswd, this.regionCd);
     }
 }

+ 21 - 0
src/main/java/com/sig/comm/server/tcp/protocol/SigLoginAck.java

@@ -0,0 +1,21 @@
+package com.sig.comm.server.tcp.protocol;
+
+import io.netty.channel.Channel;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+@Getter
+@Setter
+public abstract class SigLoginAck extends SigSendPacket {
+
+    private byte response;
+    private byte reason;
+    public SigLoginAck(Channel channel, int sequence, int regionId, byte response, byte reason) {
+        super(channel, eSigOpCode.SIG_LOGIN.getValue(), sequence, regionId);
+
+        this.response = response;
+        this.reason = reason;
+    }
+}

+ 73 - 0
src/main/java/com/sig/comm/server/tcp/protocol/SigPhaseChange.java

@@ -0,0 +1,73 @@
+package com.sig.comm.server.tcp.protocol;
+
+import com.sig.comm.server.tcp.protocol.vo.SigPhase;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.Channel;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+public class SigPhaseChange extends AbstractSigPacket {
+
+//    byte int_no [2];
+//    byte grp_id [2];
+
+//    byte mode   [1];
+//    byte ring_a [1];
+//    byte ring_b [1];
+//    byte status [1];
+//    byte count  [1];
+//    byte cycle  [1];
+//
+//    byte phase_a[8];
+//    byte phase_b[8];
+    private List<SigPhase> phases;
+    public SigPhaseChange(long recvTime, Channel channel, ByteBuf byteBuf) throws Exception {
+        super(recvTime, channel, byteBuf);
+
+        int dataSize = this.count * 26;
+        if (this.dataLength != dataSize) {
+            log.error("SIG_PHASE_CHANGE Data Received. But Data Length Error: Required {} Bytes, Received {} Bytes.", dataSize, this.dataLength);
+            throw new Exception("SIG_PHASE_CHANGE Data Length Error: Required " + dataSize + " Bytes, Received " + this.dataLength + " Bytes.");
+        }
+
+        log.info("SIG_PHASE_CHANGE {} EA.", this.count);
+
+        this.phases = new ArrayList<>();
+
+        for (int ii = 0; ii < this.count; ii++) {
+            SigPhase phase = new SigPhase();
+            phase.setIntNo(byteBuf.readUnsignedShort());
+            phase.setGroupNo(byteBuf.readUnsignedShort());
+
+            phase.setMode(byteBuf.readUnsignedByte());
+            phase.setRingA(byteBuf.readUnsignedByte());
+            phase.setRingB(byteBuf.readUnsignedByte());
+            phase.setStatus(byteBuf.readUnsignedByte());
+            phase.setCount(byteBuf.readUnsignedByte());
+            phase.setCycle(byteBuf.readUnsignedByte());
+
+            phase.setPhaseA1(byteBuf.readUnsignedByte());
+            phase.setPhaseA2(byteBuf.readUnsignedByte());
+            phase.setPhaseA3(byteBuf.readUnsignedByte());
+            phase.setPhaseA4(byteBuf.readUnsignedByte());
+            phase.setPhaseA5(byteBuf.readUnsignedByte());
+            phase.setPhaseA6(byteBuf.readUnsignedByte());
+            phase.setPhaseA7(byteBuf.readUnsignedByte());
+            phase.setPhaseA8(byteBuf.readUnsignedByte());
+
+            phase.setPhaseB1(byteBuf.readUnsignedByte());
+            phase.setPhaseB2(byteBuf.readUnsignedByte());
+            phase.setPhaseB3(byteBuf.readUnsignedByte());
+            phase.setPhaseB4(byteBuf.readUnsignedByte());
+            phase.setPhaseB5(byteBuf.readUnsignedByte());
+            phase.setPhaseB6(byteBuf.readUnsignedByte());
+            phase.setPhaseB7(byteBuf.readUnsignedByte());
+            phase.setPhaseB8(byteBuf.readUnsignedByte());
+
+            this.phases.add(phase);
+        }
+    }
+}

+ 61 - 0
src/main/java/com/sig/comm/server/tcp/protocol/SigSendPacket.java

@@ -0,0 +1,61 @@
+package com.sig.comm.server.tcp.protocol;
+
+import io.netty.channel.Channel;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+
+import java.nio.ByteBuffer;
+import java.time.LocalDateTime;
+
+@Slf4j
+@Getter
+@Setter
+public abstract class SigSendPacket {
+
+    protected final long sendTime;
+    protected final Channel channel;
+    protected final int msgLen;
+
+    protected final byte stx;
+    protected final byte opCode;
+    protected final byte year;
+    protected final byte month;
+    protected final byte day;
+    protected final byte hour;
+    protected final byte min;
+    protected final byte sec;
+    protected final int sequence;
+    protected final int regionId;
+    protected final int dataLength;
+    protected final int count;
+
+    protected final byte chkSum;
+    protected final byte etx;
+
+    public SigSendPacket(Channel channel, byte opCode, int sequence, int regionId) {
+        this.sendTime = System.currentTimeMillis();
+        this.channel = channel;
+        this.msgLen = 0;
+        this.stx = SigProtocolConst.SIG_ETX;
+        this.opCode = opCode;
+
+        LocalDateTime now = LocalDateTime.now();
+        this.year = (byte)(now.getYear() - 2000);
+        this.month = (byte)now.getMonthValue();
+        this.day = (byte)now.getDayOfMonth();
+        this.hour = (byte)now.getHour();
+        this.min = (byte)now.getMinute();
+        this.sec = (byte)now.getSecond();
+
+        this.sequence = sequence;
+        this.regionId = regionId;
+        this.dataLength = 0;
+        this.count = 0;
+        this.chkSum = 0x00;
+        this.etx = SigProtocolConst.SIG_ETX;
+    }
+
+     public abstract ByteBuffer serialize();
+
+}

+ 34 - 0
src/main/java/com/sig/comm/server/tcp/protocol/vo/SigPhase.java

@@ -0,0 +1,34 @@
+package com.sig.comm.server.tcp.protocol.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class SigPhase {
+
+    private int intNo;
+    private int groupNo;
+    private short mode;
+    private short ringA;
+    private short ringB;
+    private short status;
+    private short count;
+    private short cycle;
+    private short phaseA1;
+    private short phaseA2;
+    private short phaseA3;
+    private short phaseA4;
+    private short phaseA5;
+    private short phaseA6;
+    private short phaseA7;
+    private short phaseA8;
+    private short phaseB1;
+    private short phaseB2;
+    private short phaseB3;
+    private short phaseB4;
+    private short phaseB5;
+    private short phaseB6;
+    private short phaseB7;
+    private short phaseB8;
+}