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