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(); |
| 42 | LispMapNotify mapNotify = (LispMapNotify) mapServer.process((LispMapRegister) msg); |
| 43 | |
| 44 | // TODO: deserialize mapNotify message and write to channel |
| 45 | } |
| 46 | |
| 47 | if (msg instanceof LispMapRequest) { |
| 48 | LispMapResolver mapResolver = new LispMapResolver(); |
| 49 | LispMapReply mapReply = (LispMapReply) mapResolver.process((LispMapRequest) msg); |
| 50 | |
| 51 | // TODO: deserialize mapReply message and write to channel |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) |
| 57 | throws Exception { |
| 58 | |
| 59 | if (evt instanceof IdleStateEvent) { |
| 60 | IdleStateEvent event = (IdleStateEvent) evt; |
| 61 | if (event.state() == IdleState.READER_IDLE) { |
| 62 | log.info("READER_IDLE read timeout"); |
| 63 | ctx.disconnect(); |
| 64 | } else if (event.state() == IdleState.WRITER_IDLE) { |
| 65 | log.info("WRITER_IDLE write timeout"); |
| 66 | ctx.disconnect(); |
| 67 | } else if (event.state() == IdleState.ALL_IDLE) { |
| 68 | log.info("ALL_IDLE total timeout"); |
| 69 | ctx.disconnect(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) |
| 76 | throws Exception { |
| 77 | log.warn(cause.getMessage()); |
| 78 | ctx.close(); |
| 79 | } |
| 80 | } |