blob: 5ed354ecdb6a6c78a207b784b7c4a3b5977508a4 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 Jampaniab6d3112014-10-02 16:30:14 -070019package org.onlab.netty;
20
Madan Jampani29e5dfd2014-10-07 17:26:25 -070021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
Madan Jampaniab6d3112014-10-02 16:30:14 -070024import io.netty.buffer.ByteBuf;
Madan Jampaniddf76222014-10-04 23:48:44 -070025import io.netty.channel.ChannelHandler.Sharable;
Madan Jampaniab6d3112014-10-02 16:30:14 -070026import io.netty.channel.ChannelHandlerContext;
27import io.netty.handler.codec.MessageToByteEncoder;
28
29/**
30 * Encode InternalMessage out into a byte buffer.
31 */
Madan Jampaniddf76222014-10-04 23:48:44 -070032@Sharable
Madan Jampaniab6d3112014-10-02 16:30:14 -070033public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
34
Madan Jampani29e5dfd2014-10-07 17:26:25 -070035 private final Logger log = LoggerFactory.getLogger(getClass());
36
Madan Jampaniab6d3112014-10-02 16:30:14 -070037 // onosiscool in ascii
38 public static final byte[] PREAMBLE = "onosiscool".getBytes();
Madan Jampani938aa432014-10-04 17:37:23 -070039 public static final int HEADER_VERSION = 1;
40 public static final int SERIALIZER_VERSION = 1;
41
Madan Jampaniab6d3112014-10-02 16:30:14 -070042
Madan Jampani53e44e62014-10-07 12:39:51 -070043 private static final KryoSerializer SERIALIZER = new KryoSerializer();
Madan Jampaniab6d3112014-10-02 16:30:14 -070044
45 @Override
Madan Jampani86ed0552014-10-03 16:45:42 -070046 protected void encode(
47 ChannelHandlerContext context,
48 InternalMessage message,
Madan Jampaniab6d3112014-10-02 16:30:14 -070049 ByteBuf out) throws Exception {
50
Madan Jampani938aa432014-10-04 17:37:23 -070051 // write version
52 out.writeInt(HEADER_VERSION);
53
Madan Jampaniab6d3112014-10-02 16:30:14 -070054 // write preamble
55 out.writeBytes(PREAMBLE);
56
Madan Jampani53e44e62014-10-07 12:39:51 -070057 byte[] payload = SERIALIZER.encode(message);
Madan Jampaniab6d3112014-10-02 16:30:14 -070058
Madan Jampani86ed0552014-10-03 16:45:42 -070059 // write payload length
Madan Jampaniab6d3112014-10-02 16:30:14 -070060 out.writeInt(payload.length);
61
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070062 // write payloadSerializer version
Madan Jampani938aa432014-10-04 17:37:23 -070063 out.writeInt(SERIALIZER_VERSION);
64
Madan Jampani86ed0552014-10-03 16:45:42 -070065 // write payload.
Madan Jampaniab6d3112014-10-02 16:30:14 -070066 out.writeBytes(payload);
67 }
Madan Jampani29e5dfd2014-10-07 17:26:25 -070068
69 @Override
70 public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
71 log.error("Exception inside channel handling pipeline.", cause);
72 context.close();
73 }
Madan Jampaniab6d3112014-10-02 16:30:14 -070074}