blob: 6118c49c48cbd2819c42a5350507fb1d96608899 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
Madan Jampaniab6d3112014-10-02 16:30:14 -070016package org.onlab.netty;
17
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070018import org.onlab.util.KryoNamespace;
Madan Jampaniab6d3112014-10-02 16:30:14 -070019
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080020import com.esotericsoftware.kryo.Kryo;
21import com.esotericsoftware.kryo.Serializer;
22import com.esotericsoftware.kryo.io.Input;
23import com.esotericsoftware.kryo.io.Output;
Madan Jampaniab6d3112014-10-02 16:30:14 -070024
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080025import java.nio.ByteBuffer;
26
Madan Jampaniab6d3112014-10-02 16:30:14 -070027/**
28 * Kryo Serializer.
29 */
Madan Jampani53e44e62014-10-07 12:39:51 -070030public class KryoSerializer {
Madan Jampaniab6d3112014-10-02 16:30:14 -070031
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070032 private KryoNamespace serializerPool;
Madan Jampaniab6d3112014-10-02 16:30:14 -070033
34 public KryoSerializer() {
35 setupKryoPool();
36 }
37
38 /**
39 * Sets up the common serialzers pool.
40 */
41 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070042 serializerPool = KryoNamespace.newBuilder()
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080043 .register(byte[].class)
44 .register(new InternalMessageSerializer(), InternalMessage.class)
45 .register(new EndPointSerializer(), Endpoint.class)
46 .build();
Madan Jampaniab6d3112014-10-02 16:30:14 -070047 }
48
49
Madan Jampani86ed0552014-10-03 16:45:42 -070050 public <T> T decode(byte[] data) {
Madan Jampaniab6d3112014-10-02 16:30:14 -070051 return serializerPool.deserialize(data);
52 }
53
Madan Jampaniab6d3112014-10-02 16:30:14 -070054 public byte[] encode(Object payload) {
55 return serializerPool.serialize(payload);
56 }
Madan Jampani86ed0552014-10-03 16:45:42 -070057
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070058 public <T> T decode(ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070059 return serializerPool.deserialize(buffer);
60 }
61
Yuta HIGUCHIcad78e92014-10-06 17:58:36 -070062 public void encode(Object obj, ByteBuffer buffer) {
Madan Jampani86ed0552014-10-03 16:45:42 -070063 serializerPool.serialize(obj, buffer);
64 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080065
66 public static final class InternalMessageSerializer
67 extends Serializer<InternalMessage> {
68
69 @Override
70 public void write(Kryo kryo, Output output, InternalMessage object) {
71 output.writeLong(object.id());
72 kryo.writeClassAndObject(output, object.sender());
73 output.writeString(object.type());
74 output.writeInt(object.payload().length, true);
75 output.writeBytes(object.payload());
76 }
77
78 @Override
79 public InternalMessage read(Kryo kryo, Input input,
80 Class<InternalMessage> type) {
81 long id = input.readLong();
82 Endpoint sender = (Endpoint) kryo.readClassAndObject(input);
83 String msgtype = input.readString();
84 int length = input.readInt(true);
85 byte[] payload = input.readBytes(length);
86 return new InternalMessage(id, sender, msgtype, payload);
87 }
88
89 }
90
91 public static final class EndPointSerializer extends Serializer<Endpoint> {
92
93 @Override
94 public void write(Kryo kryo, Output output, Endpoint object) {
95 output.writeString(object.host());
96 output.writeInt(object.port());
97 }
98
99 @Override
100 public Endpoint read(Kryo kryo, Input input, Class<Endpoint> type) {
101 String host = input.readString();
102 int port = input.readInt();
103 return new Endpoint(host, port);
104 }
105 }
Yuta HIGUCHI92626c02014-10-06 15:46:18 -0700106}