diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-04-26 20:16:47 +0000 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-04-26 20:16:47 +0000 |
commit | e90c0611b8236bcd0c9d261ae404114b047462de (patch) | |
tree | f41e5c7305acec2831354f11541f50be262c1c31 | |
parent | 02c1fb78a8b1681325ec01f49ae6ff6603825243 (diff) | |
download | trackermap-server-e90c0611b8236bcd0c9d261ae404114b047462de.tar.gz trackermap-server-e90c0611b8236bcd0c9d261ae404114b047462de.tar.bz2 trackermap-server-e90c0611b8236bcd0c9d261ae404114b047462de.zip |
18 files changed, 287 insertions, 449 deletions
diff --git a/src/org/traccar/GenericPipelineFactory.java b/src/org/traccar/GenericPipelineFactory.java new file mode 100644 index 000000000..13f4c36ac --- /dev/null +++ b/src/org/traccar/GenericPipelineFactory.java @@ -0,0 +1,74 @@ +/* + * Copyright 2012 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.channel.*; +import org.jboss.netty.handler.logging.LoggingHandler; +import org.traccar.model.DataManager; + +/** + * Generic pipeline factory + */ +public abstract class GenericPipelineFactory implements ChannelPipelineFactory { + + private TrackerServer server; + private DataManager dataManager; + private Boolean loggerEnabled; + + /** + * Open channel handler + */ + protected class OpenChannelHandler extends SimpleChannelHandler { + + private TrackerServer server; + + public OpenChannelHandler(TrackerServer server) { + this.server = server; + } + + @Override + public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) { + server.getChannelGroup().add(e.getChannel()); + } + } + + public GenericPipelineFactory( + TrackerServer server, DataManager dataManager, Boolean loggerEnabled) { + this.server = server; + this.dataManager = dataManager; + this.loggerEnabled = loggerEnabled; + } + + protected DataManager getDataManager() { + return dataManager; + } + + protected abstract void addSpecificHandlers(ChannelPipeline pipeline); + + public ChannelPipeline getPipeline() { + ChannelPipeline pipeline = Channels.pipeline(); + pipeline.addLast("openHandler", new OpenChannelHandler(server)); + if (loggerEnabled) { + pipeline.addLast("logger", new LoggingHandler("logger")); + } + addSpecificHandlers(pipeline); + //pipeline.addLast("frameDecoder", new XexunFrameDecoder()); + //pipeline.addLast("stringDecoder", new StringDecoder()); + //pipeline.addLast("objectDecoder", new XexunProtocolDecoder(serverCreator, resetDelay)); + pipeline.addLast("handler", new TrackerEventHandler(dataManager)); + return pipeline; + } +}
\ No newline at end of file diff --git a/src/org/traccar/GenericProtocolDecoder.java b/src/org/traccar/GenericProtocolDecoder.java index c48cf6c56..c2e7992bd 100644 --- a/src/org/traccar/GenericProtocolDecoder.java +++ b/src/org/traccar/GenericProtocolDecoder.java @@ -17,12 +17,9 @@ package org.traccar; import java.util.Timer; import java.util.TimerTask; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.*; import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; -import org.jboss.netty.channel.ChannelEvent; -import org.jboss.netty.channel.ChannelState; -import org.jboss.netty.channel.ChannelStateEvent; +import org.traccar.model.DataManager; /** * Base class for protocol decoders diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java index b5e7acdea..0242a391b 100644 --- a/src/org/traccar/Server.java +++ b/src/org/traccar/Server.java @@ -15,46 +15,26 @@ */ package org.traccar; -import java.util.Map; -import java.util.HashMap; -import java.util.List; -import java.util.LinkedList; -import java.util.Properties; import java.io.FileInputStream; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.logging.Logger; -import java.util.logging.FileHandler; +import java.util.*; import java.util.logging.Formatter; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import org.traccar.helper.NamedParameterStatement; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; -import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; +import java.util.logging.*; import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.handler.logging.LoggingHandler; -import org.traccar.protocol.XexunFrameDecoder; -import org.traccar.protocol.XexunProtocolDecoder; -import org.traccar.protocol.Gps103ProtocolDecoder; -import org.traccar.protocol.Tk103ProtocolDecoder; -import org.traccar.protocol.Gl200ProtocolDecoder; -import org.traccar.protocol.T55ProtocolDecoder; -import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.channel.ChannelPipelineFactory; -import org.jboss.netty.channel.ChannelStateEvent; -import org.jboss.netty.channel.Channels; -import org.jboss.netty.channel.SimpleChannelHandler; +import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.helper.AdvancedConnection; -import org.traccar.protocol.Gl100ProtocolDecoder; -import org.traccar.protocol.Xexun2ProtocolDecoder; -import org.traccar.protocol.Avl08ProtocolDecoder; +import org.traccar.helper.NamedParameterStatement; +import org.traccar.model.DataManager; +import org.traccar.model.Device; +import org.traccar.model.Position; +import org.traccar.protocol.*; /** * Server @@ -66,15 +46,15 @@ public class Server implements DataManager { */ private List serverList; - private boolean loggerEnable; + private boolean loggerEnabled; public Server() { serverList = new LinkedList(); - loggerEnable = false; + loggerEnabled = false; } - + public boolean isLoggerEnabled() { - return loggerEnable; + return loggerEnabled; } /** @@ -103,6 +83,31 @@ public class Server implements DataManager { } /** + * Start + */ + public void start() { + for (Object server: serverList) { + ((TrackerServer) server).start(); + } + } + + /** + * Stop + */ + public void stop() { + for (Object server: serverList) { + ((TrackerServer) server).stop(); + } + } + + /** + * Destroy + */ + public void destroy() { + serverList.clear(); + } + + /** * Database statements */ private NamedParameterStatement selectDevice; @@ -155,9 +160,9 @@ public class Server implements DataManager { return deviceList; } - + public Device getDeviceByImei(String imei) throws SQLException { - + if ((devices == null) || (Calendar.getInstance().getTimeInMillis() - devicesLastUpdate.getTimeInMillis() > devicesListRefreshDelay)) { devices = new HashMap(); List deviceList = getDevices(); @@ -171,7 +176,7 @@ public class Server implements DataManager { } public synchronized void setPosition(Position position) throws SQLException { - + insertPosition.prepare(); insertPosition.setLong("device_id", position.getDeviceId()); @@ -187,15 +192,15 @@ public class Server implements DataManager { insertPosition.executeUpdate(); } - + /** * Init logger */ public void initLogger(Properties properties) throws IOException { - loggerEnable = Boolean.valueOf(properties.getProperty("logger.enable")); + loggerEnabled = Boolean.valueOf(properties.getProperty("logger.enable")); - if (loggerEnable) { + if (loggerEnabled) { Logger logger = Logger.getLogger("logger"); String fileName = properties.getProperty("logger.file"); @@ -206,7 +211,7 @@ public class Server implements DataManager { file.setFormatter(new Formatter() { private final String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); - + private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @@ -225,458 +230,231 @@ public class Server implements DataManager { } } - /** - * Open channel handler - */ - protected class OpenChannelHandler extends SimpleChannelHandler { - - private TrackerServer server; - - public OpenChannelHandler(TrackerServer server) { - this.server = server; + private boolean isProtocolEnabled(Properties properties, String protocol) { + String enabled = properties.getProperty(protocol + ".enable"); + if (enabled != null) { + return Boolean.valueOf(enabled); } + return false; + } - @Override - public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) { - server.getChannelGroup().add(e.getChannel()); + private Integer getProtocolPort(Properties properties, String protocol) { + String port = properties.getProperty(protocol + ".port"); + if (port != null) { + return Integer.valueOf(port); } + return 5000; // Magic number } - /** - * Xexun pipeline factory - */ - protected class XexunPipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public XexunPipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - pipeline.addLast("frameDecoder", new XexunFrameDecoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new XexunProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; + private Integer getProtocolResetDelay(Properties properties, String protocol) { + String resetDelay = properties.getProperty(protocol + ".resetDelay"); + if (resetDelay != null) { + return Integer.valueOf(resetDelay); } + return 0; } - + /** * Init Xexun server */ public void initXexunServer(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("xexun.enable")); - if (enable) { + String protocol = "xexun"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("xexun.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("xexun.resetDelay"); - server.setPipelineFactory(new XexunPipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new XexunFrameDecoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new XexunProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } /** - * Gps103 pipeline factory - */ - protected class Gps103PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Gps103PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) ';' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - - /** * Init Gps103 server */ public void initGps103Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("gps103.enable")); - if (enable) { + String protocol = "gps103"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("gps103.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("gps103.resetDelay"); - server.setPipelineFactory(new Gps103PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) ';' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new Gps103ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } /** - * Tk103 pipeline factory - */ - protected class Tk103PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Tk103PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) ')' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("objectDecoder", new Tk103ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - - /** * Init Tk103 server */ public void initTk103Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("tk103.enable")); - if (enable) { + String protocol = "tk103"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("tk103.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("tk103.resetDelay"); - server.setPipelineFactory(new Tk103PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) ')' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new Tk103ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } /** - * Gl100 pipeline factory - */ - protected class Gl100PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Gl100PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) 0x0 }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("objectDecoder", new Gl100ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - - /** * Init Gl100 server */ public void initGl100Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("gl100.enable")); - if (enable) { + String protocol = "gl100"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("gl100.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("gl100.resetDelay"); - server.setPipelineFactory(new Gl100PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) 0x0 }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new Gl100ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } - - /** - * Gl200 pipeline factory - */ - protected class Gl200PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Gl200PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) '$' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("objectDecoder", new Gl200ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - /** * Init Gl200 server */ public void initGl200Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("gl200.enable")); - if (enable) { + String protocol = "gl200"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("gl200.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("gl200.resetDelay"); - server.setPipelineFactory(new Gl200PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) '$' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new Gl200ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } - - /** - * T55 pipeline factory - */ - protected class T55PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public T55PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) '\r', (byte) '\n' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("objectDecoder", new T55ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - + /** * Init T55 server */ public void initT55Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("t55.enable")); - if (enable) { + String protocol = "t55"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("t55.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("t55.resetDelay"); - server.setPipelineFactory(new T55PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) '\r', (byte) '\n' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("objectDecoder", new T55ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } - - /** - * Xexun 2 pipeline factory - */ - protected class Xexun2PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Xexun2PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) '\n' }; // tracker bug \n\r - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new Xexun2ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - + /** * Init Xexun 2 server */ public void initXexun2Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("xexun2.enable")); - if (enable) { + String protocol = "xexun2"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("xexun2.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("xexun2.resetDelay"); - server.setPipelineFactory(new Xexun2PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) '\n' }; // tracker bug \n\r + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new Xexun2ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } /** - * AVL-08 pipeline factory - */ - protected class Avl08PipelineFactory implements ChannelPipelineFactory { - - private TrackerServer server; - private Server serverCreator; - private Integer resetDelay; - - public Avl08PipelineFactory( - TrackerServer server, Server serverCreator, Integer resetDelay) { - this.server = server; - this.serverCreator = serverCreator; - this.resetDelay = resetDelay; - } - - public ChannelPipeline getPipeline() { - ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("openHandler", new OpenChannelHandler(server)); - if (serverCreator.isLoggerEnabled()) { - pipeline.addLast("logger", new LoggingHandler("logger")); - } - byte delimiter[] = { (byte) '\r', (byte) '\n' }; - pipeline.addLast("frameDecoder", - new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new Avl08ProtocolDecoder(serverCreator, resetDelay)); - pipeline.addLast("handler", new TrackerEventHandler(serverCreator)); - return pipeline; - } - } - - /** * Init AVL-08 server */ public void initAvl08Server(Properties properties) throws SQLException { - boolean enable = Boolean.valueOf(properties.getProperty("avl08.enable")); - if (enable) { + String protocol = "avl08"; + if (isProtocolEnabled(properties, protocol)) { - TrackerServer server = new TrackerServer( - Integer.valueOf(properties.getProperty("avl08.port"))); + TrackerServer server = new TrackerServer(getProtocolPort(properties, protocol)); + final Integer resetDelay = getProtocolResetDelay(properties, protocol); - String resetDelay = properties.getProperty("avl08.resetDelay"); - server.setPipelineFactory(new Avl08PipelineFactory( - server, this, (resetDelay == null) ? 0 : Integer.valueOf(resetDelay))); + server.setPipelineFactory(new GenericPipelineFactory(server, this, isLoggerEnabled()) { + protected void addSpecificHandlers(ChannelPipeline pipeline) { + byte delimiter[] = { (byte) '\r', (byte) '\n' }; + pipeline.addLast("frameDecoder", + new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new Avl08ProtocolDecoder(getDataManager(), resetDelay)); + } + }); serverList.add(server); } } - - /** - * Start - */ - public void start() { - for (Object server: serverList) { - ((TrackerServer) server).start(); - } - } - - /** - * Stop - */ - public void stop() { - for (Object server: serverList) { - ((TrackerServer) server).stop(); - } - } - - /** - * Destroy - */ - public void destroy() { - serverList.clear(); - } } diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java index 4750c14c1..e6428ee36 100644 --- a/src/org/traccar/TrackerEventHandler.java +++ b/src/org/traccar/TrackerEventHandler.java @@ -15,11 +15,9 @@ */ package org.traccar; -import org.jboss.netty.channel.SimpleChannelHandler; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ExceptionEvent; -import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.channel.ChannelHandler; +import org.jboss.netty.channel.*; +import org.traccar.model.DataManager; +import org.traccar.model.Position; /** * Tracker message handler diff --git a/src/org/traccar/TrackerServer.java b/src/org/traccar/TrackerServer.java index 6f602d365..da889d0a3 100644 --- a/src/org/traccar/TrackerServer.java +++ b/src/org/traccar/TrackerServer.java @@ -19,10 +19,10 @@ import java.net.InetSocketAddress; import java.util.concurrent.Executors; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; import org.jboss.netty.channel.group.ChannelGroup; -import org.jboss.netty.channel.group.DefaultChannelGroup; import org.jboss.netty.channel.group.ChannelGroupFuture; +import org.jboss.netty.channel.group.DefaultChannelGroup; +import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; /** * Tracker server diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java index c71f7dd05..c86217182 100644 --- a/src/org/traccar/helper/NamedParameterStatement.java +++ b/src/org/traccar/helper/NamedParameterStatement.java @@ -15,17 +15,9 @@ */ package org.traccar.helper; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.sql.Types; - -import java.util.Map; -import java.util.HashMap; -import java.util.List; -import java.util.LinkedList; +import java.sql.*; import java.util.Date; +import java.util.*; public class NamedParameterStatement { diff --git a/src/org/traccar/DataManager.java b/src/org/traccar/model/DataManager.java index 9fc873bdb..a7338b3c4 100644 --- a/src/org/traccar/DataManager.java +++ b/src/org/traccar/model/DataManager.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar; +package org.traccar.model; import java.util.List; diff --git a/src/org/traccar/Device.java b/src/org/traccar/model/Device.java index 2fb6dfb4e..4b911d805 100644 --- a/src/org/traccar/Device.java +++ b/src/org/traccar/model/Device.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar; +package org.traccar.model; /** * Device diff --git a/src/org/traccar/Position.java b/src/org/traccar/model/Position.java index 520b49dd2..d2de726b1 100644 --- a/src/org/traccar/Position.java +++ b/src/org/traccar/model/Position.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar; +package org.traccar.model; import java.util.Date; diff --git a/src/org/traccar/protocol/Avl08ProtocolDecoder.java b/src/org/traccar/protocol/Avl08ProtocolDecoder.java index 7f564af37..13ee776cb 100644 --- a/src/org/traccar/protocol/Avl08ProtocolDecoder.java +++ b/src/org/traccar/protocol/Avl08ProtocolDecoder.java @@ -18,14 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; -import org.traccar.Device; // DELME +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * AVL-08 tracker protocol decoder diff --git a/src/org/traccar/protocol/Gl100ProtocolDecoder.java b/src/org/traccar/protocol/Gl100ProtocolDecoder.java index 4ec41f8c1..c28fc65a0 100644 --- a/src/org/traccar/protocol/Gl100ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl100ProtocolDecoder.java @@ -18,13 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * GL200 tracker protocol decoder diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index a43562eb9..9b3144fe8 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -18,13 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * GL200 tracker protocol decoder diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index e9c889af7..5142abae8 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -18,13 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * Gps 103 tracker protocol decoder diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index d1ffc7a4d..bf956ce4f 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -18,13 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * T55 tracker protocol decoder diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index 3eab4a386..8695d96b2 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -18,13 +18,13 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; -import java.util.regex.Pattern; import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * Gps 103 tracker protocol decoder diff --git a/src/org/traccar/protocol/Xexun2ProtocolDecoder.java b/src/org/traccar/protocol/Xexun2ProtocolDecoder.java index ce530785e..7c4f671f7 100644 --- a/src/org/traccar/protocol/Xexun2ProtocolDecoder.java +++ b/src/org/traccar/protocol/Xexun2ProtocolDecoder.java @@ -17,14 +17,14 @@ package org.traccar.protocol; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.regex.Pattern; -import java.util.regex.Matcher; import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * Xexun tracker protocol decoder diff --git a/src/org/traccar/protocol/XexunFrameDecoder.java b/src/org/traccar/protocol/XexunFrameDecoder.java index 86f409b3e..39bbc66dc 100644 --- a/src/org/traccar/protocol/XexunFrameDecoder.java +++ b/src/org/traccar/protocol/XexunFrameDecoder.java @@ -15,10 +15,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.handler.codec.frame.FrameDecoder; public class XexunFrameDecoder extends FrameDecoder { diff --git a/src/org/traccar/protocol/XexunProtocolDecoder.java b/src/org/traccar/protocol/XexunProtocolDecoder.java index 398b9a257..34059501b 100644 --- a/src/org/traccar/protocol/XexunProtocolDecoder.java +++ b/src/org/traccar/protocol/XexunProtocolDecoder.java @@ -15,17 +15,17 @@ */ package org.traccar.protocol; +import java.text.ParseException; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.regex.Pattern; -import java.util.regex.Matcher; -import java.text.ParseException; import java.util.TimeZone; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; -import org.traccar.Position; -import org.traccar.DataManager; +import org.traccar.model.DataManager; import org.traccar.GenericProtocolDecoder; +import org.traccar.model.Position; /** * Xexun tracker protocol decoder |