Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 2 | * 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | package net.floodlightcontroller.core.internal; |
| 19 | |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 23 | import org.jboss.netty.channel.ChannelStateEvent; |
| 24 | import org.jboss.netty.channel.Channels; |
| 25 | import org.jboss.netty.channel.SimpleChannelUpstreamHandler; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | import org.jboss.netty.util.Timeout; |
| 27 | import org.jboss.netty.util.Timer; |
| 28 | import org.jboss.netty.util.TimerTask; |
| 29 | |
| 30 | /** |
| 31 | * Trigger a timeout if a switch fails to complete handshake soon enough |
| 32 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 33 | public class HandshakeTimeoutHandler |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 34 | extends SimpleChannelUpstreamHandler { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | static final HandshakeTimeoutException EXCEPTION = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | new HandshakeTimeoutException(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 38 | final OFChannelHandler channelHandler; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 39 | final Timer timer; |
| 40 | final long timeoutNanos; |
| 41 | volatile Timeout timeout; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | public HandshakeTimeoutHandler(OFChannelHandler channelHandler, |
| 44 | Timer timer, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 45 | long timeoutSeconds) { |
| 46 | super(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 47 | this.channelHandler = channelHandler; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | this.timer = timer; |
| 49 | this.timeoutNanos = TimeUnit.SECONDS.toNanos(timeoutSeconds); |
| 50 | |
| 51 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 55 | throws Exception { |
| 56 | if (timeoutNanos > 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | timeout = timer.newTimeout(new HandshakeTimeoutTask(ctx), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 58 | timeoutNanos, TimeUnit.NANOSECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 59 | } |
| 60 | ctx.sendUpstream(e); |
| 61 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | @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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 72 | 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 89 | if (!channelHandler.isHandshakeComplete()) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 90 | Channels.fireExceptionCaught(ctx, EXCEPTION); |
| 91 | } |
| 92 | } |
| 93 | } |