aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-10-26 11:48:53 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-10-26 11:48:53 +1300
commit64aa76a78916310cfbc14ec3ae6083555b95a4a0 (patch)
tree3e8a3f7bdab0250f284aae5768c1f50ed3e7e811
parent511b1807da62c225afc70d45f6eb047382d242a9 (diff)
downloadtrackermap-server-64aa76a78916310cfbc14ec3ae6083555b95a4a0.tar.gz
trackermap-server-64aa76a78916310cfbc14ec3ae6083555b95a4a0.tar.bz2
trackermap-server-64aa76a78916310cfbc14ec3ae6083555b95a4a0.zip
Modify Xexun frame decoder
-rw-r--r--src/org/traccar/helper/ChannelBufferTools.java4
-rw-r--r--src/org/traccar/protocol/XexunFrameDecoder.java32
-rw-r--r--test/org/traccar/protocol/XexunFrameDecoderTest.java24
3 files changed, 36 insertions, 24 deletions
diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java
index 5117f1779..ae6dfc830 100644
--- a/src/org/traccar/helper/ChannelBufferTools.java
+++ b/src/org/traccar/helper/ChannelBufferTools.java
@@ -24,10 +24,6 @@ public final class ChannelBufferTools {
private ChannelBufferTools() {
}
- public static Integer find(ChannelBuffer buf, int start, String subString) {
- return find(buf, start, buf.readerIndex() + buf.readableBytes(), subString);
- }
-
/**
* Find string in network buffer
*/
diff --git a/src/org/traccar/protocol/XexunFrameDecoder.java b/src/org/traccar/protocol/XexunFrameDecoder.java
index 60fa02696..b892fa346 100644
--- a/src/org/traccar/protocol/XexunFrameDecoder.java
+++ b/src/org/traccar/protocol/XexunFrameDecoder.java
@@ -20,47 +20,39 @@ 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 XexunFrameDecoder extends FrameDecoder {
@Override
protected Object decode(
- ChannelHandlerContext ctx,
- Channel channel,
- ChannelBuffer buf) throws Exception {
+ ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
- // Check minimum length
- int length = buf.readableBytes();
- if (length < 80) {
+ if (buf.readableBytes() < 80) {
return null;
}
- // Find start
- Integer beginIndex = ChannelBufferTools.find(buf, buf.readerIndex(), "GPRMC");
- if (beginIndex == null) {
- beginIndex = ChannelBufferTools.find(buf, buf.readerIndex(), "GNRMC");
- if (beginIndex == null) {
+ int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GPRMC"));
+ if (beginIndex == -1) {
+ beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("GNRMC"));
+ if (beginIndex == -1) {
return null;
}
}
- // Find identifier
- Integer idIndex = ChannelBufferTools.find(buf, beginIndex, "imei:");
- if (idIndex == null) {
+ int identifierIndex = buf.indexOf(beginIndex, buf.writerIndex(), new StringFinder("imei:"));
+ if (identifierIndex == -1) {
return null;
}
- // Find end
- Integer endIndex = ChannelBufferTools.find(buf, idIndex, ",");
- if (endIndex == null) {
+ int endIndex = buf.indexOf(identifierIndex, buf.writerIndex(), (byte) ',');
+ if (endIndex == -1) {
return null;
}
- // Read buffer
buf.skipBytes(beginIndex);
- ChannelBuffer frame = buf.readBytes(endIndex - beginIndex + 1);
- return frame;
+ return buf.readBytes(endIndex - beginIndex + 1);
}
}
diff --git a/test/org/traccar/protocol/XexunFrameDecoderTest.java b/test/org/traccar/protocol/XexunFrameDecoderTest.java
new file mode 100644
index 000000000..e9a79ab45
--- /dev/null
+++ b/test/org/traccar/protocol/XexunFrameDecoderTest.java
@@ -0,0 +1,24 @@
+package org.traccar.protocol;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.traccar.ProtocolDecoderTest;
+
+public class XexunFrameDecoderTest extends ProtocolDecoderTest {
+
+ @Test
+ public void testDecode() throws Exception {
+
+ XexunFrameDecoder decoder = new XexunFrameDecoder();
+
+ Assert.assertEquals(
+ binary("4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c"),
+ decoder.decode(null, null, binary("313531303132313435392c2b33313635323435343932372c4750524d432c3230353933352e3030302c412c353134302e343335302c4e2c3530312e303638362c452c302e30302c302e30302c3132313031352c30302c303030302e302c412a37302c462c2c696d65693a3335393538373031343731383339322c313232497")));
+
+ Assert.assertEquals(
+ binary("4750524d432c3130333733312e3633362c412c343534352e353236362c4e2c30303434382e383235392c452c32312e31322c3237362e30312c3135303631352c2c2c412a35372c4c2c2c20696d65693a3031333934393030323032363637352c"),
+ decoder.decode(null, null, binary("3135303631353132333733312c2b33333634373338343631312c4750524d432c3130333733312e3633362c412c343534352e353236362c4e2c30303434382e383235392c452c32312e31322c3237362e30312c3135303631352c2c2c412a35372c4c2c2c20696d65693a3031333934393030323032363637352c30342c333532322e392c463a332e3732562c302c3134322c32313734342c3230382c30312c303730322c394338430a0d")));
+
+ }
+
+}