blob: d3f31bbcd62279d1b2065ac59a3926276212d475 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
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.
15 */
Yuta HIGUCHI60a190b2014-11-07 16:24:47 -080016package org.onlab.onos.store.serializers.impl;
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070017
18import org.onlab.onos.cluster.NodeId;
19import org.onlab.onos.store.cluster.messaging.ClusterMessage;
20import org.onlab.onos.store.cluster.messaging.MessageSubject;
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070021import com.esotericsoftware.kryo.Kryo;
22import com.esotericsoftware.kryo.Serializer;
23import com.esotericsoftware.kryo.io.Input;
24import com.esotericsoftware.kryo.io.Output;
25
26public final class ClusterMessageSerializer extends Serializer<ClusterMessage> {
27
Yuta HIGUCHI3e0b6512014-10-07 17:43:56 -070028 /**
29 * Creates a serializer for {@link ClusterMessage}.
30 */
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070031 public ClusterMessageSerializer() {
32 // does not accept null
33 super(false);
34 }
35
36 @Override
Madan Jampani53e44e62014-10-07 12:39:51 -070037 public void write(Kryo kryo, Output output, ClusterMessage message) {
38 kryo.writeClassAndObject(output, message.sender());
39 kryo.writeClassAndObject(output, message.subject());
40 output.writeInt(message.payload().length);
41 output.writeBytes(message.payload());
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070042 }
43
44 @Override
45 public ClusterMessage read(Kryo kryo, Input input,
46 Class<ClusterMessage> type) {
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070047 NodeId sender = (NodeId) kryo.readClassAndObject(input);
48 MessageSubject subject = (MessageSubject) kryo.readClassAndObject(input);
Madan Jampani53e44e62014-10-07 12:39:51 -070049 int payloadSize = input.readInt();
50 byte[] payload = input.readBytes(payloadSize);
Yuta HIGUCHI993d7aa2014-10-06 22:54:38 -070051 return new ClusterMessage(sender, subject, payload);
52 }
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070053}