Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.lisp.ctl; |
| 17 | |
| 18 | import io.netty.channel.ChannelHandlerContext; |
| 19 | import io.netty.channel.ChannelInboundHandlerAdapter; |
| 20 | import io.netty.handler.timeout.IdleState; |
| 21 | import io.netty.handler.timeout.IdleStateEvent; |
| 22 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
| 23 | import org.onosproject.lisp.msg.protocols.LispMapRegister; |
| 24 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
| 25 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | /** |
| 30 | * Channel handler deals with the xTR connection and dispatches xTR messages |
| 31 | * to the appropriate locations. |
| 32 | */ |
| 33 | public class LispChannelHandler extends ChannelInboundHandlerAdapter { |
| 34 | |
| 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
| 37 | @Override |
| 38 | public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { |
| 39 | |
| 40 | if (msg instanceof LispMapRegister) { |
| 41 | LispMapServer mapServer = new LispMapServer(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 42 | LispMapNotify mapNotify = |
| 43 | (LispMapNotify) mapServer.processMapRegister((LispMapRegister) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 44 | |
| 45 | // TODO: deserialize mapNotify message and write to channel |
| 46 | } |
| 47 | |
| 48 | if (msg instanceof LispMapRequest) { |
| 49 | LispMapResolver mapResolver = new LispMapResolver(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame^] | 50 | LispMapReply mapReply = |
| 51 | (LispMapReply) mapResolver.processMapRequest((LispMapRequest) msg); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 52 | |
| 53 | // TODO: deserialize mapReply message and write to channel |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) |
| 59 | throws Exception { |
| 60 | |
| 61 | if (evt instanceof IdleStateEvent) { |
| 62 | IdleStateEvent event = (IdleStateEvent) evt; |
| 63 | if (event.state() == IdleState.READER_IDLE) { |
| 64 | log.info("READER_IDLE read timeout"); |
| 65 | ctx.disconnect(); |
| 66 | } else if (event.state() == IdleState.WRITER_IDLE) { |
| 67 | log.info("WRITER_IDLE write timeout"); |
| 68 | ctx.disconnect(); |
| 69 | } else if (event.state() == IdleState.ALL_IDLE) { |
| 70 | log.info("ALL_IDLE total timeout"); |
| 71 | ctx.disconnect(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
| 78 | throws Exception { |
| 79 | log.warn(cause.getMessage()); |
| 80 | ctx.close(); |
| 81 | } |
| 82 | } |