Search in sources :

Example 6 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class MegastekFrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    if (buf.readableBytes() < 10) {
        return null;
    }
    if (Character.isDigit(buf.getByte(buf.readerIndex()))) {
        int length = 4 + Integer.parseInt(buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII));
        if (buf.readableBytes() >= length) {
            return buf.readBytes(length);
        }
    } else {
        while (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n') {
            buf.skipBytes(1);
        }
        int delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n"));
        if (delimiter == -1) {
            delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '!');
        }
        if (delimiter != -1) {
            ChannelBuffer result = buf.readBytes(delimiter - buf.readerIndex());
            buf.skipBytes(1);
            return result;
        }
    }
    return null;
}
Also used : StringFinder(org.traccar.helper.StringFinder) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 7 with StringFinder

use of org.traccar.helper.StringFinder in project traccar by tananaev.

the class TotemFrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
    if (buf.readableBytes() < 10) {
        return null;
    }
    int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("$$"));
    if (beginIndex == -1) {
        return null;
    } else if (beginIndex > buf.readerIndex()) {
        buf.readerIndex(beginIndex);
    }
    int length;
    if (buf.getByte(buf.readerIndex() + 2) == (byte) '0') {
        length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 4, StandardCharsets.US_ASCII));
    } else {
        length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, StandardCharsets.US_ASCII), 16);
    }
    if (length <= buf.readableBytes()) {
        return buf.readBytes(length);
    }
    return null;
}
Also used : StringFinder(org.traccar.helper.StringFinder)

Aggregations

StringFinder (org.traccar.helper.StringFinder)7 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)5 DeviceSession (org.traccar.DeviceSession)2 Position (org.traccar.model.Position)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 ChannelBufferIndexFinder (org.jboss.netty.buffer.ChannelBufferIndexFinder)1 DateBuilder (org.traccar.helper.DateBuilder)1 Parser (org.traccar.helper.Parser)1