Fixed netty & checkstyle failures
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / IdleHandler.java
index c3bd7ab77d7d814e2b81738617f58177210e6388..5f408e6c6c9396073786cdef2adf78c079c6e057 100644 (file)
@@ -1,36 +1,53 @@
-package org.opendaylight.openflowjava.protocol.impl.core;\r
-\r
-import java.util.concurrent.TimeUnit;\r
-\r
-import io.netty.channel.ChannelHandlerContext;\r
-import io.netty.handler.timeout.IdleState;\r
-import io.netty.handler.timeout.IdleStateEvent;\r
-import io.netty.handler.timeout.IdleStateHandler;\r
-\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-public class IdleHandler extends IdleStateHandler{\r
-    \r
-    public IdleHandler(long readerIdleTime, long writerIdleTime,\r
-            long allIdleTime, TimeUnit unit) {\r
-        super(readerIdleTime, writerIdleTime, allIdleTime, unit);\r
-    }\r
-\r
-\r
-    private static final Logger LOGGER = LoggerFactory.getLogger(IdleHandler.class);\r
-\r
-    \r
-    @Override\r
-    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)\r
-            throws Exception {\r
-        if ((evt.state() == IdleState.READER_IDLE) && (evt.isFirst())) {\r
-            LOGGER.info("Switch idle");\r
-            SwitchIdleEventBuilder builder = new SwitchIdleEventBuilder();\r
-            builder.setInfo("Switch idle");\r
-            ctx.fireChannelRead(builder.build());\r
-        }\r
-    }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowjava.protocol.impl.core;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.timeout.ReadTimeoutHandler;
+
+import java.util.concurrent.TimeUnit;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Detects idle state of switch and informs upper layers
+ * @author michal.polkorab
+ */
+public class IdleHandler extends ReadTimeoutHandler {
+
+    private static final Logger LOG = LoggerFactory.getLogger(IdleHandler.class);
+    private boolean first = true;
+
+    /**
+     * @param readerIdleTime
+     * @param unit
+     */
+    public IdleHandler(final long readerIdleTime, final TimeUnit unit) {
+        super(readerIdleTime, unit);
+    }
+
+    @Override
+    public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
+        super.channelRead(ctx, msg);
+        first = true;
+    }
+
+    @Override
+    protected void readTimedOut(final ChannelHandlerContext ctx) throws Exception {
+        if (first) {
+            LOG.debug("Switch idle");
+            SwitchIdleEventBuilder builder = new SwitchIdleEventBuilder();
+            builder.setInfo("Switch idle");
+            ctx.fireChannelRead(builder.build());
+            first = false;
+        }
+    }
+}