diff options
Diffstat (limited to 'src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java')
-rw-r--r-- | src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java b/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java index 7b21a1a72..94b1cb31b 100644 --- a/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java +++ b/src/net/sourceforge/opentracking/protocol/xexun/XexunProtocolDecoder.java @@ -20,13 +20,17 @@ import java.util.GregorianCalendar; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.text.ParseException; +import java.util.Timer; +import java.util.TimerTask; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; 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 java.sql.SQLException; +import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelState; +import org.jboss.netty.channel.ChannelStateEvent; /** * Xexun tracker protocol decoder @@ -40,10 +44,16 @@ public class XexunProtocolDecoder extends OneToOneDecoder { private DataManager dataManager; /** + * Reset connection delay + */ + private Integer resetDelay; + + /** * Init device table */ - public XexunProtocolDecoder(DataManager newDataManager) { - dataManager = newDataManager; + public XexunProtocolDecoder(DataManager dataManager, Integer resetDelay) { + this.dataManager = dataManager; + this.resetDelay = resetDelay; } /** @@ -129,4 +139,35 @@ public class XexunProtocolDecoder 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 && resetDelay != 0) { + new Timer().schedule(new DisconnectTask(evt.getChannel()), resetDelay); + } + } + } + } |