blob: c9aa3ca5754711be7a693232ead1ebdab9573922 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07002* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.internal;
19
20import java.util.concurrent.TimeUnit;
21
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022import org.jboss.netty.channel.ChannelHandlerContext;
23import org.jboss.netty.channel.ChannelStateEvent;
24import org.jboss.netty.channel.Channels;
25import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026import org.jboss.netty.util.Timeout;
27import org.jboss.netty.util.Timer;
28import org.jboss.netty.util.TimerTask;
29
30/**
31 * Trigger a timeout if a switch fails to complete handshake soon enough
32 */
Ray Milkey269ffb92014-04-03 14:43:30 -070033public class HandshakeTimeoutHandler
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070034 extends SimpleChannelUpstreamHandler {
Ray Milkey269ffb92014-04-03 14:43:30 -070035 static final HandshakeTimeoutException EXCEPTION =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036 new HandshakeTimeoutException();
Ray Milkey269ffb92014-04-03 14:43:30 -070037
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070038 final OFChannelHandler channelHandler;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039 final Timer timer;
40 final long timeoutNanos;
41 volatile Timeout timeout;
Ray Milkey269ffb92014-04-03 14:43:30 -070042
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070043 public HandshakeTimeoutHandler(OFChannelHandler channelHandler,
44 Timer timer,
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080045 long timeoutSeconds) {
46 super();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070047 this.channelHandler = channelHandler;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048 this.timer = timer;
49 this.timeoutNanos = TimeUnit.SECONDS.toNanos(timeoutSeconds);
50
51 }
Ray Milkey269ffb92014-04-03 14:43:30 -070052
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053 @Override
54 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
55 throws Exception {
56 if (timeoutNanos > 0) {
Ray Milkey269ffb92014-04-03 14:43:30 -070057 timeout = timer.newTimeout(new HandshakeTimeoutTask(ctx),
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070058 timeoutNanos, TimeUnit.NANOSECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 }
60 ctx.sendUpstream(e);
61 }
Ray Milkey269ffb92014-04-03 14:43:30 -070062
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 @Override
64 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
65 throws Exception {
66 if (timeout != null) {
67 timeout.cancel();
68 timeout = null;
69 }
70 }
71
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080072 private final class HandshakeTimeoutTask implements TimerTask {
73
74 private final ChannelHandlerContext ctx;
75
76 HandshakeTimeoutTask(ChannelHandlerContext ctx) {
77 this.ctx = ctx;
78 }
79
80 @Override
81 public void run(Timeout timeout) throws Exception {
82 if (timeout.isCancelled()) {
83 return;
84 }
85
86 if (!ctx.getChannel().isOpen()) {
87 return;
88 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070089 if (!channelHandler.isHandshakeComplete())
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080090 Channels.fireExceptionCaught(ctx, EXCEPTION);
91 }
92 }
93}