aboutsummaryrefslogtreecommitdiff
path: root/src/net/sourceforge/opentracking/protocol/xexun/XexunFrameDecoder.java
blob: be0a92dc682daa5c82c470de28043c3374c2e1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Минимум - 100
 * Начало - (10 цифр) + ','
 * Длина 16 x ','
 */

package net.sourceforge.opentracking.protocol.xexun;

import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
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 identifier
        int index = 0;
        int countDigit = 0;
        for (; index < length; index++) {

            // Check byte
            char c = (char) buf.getByte(index);
            if (Character.isDigit(c)) {
                countDigit++;
            }

            // Check count
            if (countDigit == 10) {
                break;
            }
        }

        if (countDigit < 10) {
            return null;
        }

        // Find begin
        int beginIndex = 0;
        for (; index < length; index++) {

            char c = (char) buf.getByte(index);
            if (c == ',') {
                beginIndex = index;
                break;
            }
        }

        if (beginIndex == 0) {
            return null;
        }

        // Find imei
        int imeiIndex = 0;
        for (; index < length; index++) {

            char c = (char) buf.getByte(index);
            if (c == ':') {
                imeiIndex = index;
                break;
            }
        }

        if (imeiIndex == 0) {
            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) {
            return null;
        }

        // Read buffer
        buf.skipBytes(beginIndex);
        ChannelBuffer frame = buf.readBytes(endIndex - beginIndex + 1);

        return frame;
    }

}