diff options
Diffstat (limited to 'src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java')
-rw-r--r-- | src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java | 98 |
1 files changed, 41 insertions, 57 deletions
diff --git a/src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java b/src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java index be0a92dc6..d864b10ba 100644 --- a/src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java @@ -16,82 +16,66 @@ import org.jboss.netty.buffer.ChannelBuffer; */ public class XexunFrameDecoder extends FrameDecoder { - protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { - - //System.out.println("read: " + buf.readableBytes()); - - // Check minimum length - int length = buf.readableBytes(); - if (length < 100) { - return null; - } + /** + * Find string in network buffer + */ + private static Integer find( + ChannelBuffer buf, + Integer start, + Integer length, + String subString) { + + int index = start; + boolean match; - // Find identifier - int index = 0; - int countDigit = 0; for (; index < length; index++) { - - // Check byte - char c = (char) buf.getByte(index); - if (Character.isDigit(c)) { - countDigit++; + 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; + } } - // Check count - if (countDigit == 10) { - break; + if (match) { + return index; } } - if (countDigit < 10) { - return null; - } + return null; + } - // Find begin - int beginIndex = 0; - for (; index < length; index++) { + protected Object decode( + ChannelHandlerContext ctx, + Channel channel, + ChannelBuffer buf) throws Exception { - char c = (char) buf.getByte(index); - if (c == ',') { - beginIndex = index; - break; - } - } + System.out.println("read: " + buf.readableBytes()); + System.out.println("buffer: " + buf.toString("UTF-8")); - if (beginIndex == 0) { + // Check minimum length + int length = buf.readableBytes(); + if (length < 100) { return null; } - // Find imei - int imeiIndex = 0; - for (; index < length; index++) { - - char c = (char) buf.getByte(index); - if (c == ':') { - imeiIndex = index; - break; - } + // Find start + Integer beginIndex = find(buf, 0, length, "GPRMC"); + if (beginIndex == null) { + return null; } - if (imeiIndex == 0) { + // Find identifier + Integer idIndex = find(buf, beginIndex, length, "imei:"); + if (idIndex == null) { return null; } // Find end - int endIndex = 0; - for (; index < length; index++) { - - char c = (char) buf.getByte(index); - if (c == ',') { - endIndex = index; - break; - } - } - - if (endIndex == 0) { + Integer endIndex = find(buf, idIndex, length, ","); + if (endIndex == null) { return null; } |