From 2ad4b64a0db0b93334bf37e0c9a4152034094637 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Jan 2015 15:07:20 +1300 Subject: Implement protocol detector --- src/org/traccar/DetectorHandler.java | 77 +++++++++++++++++++++++++------ src/org/traccar/ServerManager.java | 21 ++++----- test/org/traccar/DetectorHandlerTest.java | 39 ++++++++++++++++ 3 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 test/org/traccar/DetectorHandlerTest.java 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"); } } } diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index fe5745bc8..6afd214a3 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -15,19 +15,9 @@ */ package org.traccar; -import java.io.FileInputStream; -import java.io.IOException; -import java.nio.ByteOrder; -import java.sql.SQLException; -import java.util.LinkedList; -import java.util.List; -import java.util.Properties; - import org.jboss.netty.bootstrap.ConnectionlessBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; import org.jboss.netty.handler.codec.frame.FixedLengthFrameDecoder; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; @@ -36,15 +26,22 @@ import org.jboss.netty.handler.codec.http.HttpRequestDecoder; import org.jboss.netty.handler.codec.http.HttpResponseEncoder; import org.jboss.netty.handler.codec.string.StringDecoder; import org.jboss.netty.handler.codec.string.StringEncoder; - +import org.traccar.database.DataManager; import org.traccar.geocode.GoogleReverseGeocoder; import org.traccar.geocode.NominatimReverseGeocoder; import org.traccar.geocode.ReverseGeocoder; import org.traccar.helper.Log; import org.traccar.http.WebServer; -import org.traccar.database.DataManager; import org.traccar.protocol.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteOrder; +import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; + /** * Server Manager */ diff --git a/test/org/traccar/DetectorHandlerTest.java b/test/org/traccar/DetectorHandlerTest.java new file mode 100644 index 000000000..f506856af --- /dev/null +++ b/test/org/traccar/DetectorHandlerTest.java @@ -0,0 +1,39 @@ +package org.traccar; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.channel.Channels; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.jboss.netty.handler.timeout.IdleStateHandler; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.traccar.helper.TestDataManager; +import org.traccar.model.Position; +import org.traccar.protocol.Gps103ProtocolDecoder; + +import java.nio.charset.Charset; +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class DetectorHandlerTest { + + @Test + public void testCheckPipeline() throws Exception { + + /*ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, "\r\n", "\n", ";")); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(new TestDataManager(), null, null)); + + DetectorHandler.checkPipeline("gps103", pipeline, ChannelBuffers.copiedBuffer( + "imei:869039001186913,tracker,1308282156,0,F,215630.000,A,5602.11015,N,9246.30767,E,1.4,,175.9,;", Charset.defaultCharset()));*/ + } + +} -- cgit v1.2.3