aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/DetectorHandler.java
blob: 223985906fac27258bcac24731306888444e252c (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.traccar.helper.Log;

import java.lang.reflect.Method;
import java.net.SocketAddress;
import java.util.List;

public class DetectorHandler extends SimpleChannelHandler {

    private final List<TrackerServer> serverList;

    private boolean showFailed;

    DetectorHandler(List<TrackerServer> serverList) {
        this.serverList = serverList;
    }

    public void checkPipeline(String protocol, ChannelPipeline pipeline, ChannelBuffer buf) throws Exception {
        Object tmp = buf.duplicate();

        // Frame decoder
        FrameDecoder frameDecoder = (FrameDecoder) pipeline.get("frameDecoder");
        if (frameDecoder != null) {
            try {
                Method method = frameDecoder.getClass().getDeclaredMethod(
                        "decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
                method.setAccessible(true);
                tmp = method.invoke(frameDecoder, null, null, tmp);
            } catch (NoSuchMethodException error) {
                Method method = frameDecoder.getClass().getSuperclass().getDeclaredMethod(
                        "decode", ChannelHandlerContext.class, Channel.class, ChannelBuffer.class);
                method.setAccessible(true);
                tmp = method.invoke(frameDecoder, null, null, tmp);
            }
        }

        // String decoder
        if (pipeline.get("stringDecoder") != null) {
            StringDecoder stringDecoder = new StringDecoder();
            if (tmp != null) {
                try {
                    Method method = stringDecoder.getClass().getDeclaredMethod(
                            "decode", ChannelHandlerContext.class, Channel.class, Object.class);
                    method.setAccessible(true);
                    tmp = method.invoke(stringDecoder, null, null, tmp);
                } catch (NoSuchMethodException error) {
                    Method method = stringDecoder.getClass().getSuperclass().getDeclaredMethod(
                            "decode", ChannelHandlerContext.class, Channel.class, Object.class);
                    method.setAccessible(true);
                    tmp = method.invoke(stringDecoder, null, null, tmp);
                }
            }
        }

        // Protocol decoder
        BaseProtocolDecoder protocolDecoder = (BaseProtocolDecoder) pipeline.get("objectDecoder");
        if (tmp != null) {
            try {
                Method method = protocolDecoder.getClass().getDeclaredMethod(
                        "decode", ChannelHandlerContext.class, Channel.class, SocketAddress.class, Object.class);
                method.setAccessible(true);
                tmp = method.invoke(protocolDecoder, null, null, null, tmp);
            } catch (NoSuchMethodException error) {
                Method method = protocolDecoder.getClass().getSuperclass().getDeclaredMethod(
                        "decode", ChannelHandlerContext.class, Channel.class, SocketAddress.class, Object.class);
                method.setAccessible(true);
                tmp = method.invoke(protocolDecoder, null, null, null, tmp);
            }
        }

        if (tmp != null) {
            Log.info("Protocol " + protocol + " possible match");
        } else if (showFailed) {
            Log.info("Protocol " + protocol + " no match");
        }
    }

    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {

        if (e.getMessage() instanceof ChannelBuffer) {
            ChannelBuffer buf = (ChannelBuffer) e.getMessage();

            for (TrackerServer server : serverList) {
                try {
                    if (!server.getProtocol().equals("detector")) {
                        checkPipeline(server.getProtocol(), server.getPipelineFactory().getPipeline(), buf);
                    }
                } catch (Exception error) {
                    if (showFailed) {
                        Log.info("Protocol " + server.getProtocol() + " error");
                    }
                }
            }
        }
    }

}