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 | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 19 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.esotericsoftware.kryo.Serializer; |
| 22 | import com.esotericsoftware.kryo.io.Input; |
| 23 | import com.esotericsoftware.kryo.io.Output; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 24 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 25 | import java.nio.ByteBuffer; |
| 26 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Kryo Serializer. |
| 29 | */ |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 30 | public class KryoSerializer { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 31 | |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 32 | private KryoNamespace serializerPool; |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 33 | |
| 34 | public KryoSerializer() { |
| 35 | setupKryoPool(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Sets up the common serialzers pool. |
| 40 | */ |
| 41 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 42 | serializerPool = KryoNamespace.newBuilder() |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 43 | .register(byte[].class) |
| 44 | .register(new InternalMessageSerializer(), InternalMessage.class) |
| 45 | .register(new EndPointSerializer(), Endpoint.class) |
| 46 | .build(); |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 50 | public <T> T decode(byte[] data) { |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 51 | return serializerPool.deserialize(data); |
| 52 | } |
| 53 | |
Madan Jampani | ab6d311 | 2014-10-02 16:30:14 -0700 | [diff] [blame] | 54 | public byte[] encode(Object payload) { |
| 55 | return serializerPool.serialize(payload); |
| 56 | } |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 57 | |
Yuta HIGUCHI | cad78e9 | 2014-10-06 17:58:36 -0700 | [diff] [blame] | 58 | public <T> T decode(ByteBuffer buffer) { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 59 | return serializerPool.deserialize(buffer); |
| 60 | } |
| 61 | |
Yuta HIGUCHI | cad78e9 | 2014-10-06 17:58:36 -0700 | [diff] [blame] | 62 | public void encode(Object obj, ByteBuffer buffer) { |
Madan Jampani | 86ed055 | 2014-10-03 16:45:42 -0700 | [diff] [blame] | 63 | serializerPool.serialize(obj, buffer); |
| 64 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 65 | |
| 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 HIGUCHI | 92626c0 | 2014-10-06 15:46:18 -0700 | [diff] [blame] | 106 | } |