Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 16 | package org.onosproject.store.serializers.custom; |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.cluster.NodeId; |
| 19 | import org.onosproject.store.cluster.messaging.ClusterMessage; |
| 20 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 21 | import com.esotericsoftware.kryo.Kryo; |
| 22 | import com.esotericsoftware.kryo.Serializer; |
| 23 | import com.esotericsoftware.kryo.io.Input; |
| 24 | import com.esotericsoftware.kryo.io.Output; |
| 25 | |
| 26 | public final class ClusterMessageSerializer extends Serializer<ClusterMessage> { |
| 27 | |
Yuta HIGUCHI | 3e0b651 | 2014-10-07 17:43:56 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Creates a serializer for {@link ClusterMessage}. |
| 30 | */ |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 31 | public ClusterMessageSerializer() { |
| 32 | // does not accept null |
| 33 | super(false); |
| 34 | } |
| 35 | |
| 36 | @Override |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 37 | 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 HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public ClusterMessage read(Kryo kryo, Input input, |
| 46 | Class<ClusterMessage> type) { |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 47 | NodeId sender = (NodeId) kryo.readClassAndObject(input); |
| 48 | MessageSubject subject = (MessageSubject) kryo.readClassAndObject(input); |
Madan Jampani | 53e44e6 | 2014-10-07 12:39:51 -0700 | [diff] [blame] | 49 | int payloadSize = input.readInt(); |
| 50 | byte[] payload = input.readBytes(payloadSize); |
Yuta HIGUCHI | 993d7aa | 2014-10-06 22:54:38 -0700 | [diff] [blame] | 51 | return new ClusterMessage(sender, subject, payload); |
| 52 | } |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 53 | } |