diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-05-29 23:27:46 +0400 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-05-29 23:27:46 +0400 |
commit | 3b61199ca03a602514457cf7ed640ff6d9a39519 (patch) | |
tree | 14099d0648a01f5dc4299894df0fe232ff4870cb /src | |
parent | bf8177e17e6ce607e60fe6e43ecf7401af51a03c (diff) | |
download | trackermap-server-3b61199ca03a602514457cf7ed640ff6d9a39519.tar.gz trackermap-server-3b61199ca03a602514457cf7ed640ff6d9a39519.tar.bz2 trackermap-server-3b61199ca03a602514457cf7ed640ff6d9a39519.zip |
fixed #3
Diffstat (limited to 'src')
-rw-r--r-- | src/Main.java | 5 | ||||
-rw-r--r-- | src/org/traccar/GenericPipelineFactory.java | 47 | ||||
-rw-r--r-- | src/org/traccar/Server.java | 10 | ||||
-rw-r--r-- | src/org/traccar/TrackerEventHandler.java | 7 | ||||
-rw-r--r-- | src/org/traccar/http/WebServer.java | 7 | ||||
-rw-r--r-- | src/org/traccar/model/DatabaseDataManager.java | 3 | ||||
-rw-r--r-- | src/org/traccar/protocol/T55ProtocolDecoder.java | 16 |
7 files changed, 65 insertions, 30 deletions
diff --git a/src/Main.java b/src/Main.java index d8a3b397c..8b6f2002e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ import org.traccar.Server; +import org.traccar.helper.Log; public class Main { @@ -7,14 +8,14 @@ public class Main { final Server service = new Server(); service.init(args); - System.out.println("starting server..."); + Log.info("starting server..."); service.start(); // Shutdown server properly Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { - System.out.println("shutting down server..."); + Log.info("shutting down server..."); service.stop(); } }); diff --git a/src/org/traccar/GenericPipelineFactory.java b/src/org/traccar/GenericPipelineFactory.java index 13f4c36ac..7577b70fc 100644 --- a/src/org/traccar/GenericPipelineFactory.java +++ b/src/org/traccar/GenericPipelineFactory.java @@ -15,8 +15,11 @@ */ package org.traccar; +import java.net.InetSocketAddress; +import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.*; import org.jboss.netty.handler.logging.LoggingHandler; +import org.traccar.helper.Log; import org.traccar.model.DataManager; /** @@ -44,31 +47,59 @@ public abstract class GenericPipelineFactory implements ChannelPipelineFactory { server.getChannelGroup().add(e.getChannel()); } } - + + /** + * Logging using global logger + */ + protected class StandardLoggingHandler extends LoggingHandler { + + static final String HEX_CHARS = "0123456789ABCDEF"; + + @Override + public void log(ChannelEvent e) { + if (e instanceof MessageEvent) { + MessageEvent event = (MessageEvent) e; + String msg = "[" + ((InetSocketAddress) e.getChannel().getLocalAddress()).getPort() + " - "; + msg += ((InetSocketAddress) event.getRemoteAddress()).getAddress().getHostAddress() + "]"; + + // Append hex message + if (event.getMessage() instanceof ChannelBuffer) { + ChannelBuffer buffer = (ChannelBuffer) event.getMessage(); + msg += " - (HEX: "; + for (int i = buffer.readerIndex(); i < buffer.writerIndex(); i++) { + byte b = buffer.getByte(i); + msg += HEX_CHARS.charAt((b & 0xf0) >> 4); + msg += HEX_CHARS.charAt((b & 0x0F)); + } + msg += ")"; + } + + Log.fine(msg); + } + } + } + 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")); + pipeline.addLast("logger", new StandardLoggingHandler()); } 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)); + pipeline.addLast("handler", new TrackerEventHandler(dataManager)); return pipeline; } }
\ No newline at end of file diff --git a/src/org/traccar/Server.java b/src/org/traccar/Server.java index 654d32321..cbcb68324 100644 --- a/src/org/traccar/Server.java +++ b/src/org/traccar/Server.java @@ -30,6 +30,7 @@ import org.jboss.netty.channel.ChannelPipeline; 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.Log; import org.traccar.http.WebServer; import org.traccar.model.DataManager; import org.traccar.model.DatabaseDataManager; @@ -132,12 +133,12 @@ public class Server { if (loggerEnabled) { - Logger logger = Logger.getLogger("logger"); String fileName = properties.getProperty("logger.file"); if (fileName != null) { FileHandler file = new FileHandler(fileName, true); + // Simple formatter file.setFormatter(new Formatter() { private final String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); @@ -147,15 +148,16 @@ public class Server { public String format(LogRecord record) { String line = dateFormat.format(new Date(record.getMillis())); - line += " - "; + line += " " + record.getLevel().getName() + ": "; line += formatMessage(record); line += LINE_SEPARATOR; return line; } }); - logger.setLevel(Level.ALL); - logger.addHandler(file); + // NOTE: Console logger level will remain INFO + Log.getLogger().setLevel(Level.ALL); + Log.getLogger().addHandler(file); } } } diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java index 8378dece3..e4e45a174 100644 --- a/src/org/traccar/TrackerEventHandler.java +++ b/src/org/traccar/TrackerEventHandler.java @@ -16,6 +16,7 @@ package org.traccar; import org.jboss.netty.channel.*; +import org.traccar.helper.Log; import org.traccar.model.DataManager; import org.traccar.model.Position; @@ -43,9 +44,9 @@ public class TrackerEventHandler extends SimpleChannelHandler { Position position = (Position) e.getMessage(); if (position == null) { - System.out.println("null message"); + Log.info("null message"); } else { - System.out.println( + Log.info( "id: " + position.getId() + ", deviceId: " + position.getDeviceId() + ", valid: " + position.getValid() + @@ -61,7 +62,7 @@ public class TrackerEventHandler extends SimpleChannelHandler { try { dataManager.addPosition(position); } catch (Exception error) { - System.out.println(error.getMessage()); + Log.warning(error.getMessage()); } } } diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java index 53cdb2966..cf7b21623 100644 --- a/src/org/traccar/http/WebServer.java +++ b/src/org/traccar/http/WebServer.java @@ -28,6 +28,7 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.AbstractHandler; +import org.traccar.helper.Log; import org.traccar.model.DataManager; import org.traccar.model.Device; import org.traccar.model.Position; @@ -160,7 +161,7 @@ public class WebServer { } } catch (Exception error) { out.print("{'success':false,'results':["); - System.out.println(error.getMessage()); + Log.warning(error.getMessage()); } out.print("]}"); @@ -196,7 +197,7 @@ public class WebServer { try { server.start(); } catch (Exception error) { - System.out.println(error.getMessage()); + Log.warning(error.getMessage()); } } @@ -204,7 +205,7 @@ public class WebServer { try { server.stop(); } catch (Exception error) { - System.out.println(error.getMessage()); + Log.warning(error.getMessage()); } } diff --git a/src/org/traccar/model/DatabaseDataManager.java b/src/org/traccar/model/DatabaseDataManager.java index ece8014ce..ed3335342 100644 --- a/src/org/traccar/model/DatabaseDataManager.java +++ b/src/org/traccar/model/DatabaseDataManager.java @@ -19,6 +19,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; import org.traccar.helper.AdvancedConnection; +import org.traccar.helper.Log; import org.traccar.helper.NamedParameterStatement; /** @@ -95,7 +96,7 @@ public class DatabaseDataManager implements DataManager { initializeQuery.prepare(); initializeQuery.executeUpdate(); } catch (Exception error) { - System.out.println(error.getMessage()); + Log.warning(error.getMessage()); } } diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index bf956ce4f..98d0079a6 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -67,8 +67,6 @@ public class T55ProtocolDecoder extends GenericProtocolDecoder { throws Exception { String sentence = (String) msg; - - //System.out.println(sentence); // Detect device ID if (sentence.contains("$PGID")) { @@ -89,11 +87,11 @@ public class T55ProtocolDecoder extends GenericProtocolDecoder { if (!parser.matches()) { return null; } - + // Create new position Position position = new Position(); position.setDeviceId(deviceId); - + Integer index = 1; // Time @@ -103,10 +101,10 @@ public class T55ProtocolDecoder extends GenericProtocolDecoder { time.set(Calendar.MINUTE, Integer.valueOf(parser.group(index++))); time.set(Calendar.SECOND, Integer.valueOf(parser.group(index++))); index += 1; // Skip milliseconds - + // Validity position.setValid(parser.group(index++).compareTo("A") == 0 ? true : false); - + // Latitude Double latitude = Double.valueOf(parser.group(index++)); latitude += Double.valueOf(parser.group(index++)) / 60; @@ -134,16 +132,16 @@ public class T55ProtocolDecoder extends GenericProtocolDecoder { } else { position.setCourse(0.0); } - + // Date time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(parser.group(index++))); time.set(Calendar.MONTH, Integer.valueOf(parser.group(index++)) - 1); time.set(Calendar.YEAR, 2000 + Integer.valueOf(parser.group(index++))); position.setTime(time.getTime()); - + // Altitude position.setAltitude(0.0); - + return position; } |