From 64aa76a78916310cfbc14ec3ae6083555b95a4a0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 26 Oct 2015 11:48:53 +1300 Subject: Modify Xexun frame decoder --- src/org/traccar/helper/ChannelBufferTools.java | 4 ---- src/org/traccar/protocol/XexunFrameDecoder.java | 32 ++++++++++--------------- 2 files changed, 12 insertions(+), 24 deletions(-) (limited to 'src/org') 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); } } -- cgit v1.2.3