diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2012-03-22 04:54:46 +0000 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2012-03-22 04:54:46 +0000 |
commit | d0de5245574e223384d9e89421850af16fb7a848 (patch) | |
tree | 8203e522ecfc6540284ef3c29c9af1c42be5e84a /src/org/traccar/GenericProtocolDecoder.java | |
parent | 2298302ea7f3b4f09b52638355c2b86a952a8306 (diff) | |
download | trackermap-server-d0de5245574e223384d9e89421850af16fb7a848.tar.gz trackermap-server-d0de5245574e223384d9e89421850af16fb7a848.tar.bz2 trackermap-server-d0de5245574e223384d9e89421850af16fb7a848.zip |
Diffstat (limited to 'src/org/traccar/GenericProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/GenericProtocolDecoder.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/org/traccar/GenericProtocolDecoder.java b/src/org/traccar/GenericProtocolDecoder.java new file mode 100644 index 000000000..0d79818ab --- /dev/null +++ b/src/org/traccar/GenericProtocolDecoder.java @@ -0,0 +1,88 @@ +/* + * 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 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.ChannelEvent; +import org.jboss.netty.channel.ChannelState; +import org.jboss.netty.channel.ChannelStateEvent; + +/** + * Base class for protocol decoders + */ +public abstract class GenericProtocolDecoder extends OneToOneDecoder { + + /** + * Data manager + */ + private DataManager dataManager; + + /** + * Return data manager + */ + public DataManager getDataManager() { + return dataManager; + } + + /** + * Reset connection delay + */ + private Integer resetDelay; + + /** + * Initialize variables + */ + public GenericProtocolDecoder(DataManager dataManager, Integer resetDelay) { + this.dataManager = dataManager; + this.resetDelay = resetDelay; + } + + /** + * Disconnect channel + */ + private 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 GenericProtocolDecoder.DisconnectTask(evt.getChannel()), resetDelay); + } + } + } + +} |