2 * Copyright 2014 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onosproject.openflow.controller.impl;
19 import java.util.concurrent.TimeUnit;
21 import org.jboss.netty.channel.ChannelHandlerContext;
22 import org.jboss.netty.channel.ChannelStateEvent;
23 import org.jboss.netty.channel.Channels;
24 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
25 import org.jboss.netty.util.Timeout;
26 import org.jboss.netty.util.Timer;
27 import org.jboss.netty.util.TimerTask;
30 * Trigger a timeout if a switch fails to complete handshake soon enough.
32 public class HandshakeTimeoutHandler
33 extends SimpleChannelUpstreamHandler {
34 static final HandshakeTimeoutException EXCEPTION =
35 new HandshakeTimeoutException();
37 final OFChannelHandler channelHandler;
39 final long timeoutNanos;
40 volatile Timeout timeout;
42 public HandshakeTimeoutHandler(OFChannelHandler channelHandler,
44 long timeoutSeconds) {
46 this.channelHandler = channelHandler;
48 this.timeoutNanos = TimeUnit.SECONDS.toNanos(timeoutSeconds);
53 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
55 if (timeoutNanos > 0) {
56 timeout = timer.newTimeout(new HandshakeTimeoutTask(ctx),
57 timeoutNanos, TimeUnit.NANOSECONDS);
63 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
65 if (timeout != null) {
71 private final class HandshakeTimeoutTask implements TimerTask {
73 private final ChannelHandlerContext ctx;
75 HandshakeTimeoutTask(ChannelHandlerContext ctx) {
80 public void run(Timeout t) throws Exception {
81 if (t.isCancelled()) {
85 if (!ctx.getChannel().isOpen()) {
88 if (!channelHandler.isHandshakeComplete()) {
89 Channels.fireExceptionCaught(ctx, EXCEPTION);