diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2010-07-27 20:26:26 +0000 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2010-07-27 20:26:26 +0000 |
commit | f60fe194659614fcb65339fc1772308f7ced5126 (patch) | |
tree | a7af5561b806a18a44ad80acf5d7fef290bd3bd1 /src | |
parent | 8c958b95a3b01c1b1b392723e2b35c7575e3a45e (diff) | |
download | trackermap-server-f60fe194659614fcb65339fc1772308f7ced5126.tar.gz trackermap-server-f60fe194659614fcb65339fc1772308f7ced5126.tar.bz2 trackermap-server-f60fe194659614fcb65339fc1772308f7ced5126.zip |
fix gps103 protocol
Diffstat (limited to 'src')
-rw-r--r-- | src/net/sourceforge/opentracking/Server.java | 2 | ||||
-rw-r--r-- | src/net/sourceforge/opentracking/protocol/gps103/Gps103ProtocolDecoder.java | 46 |
2 files changed, 46 insertions, 2 deletions
diff --git a/src/net/sourceforge/opentracking/Server.java b/src/net/sourceforge/opentracking/Server.java index acee30037..278a4589a 100644 --- a/src/net/sourceforge/opentracking/Server.java +++ b/src/net/sourceforge/opentracking/Server.java @@ -33,6 +33,7 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import net.sourceforge.opentracking.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 org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.logging.LoggingHandler; @@ -241,6 +242,7 @@ public class Server implements DataManager { server.getPipeline().addLast("frameDecoder", new DelimiterBasedFrameDecoder(1024, ChannelBuffers.wrappedBuffer(delimiter))); server.getPipeline().addLast("stringDecoder", new StringDecoder()); + server.getPipeline().addLast("stringEncoder", new StringEncoder()); server.getPipeline().addLast("objectDecoder", new Gps103ProtocolDecoder(this)); server.getPipeline().addLast("handler", new TrackerEventHandler(this)); diff --git a/src/net/sourceforge/opentracking/protocol/gps103/Gps103ProtocolDecoder.java b/src/net/sourceforge/opentracking/protocol/gps103/Gps103ProtocolDecoder.java index 0b9b1c5bf..26637f3f1 100644 --- a/src/net/sourceforge/opentracking/protocol/gps103/Gps103ProtocolDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/gps103/Gps103ProtocolDecoder.java @@ -18,6 +18,8 @@ package net.sourceforge.opentracking.protocol.gps103; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; +import java.util.Timer; +import java.util.TimerTask; import java.util.regex.Pattern; import java.util.regex.Matcher; import org.jboss.netty.channel.Channel; @@ -26,6 +28,9 @@ import org.jboss.netty.handler.codec.oneone.OneToOneDecoder; import org.jboss.netty.channel.ChannelPipelineCoverage; import net.sourceforge.opentracking.Position; import net.sourceforge.opentracking.DataManager; +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelState; +import org.jboss.netty.channel.ChannelStateEvent; /** * Gps 103 tracker protocol decoder @@ -69,9 +74,14 @@ public class Gps103ProtocolDecoder extends OneToOneDecoder { ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { - // Parse message - String sentence = (String) msg; + + // Send response + if (sentence.contains("##")) { + channel.write("LOAD"); + } + + // Parse message Matcher parser = pattern.matcher(sentence); if (!parser.matches()) { return null; @@ -116,4 +126,36 @@ public class Gps103ProtocolDecoder extends OneToOneDecoder { return position; } + /** + * Disconnect channel + */ + class DisconnectTask extends TimerTask { + private Channel channel; + + public DisconnectTask(Channel channel) { + this.channel = channel; + } + + public void run() { + channel.disconnect(); + } + } + + /** + * Handle connect event + */ + @Override + public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent evt) throws Exception { + super.handleUpstream(ctx, evt); + + if (evt instanceof ChannelStateEvent) { + ChannelStateEvent event = (ChannelStateEvent) evt; + + if (event.getState() == ChannelState.CONNECTED && event.getValue() != null) { + new Timer().schedule(new DisconnectTask(evt.getChannel()) , 5 * 60 * 1000); + } + } + + } + } |