2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.messaging;
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import com.google.common.base.Preconditions;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.io.Externalizable;
16 import java.io.IOException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.io.Serializable;
20 import org.opendaylight.yangtools.concepts.Identifier;
23 * Represents a sliced message chunk.
25 * @author Thomas Pantelis
27 public class MessageSlice implements Serializable {
28 private static final long serialVersionUID = 1L;
30 private final Identifier identifier;
31 private final byte[] data;
32 private final int sliceIndex;
33 private final int totalSlices;
34 private final int lastSliceHashCode;
35 private final ActorRef replyTo;
37 MessageSlice(Identifier identifier, byte[] data, int sliceIndex, int totalSlices, int lastSliceHashCode,
38 final ActorRef replyTo) {
39 this.identifier = Preconditions.checkNotNull(identifier);
40 this.data = Preconditions.checkNotNull(data);
41 this.sliceIndex = sliceIndex;
42 this.totalSlices = totalSlices;
43 this.lastSliceHashCode = lastSliceHashCode;
44 this.replyTo = Preconditions.checkNotNull(replyTo);
47 public Identifier getIdentifier() {
51 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
52 + "this is OK since this class is merely a DTO and does not process the byte[] internally."
53 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
54 public byte[] getData() {
58 public int getSliceIndex() {
62 public int getTotalSlices() {
66 public int getLastSliceHashCode() {
67 return lastSliceHashCode;
70 public ActorRef getReplyTo() {
75 public String toString() {
76 return "MessageSlice [identifier=" + identifier + ", data.length=" + data.length + ", sliceIndex="
77 + sliceIndex + ", totalSlices=" + totalSlices + ", lastSliceHashCode=" + lastSliceHashCode
78 + ", replyTo=" + replyTo + "]";
81 private Object writeReplace() {
82 return new Proxy(this);
85 private static class Proxy implements Externalizable {
86 private static final long serialVersionUID = 1L;
88 private MessageSlice messageSlice;
90 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
91 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
92 @SuppressWarnings("checkstyle:RedundantModifier")
96 Proxy(MessageSlice messageSlice) {
97 this.messageSlice = messageSlice;
101 public void writeExternal(ObjectOutput out) throws IOException {
102 out.writeObject(messageSlice.identifier);
103 out.writeInt(messageSlice.sliceIndex);
104 out.writeInt(messageSlice.totalSlices);
105 out.writeInt(messageSlice.lastSliceHashCode);
106 out.writeObject(messageSlice.data);
107 out.writeObject(Serialization.serializedActorPath(messageSlice.replyTo));
111 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
112 Identifier identifier = (Identifier) in.readObject();
113 int sliceIndex = in.readInt();
114 int totalSlices = in.readInt();
115 int lastSliceHashCode = in.readInt();
116 byte[] data = (byte[])in.readObject();
117 ActorRef replyTo = JavaSerializer.currentSystem().value().provider()
118 .resolveActorRef((String) in.readObject());
120 messageSlice = new MessageSlice(identifier, data, sliceIndex, totalSlices, lastSliceHashCode, replyTo);
123 private Object readResolve() {