From 0aeeac33163640bbbe23560cbd108ddf290ac873 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Sep 2016 23:17:23 +1200 Subject: Message length parameter for H02 frame decoder --- src/org/traccar/protocol/H02FrameDecoder.java | 19 ++++++++----------- src/org/traccar/protocol/H02Protocol.java | 6 ++++-- test/org/traccar/protocol/H02FrameDecoderTest.java | 2 +- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/org/traccar/protocol/H02FrameDecoder.java b/src/org/traccar/protocol/H02FrameDecoder.java index 79e33c0ca..572ac979e 100644 --- a/src/org/traccar/protocol/H02FrameDecoder.java +++ b/src/org/traccar/protocol/H02FrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2013 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,11 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class H02FrameDecoder extends FrameDecoder { - private static final int MESSAGE_LENGTH = 32; + private int messageLength; + + public H02FrameDecoder(int messageLength) { + this.messageLength = messageLength; + } @Override protected Object decode( @@ -30,13 +34,6 @@ public class H02FrameDecoder extends FrameDecoder { char marker = (char) buf.getByte(buf.readerIndex()); - while (marker != '*' && marker != '$' && buf.readableBytes() > 0) { - buf.skipBytes(1); - if (buf.readableBytes() > 0) { - marker = (char) buf.getByte(buf.readerIndex()); - } - } - if (marker == '*') { // Return text message @@ -45,10 +42,10 @@ public class H02FrameDecoder extends FrameDecoder { return buf.readBytes(index + 1 - buf.readerIndex()); } - } else if (marker == '$' && buf.readableBytes() >= MESSAGE_LENGTH) { + } else if (marker == '$' && buf.readableBytes() >= messageLength) { // Return binary message - return buf.readBytes(MESSAGE_LENGTH); + return buf.readBytes(messageLength); } diff --git a/src/org/traccar/protocol/H02Protocol.java b/src/org/traccar/protocol/H02Protocol.java index 72b4f903d..cdc2d11bb 100644 --- a/src/org/traccar/protocol/H02Protocol.java +++ b/src/org/traccar/protocol/H02Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.BaseProtocol; +import org.traccar.Context; import org.traccar.TrackerServer; import org.traccar.model.Command; @@ -42,7 +43,8 @@ public class H02Protocol extends BaseProtocol { serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new H02FrameDecoder()); + int messageLength = Context.getConfig().getInteger(getName() + ".messageLength", 32); + pipeline.addLast("frameDecoder", new H02FrameDecoder(messageLength)); pipeline.addLast("stringEncoder", new StringEncoder()); pipeline.addLast("objectEncoder", new H02ProtocolEncoder()); pipeline.addLast("objectDecoder", new H02ProtocolDecoder(H02Protocol.this)); diff --git a/test/org/traccar/protocol/H02FrameDecoderTest.java b/test/org/traccar/protocol/H02FrameDecoderTest.java index e8b045048..a8417341a 100644 --- a/test/org/traccar/protocol/H02FrameDecoderTest.java +++ b/test/org/traccar/protocol/H02FrameDecoderTest.java @@ -9,7 +9,7 @@ public class H02FrameDecoderTest extends ProtocolTest { @Test public void testDecode() throws Exception { - H02FrameDecoder decoder = new H02FrameDecoder(); + H02FrameDecoder decoder = new H02FrameDecoder(32); Assert.assertEquals( binary("2a48512c3335333538383036303031353536382c56312c3139333530352c412c3830392e303031302c532c333435342e383939372c572c302e30302c302e30302c3239313031332c65666666666266662c3030303264342c3030303030622c3030353338352c3030353261612c323523"), -- cgit v1.2.3