From a3c540a952b5136b1de2900c8947847f70b39446 Mon Sep 17 00:00:00 2001 From: Evgeny S Maksimov Date: Fri, 1 Feb 2019 00:56:35 +0300 Subject: Frame decoder refactoring --- src/org/traccar/protocol/NavisFrameDecoder.java | 30 ++++++++++++++-------- src/org/traccar/protocol/NavisProtocol.java | 5 ++-- .../traccar/protocol/NavisFrameDecoderTest.java | 12 +++------ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/org/traccar/protocol/NavisFrameDecoder.java b/src/org/traccar/protocol/NavisFrameDecoder.java index 9a3b3d45c..0146243cd 100644 --- a/src/org/traccar/protocol/NavisFrameDecoder.java +++ b/src/org/traccar/protocol/NavisFrameDecoder.java @@ -21,20 +21,17 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.TooLongFrameException; import java.nio.charset.StandardCharsets; import org.traccar.BaseFrameDecoder; +import org.traccar.BasePipelineFactory; public class NavisFrameDecoder extends BaseFrameDecoder { + private int flexDataSize; // bytes + private static final int NTCB_HEADER_LENGTH = 16; private static final int NTCB_LENGTH_OFFSET = 12; private static final int FLEX_HEADER_LENGTH = 2; private static final int MAX_FRAME_LENGTH = 65551; - private final NavisProtocolDecoder protocolDecoder; - - public NavisFrameDecoder(NavisProtocolDecoder protocolDecoder) { - this.protocolDecoder = protocolDecoder; - } - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { @@ -53,7 +50,15 @@ public class NavisFrameDecoder extends BaseFrameDecoder { return buf.readRetainedSlice(1); } - if (protocolDecoder.getFlexDataSize() > 0) { + if (ctx != null) { + NavisProtocolDecoder protocolDecoder = + BasePipelineFactory.getHandler(ctx.pipeline(), NavisProtocolDecoder.class); + if (protocolDecoder != null) { + flexDataSize = protocolDecoder.getFlexDataSize(); + } + } + + if (flexDataSize > 0) { // FLEX frame if (buf.readableBytes() > FLEX_HEADER_LENGTH) { int length = 0; @@ -61,14 +66,13 @@ public class NavisFrameDecoder extends BaseFrameDecoder { switch (type) { // FLEX 1.0 case "~A": - length = protocolDecoder.getFlexDataSize() - * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1; + length = flexDataSize * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1; break; case "~T": - length = protocolDecoder.getFlexDataSize() + 4 + 1; + length = flexDataSize + 4 + 1; break; case "~C": - length = protocolDecoder.getFlexDataSize() + 1; + length = flexDataSize + 1; break; // FLEX 2.0 (Extra packages) case "~E": @@ -108,4 +112,8 @@ public class NavisFrameDecoder extends BaseFrameDecoder { return null; } + public void setFlexDataSize(int flexDataSize) { + this.flexDataSize = flexDataSize; + } + } diff --git a/src/org/traccar/protocol/NavisProtocol.java b/src/org/traccar/protocol/NavisProtocol.java index 628b78fd4..db0eba8c1 100644 --- a/src/org/traccar/protocol/NavisProtocol.java +++ b/src/org/traccar/protocol/NavisProtocol.java @@ -25,9 +25,8 @@ public class NavisProtocol extends BaseProtocol { addServer(new TrackerServer(false, getName()) { @Override protected void addProtocolHandlers(PipelineBuilder pipeline) { - NavisProtocolDecoder protocolDecoder = new NavisProtocolDecoder(NavisProtocol.this); - pipeline.addLast(new NavisFrameDecoder(protocolDecoder)); - pipeline.addLast(protocolDecoder); + pipeline.addLast(new NavisFrameDecoder()); + pipeline.addLast(new NavisProtocolDecoder(NavisProtocol.this)); } }); } diff --git a/test/org/traccar/protocol/NavisFrameDecoderTest.java b/test/org/traccar/protocol/NavisFrameDecoderTest.java index 5e8d8fd36..0ebfeacd2 100644 --- a/test/org/traccar/protocol/NavisFrameDecoderTest.java +++ b/test/org/traccar/protocol/NavisFrameDecoderTest.java @@ -9,8 +9,7 @@ public class NavisFrameDecoderTest extends ProtocolTest { @Test public void testDecodeNtcb() throws Exception { - NavisProtocolDecoder protocolDecoder = new NavisProtocolDecoder(null); - NavisFrameDecoder frameDecoder = new NavisFrameDecoder(protocolDecoder); + NavisFrameDecoder frameDecoder = new NavisFrameDecoder(); verifyFrame(binary( "404e5443010000000000000059009adb2a3e54250000000000ff1500040b0a1008291838001200760ee600000000000000000000000f1500040b0a10ac20703fb1aec23f00000000320149668f430000000000000000000000000000000000000000000000f3808080"), @@ -21,14 +20,9 @@ public class NavisFrameDecoderTest extends ProtocolTest { @Test public void testDecodeFlex10() throws Exception { - NavisProtocolDecoder protocolDecoder = new NavisProtocolDecoder(null); - NavisFrameDecoder frameDecoder = new NavisFrameDecoder(protocolDecoder); + NavisFrameDecoder frameDecoder = new NavisFrameDecoder(); - verifyNull(protocolDecoder, binary( - "404e544301000000c9b5f602130046c52a3e533a383639363936303439373232383235")); - - verifyNull(protocolDecoder, binary( - "404e544301000000aaecf6021300c8712a3e464c4558b00a0a45ffff300a08080f8388")); + frameDecoder.setFlexDataSize(73); verifyFrame(binary( "7e54040000000400000030129957405c000b00632f9857405ccace03021e129101a103000000000000c4005ba3fe3b00000000120046100000000000001aff7f000080bfffff80000080bfffffffff9f"), -- cgit v1.2.3