Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.routing.fpm; |
| 18 | |
| 19 | import org.jboss.netty.channel.Channel; |
| 20 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 21 | import org.jboss.netty.channel.ChannelStateEvent; |
| 22 | import org.jboss.netty.channel.ExceptionEvent; |
| 23 | import org.jboss.netty.channel.MessageEvent; |
| 24 | import org.jboss.netty.channel.SimpleChannelHandler; |
| 25 | import org.onosproject.routing.fpm.protocol.FpmHeader; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Session handler for FPM protocol. |
| 33 | */ |
| 34 | public class FpmSessionHandler extends SimpleChannelHandler { |
| 35 | |
| 36 | private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class); |
| 37 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 38 | private final FpmListener fpmListener; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 39 | |
| 40 | private Channel channel; |
| 41 | |
| 42 | /** |
| 43 | * Class constructor. |
| 44 | * |
| 45 | * @param fpmListener listener for FPM messages |
| 46 | */ |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 47 | public FpmSessionHandler(FpmListener fpmListener) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 48 | this.fpmListener = checkNotNull(fpmListener); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) |
| 53 | throws Exception { |
| 54 | FpmHeader fpmMessage = (FpmHeader) e.getMessage(); |
| 55 | fpmListener.fpmMessage(fpmMessage); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) |
| 60 | throws Exception { |
| 61 | log.error("Exception thrown while handling FPM message", e.getCause()); |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 62 | if (channel != null) { |
| 63 | channel.close(); |
| 64 | } |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 65 | handleDisconnect(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 70 | throws Exception { |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 71 | if (!fpmListener.peerConnected(ctx.getChannel().getRemoteAddress())) { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 72 | log.error("Received new FPM connection while already connected"); |
| 73 | ctx.getChannel().close(); |
| 74 | return; |
| 75 | } |
| 76 | |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 77 | channel = ctx.getChannel(); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 82 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 87 | throws Exception { |
| 88 | handleDisconnect(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) |
| 93 | throws Exception { |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private void handleDisconnect() { |
Jonathan Hart | 6b04558 | 2016-02-03 10:00:08 -0800 | [diff] [blame] | 97 | fpmListener.peerDisconnected(channel.getRemoteAddress()); |
| 98 | channel = null; |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 99 | } |
| 100 | } |