aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/DetectorHandler.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-01-16 15:07:20 +1300
committerAnton Tananaev <anton.tananaev@gmail.com>2015-01-16 15:07:20 +1300
commit2ad4b64a0db0b93334bf37e0c9a4152034094637 (patch)
tree5bcfbed4c2a42f3588aab2aa988d3f89bf82340d /src/org/traccar/DetectorHandler.java
parente1608e2ffcb65afdadf5ef76a08cff1bfab47b49 (diff)
downloadtrackermap-server-2ad4b64a0db0b93334bf37e0c9a4152034094637.tar.gz
trackermap-server-2ad4b64a0db0b93334bf37e0c9a4152034094637.tar.bz2
trackermap-server-2ad4b64a0db0b93334bf37e0c9a4152034094637.zip
Implement protocol detector
Diffstat (limited to 'src/org/traccar/DetectorHandler.java')
-rw-r--r--src/org/traccar/DetectorHandler.java77
1 files changed, 64 insertions, 13 deletions
diff --git a/src/org/traccar/DetectorHandler.java b/src/org/traccar/DetectorHandler.java
index 1fc5062e3..0f94235c0 100644
--- a/src/org/traccar/DetectorHandler.java
+++ b/src/org/traccar/DetectorHandler.java
@@ -18,6 +18,8 @@ package org.traccar;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.*;
import org.jboss.netty.handler.codec.frame.FrameDecoder;
+import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
+import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.traccar.database.DataManager;
@@ -25,6 +27,8 @@ import org.traccar.helper.ChannelBufferTools;
import org.traccar.helper.Log;
import org.traccar.model.Position;
+import java.lang.reflect.Method;
+import java.net.SocketAddress;
import java.util.List;
public class DetectorHandler extends SimpleChannelHandler {
@@ -35,29 +39,76 @@ public class DetectorHandler extends SimpleChannelHandler {
this.serverList = serverList;
}
- @Override
- public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
+ public static void checkPipeline(String protocol, ChannelPipeline pipeline, ChannelBuffer buf) throws Exception {
+ Object tmp = buf.duplicate();
- if (e.getMessage() instanceof ChannelBuffer) {
- ChannelBuffer buf = (ChannelBuffer) e.getMessage();
+ // 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);
+ }
+ }
- for (TrackerServer server : serverList) {
+ // String decoder
+ if (pipeline.get("stringDecoder") != null) {
+ StringDecoder stringDecoder = new StringDecoder();
+ if (tmp != null) {
try {
- ChannelPipeline pipeline = server.getPipelineFactory().getPipeline();
+ 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);
+ }
+ }
+ }
- if (pipeline.get("stringDecoder") != null) {
+ // 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);
+ }
+ }
- /*ChannelBuffer tmp = buf.duplicate();
- FrameDecoder frameDecoder = (FrameDecoder) pipeline.get("frameDecoder");
- if (frameDecoder != null) {
- tmp = frameDecoder.
- }*/
+ if (tmp != null) {
+ Log.info("Protocol " + protocol + " possible match");
+ System.out.println("Protocol " + protocol + " possible match");
+ } else {
+ Log.info("Protocol " + protocol + " no match");
+ System.out.println("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) {
- Log.warning(error);
+ Log.info("Protocol " + server.getProtocol() + " error");
+ System.out.println("Protocol " + server.getProtocol() + " error");
}
}
}