Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | package org.onlab.netty; |
| 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 |
| 38 | public static final byte[] PREAMBLE = "onosiscool".getBytes(); |
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) { |
| 71 | log.error("Exception inside channel handling pipeline.", cause); |
| 72 | context.close(); |
| 73 | } |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 74 | } |