Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 16 | package org.onlab.netty; |
| 17 | |
Yuta HIGUCHI | 813fb90 | 2014-11-07 01:49:33 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
Yuta HIGUCHI | f3fbdd0 | 2014-11-04 09:46:23 -0800 | [diff] [blame] | 19 | import java.nio.charset.StandardCharsets; |
| 20 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 24 | import io.netty.buffer.ByteBuf; |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 25 | import io.netty.channel.ChannelHandler.Sharable; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 26 | import io.netty.channel.ChannelHandlerContext; |
| 27 | import io.netty.handler.codec.MessageToByteEncoder; |
| 28 | |
| 29 | /** |
| 30 | * Encode InternalMessage out into a byte buffer. |
| 31 | */ |
Madan Jampani | ddf7622 | 2014-10-04 23:48:44 -0700 | [diff] [blame] | 32 | @Sharable |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 33 | public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { |
| 34 | |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 35 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 36 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 37 | // onosiscool in ascii |
Yuta HIGUCHI | f3fbdd0 | 2014-11-04 09:46:23 -0800 | [diff] [blame] | 38 | static final byte[] PREAMBLE = "onosiscool".getBytes(StandardCharsets.US_ASCII); |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 39 | public static final int HEADER_VERSION = 1; |
| 40 | public static final int SERIALIZER_VERSION = 1; |
| 41 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 42 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 43 | private static final KryoSerializer SERIALIZER = new KryoSerializer(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 44 | |
| 45 | @Override |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 46 | protected void encode( |
| 47 | ChannelHandlerContext context, |
| 48 | InternalMessage message, |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 49 | ByteBuf out) throws Exception { |
| 50 | |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 51 | // write version |
| 52 | out.writeInt(HEADER_VERSION); |
| 53 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 54 | // write preamble |
| 55 | out.writeBytes(PREAMBLE); |
| 56 | |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 57 | byte[] payload = SERIALIZER.encode(message); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 58 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 59 | // write payload length |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 60 | out.writeInt(payload.length); |
| 61 | |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 62 | // write payloadSerializer version |
Madan Jampani | 938aa43 | 2014-10-04 17:37:23 -0700 | [diff] [blame] | 63 | out.writeInt(SERIALIZER_VERSION); |
| 64 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 65 | // write payload. |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 66 | out.writeBytes(payload); |
| 67 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | public void exceptionCaught(ChannelHandlerContext context, Throwable cause) { |
Yuta HIGUCHI | 813fb90 | 2014-11-07 01:49:33 -0800 | [diff] [blame] | 71 | if (cause instanceof IOException) { |
| 72 | log.debug("IOException inside channel handling pipeline.", cause); |
| 73 | } else { |
| 74 | log.error("non-IOException inside channel handling pipeline.", cause); |
| 75 | } |
Madan Jampani | 29e5dfd | 2014-10-07 17:26:25 -0700 | [diff] [blame] | 76 | context.close(); |
| 77 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 78 | } |