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 ++--- 2 files changed, 21 insertions(+), 14 deletions(-) (limited to 'src/org/traccar') 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)); } }); } -- cgit v1.2.3