From d938e1e4d8d54fec6aab26246b6b804d34addf66 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 26 Oct 2015 12:47:41 +1300 Subject: Modify Wondex frame decoder --- src/org/traccar/helper/ChannelBufferTools.java | 31 ---------------------- src/org/traccar/protocol/WondexFrameDecoder.java | 9 +++---- .../traccar/protocol/WondexFrameDecoderTest.java | 25 +++++++++++++++++ 3 files changed, 29 insertions(+), 36 deletions(-) create mode 100644 test/org/traccar/protocol/WondexFrameDecoderTest.java diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java index ae6dfc830..d8d74b6ea 100644 --- a/src/org/traccar/helper/ChannelBufferTools.java +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -24,37 +24,6 @@ public final class ChannelBufferTools { private ChannelBufferTools() { } - /** - * Find string in network buffer - */ - public static Integer find( - ChannelBuffer buf, - Integer start, - Integer finish, - String subString) { - - int index = start; - boolean match; - - for (; index < finish; index++) { - match = true; - - for (int i = 0; i < subString.length(); i++) { - char c = (char) buf.getByte(index + i); - if (c != subString.charAt(i)) { - match = false; - break; - } - } - - if (match) { - return index; - } - } - - return null; - } - /** * Convert hex to integer (length in hex digits) */ diff --git a/src/org/traccar/protocol/WondexFrameDecoder.java b/src/org/traccar/protocol/WondexFrameDecoder.java index 1a66c9373..eee112e9d 100644 --- a/src/org/traccar/protocol/WondexFrameDecoder.java +++ b/src/org/traccar/protocol/WondexFrameDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; import org.traccar.helper.ChannelBufferTools; +import org.traccar.helper.StringFinder; public class WondexFrameDecoder extends FrameDecoder { @@ -27,9 +28,7 @@ public class WondexFrameDecoder extends FrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { if (buf.readableBytes() < KEEP_ALIVE_LENGTH) { return null; @@ -45,8 +44,8 @@ public class WondexFrameDecoder extends FrameDecoder { } else { - Integer index = ChannelBufferTools.find(buf, buf.readerIndex(), buf.writerIndex(), "\r\n"); - if (index != null) { + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n")); + if (index != -1) { ChannelBuffer frame = buf.readBytes(index - buf.readerIndex()); buf.skipBytes(2); return frame; diff --git a/test/org/traccar/protocol/WondexFrameDecoderTest.java b/test/org/traccar/protocol/WondexFrameDecoderTest.java new file mode 100644 index 000000000..54df4c41b --- /dev/null +++ b/test/org/traccar/protocol/WondexFrameDecoderTest.java @@ -0,0 +1,25 @@ +package org.traccar.protocol; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolDecoderTest; + +import java.nio.ByteOrder; + +public class WondexFrameDecoderTest extends ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + Pt502FrameDecoder decoder = new Pt502FrameDecoder(); + + Assert.assertNull( + decoder.decode(null, null, binary("d0d70b0001ca9a3b"))); + + Assert.assertEquals( + binary("313034343938393630312c32303133303332333039353531352c31332e3537323737362c35322e3430303833382c302c3030302c37322c302c32"), + decoder.decode(null, null, binary("313034343938393630312c32303133303332333039353531352c31332e3537323737362c35322e3430303833382c302c3030302c37322c302c320d0a"))); + + } + +} -- cgit v1.2.3