Jian Li | e4f1216 | 2016-09-13 00:09:09 +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 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 17 | |
| 18 | |
| 19 | import io.netty.buffer.ByteBuf; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 20 | import io.netty.buffer.Unpooled; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 21 | import io.netty.channel.ChannelHandlerContext; |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 22 | import io.netty.channel.socket.DatagramPacket; |
| 23 | import io.netty.handler.codec.MessageToMessageEncoder; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispMessage; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Encode a LISP message for output into a ByteBuffer, |
| 30 | * for use in a netty pipeline. |
| 31 | */ |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 32 | public class LispMessageEncoder extends MessageToMessageEncoder { |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 33 | |
| 34 | @Override |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 35 | protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception { |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 36 | if (!(msg instanceof List)) { |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 37 | ByteBuf byteBuf = Unpooled.buffer(); |
| 38 | ((LispMessage) msg).writeTo(byteBuf); |
| 39 | out.add(new DatagramPacket(byteBuf, ((LispMessage) msg).getSender())); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
| 43 | List<LispMessage> msgList = (List<LispMessage>) msg; |
| 44 | |
| 45 | for (LispMessage message : msgList) { |
| 46 | if (message != null) { |
Jian Li | afe2d3f | 2016-11-01 02:49:07 +0900 | [diff] [blame] | 47 | ByteBuf byteBuf = Unpooled.buffer(); |
| 48 | message.writeTo(byteBuf); |
Jian Li | 2174e32 | 2016-12-14 03:28:59 +0900 | [diff] [blame] | 49 | out.add(new DatagramPacket(byteBuf, message.getSender())); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |