blob: 95ce7600719bcdfabd7b75d016396ddf05ef7e4b [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
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
17package org.onosproject.routing.fpm;
18
19import org.jboss.netty.channel.Channel;
20import org.jboss.netty.channel.ChannelHandlerContext;
21import org.jboss.netty.channel.ChannelStateEvent;
22import org.jboss.netty.channel.ExceptionEvent;
23import org.jboss.netty.channel.MessageEvent;
24import org.jboss.netty.channel.SimpleChannelHandler;
25import org.onosproject.routing.fpm.protocol.FpmHeader;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Session handler for FPM protocol.
33 */
34public class FpmSessionHandler extends SimpleChannelHandler {
35
36 private static Logger log = LoggerFactory.getLogger(FpmSessionHandler.class);
37
38 private final FpmMessageListener fpmListener;
39
40 private Channel channel;
41
42 /**
43 * Class constructor.
44 *
45 * @param fpmListener listener for FPM messages
46 */
47 public FpmSessionHandler(FpmMessageListener fpmListener) {
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());
62 channel.close();
63 handleDisconnect();
64 }
65
66 @Override
67 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
68 throws Exception {
69 if (this.channel != null) {
70 log.error("Received new FPM connection while already connected");
71 ctx.getChannel().close();
72 return;
73 }
74
75 this.channel = ctx.getChannel();
76 }
77
78 @Override
79 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
80 throws Exception {
81 super.channelConnected(ctx, e);
82 }
83
84 @Override
85 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e)
86 throws Exception {
87 handleDisconnect();
88 }
89
90 @Override
91 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
92 throws Exception {
93 handleDisconnect();
94 }
95
96 private void handleDisconnect() {
97 this.channel = null;
98 }
99}