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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.serialization.JavaSerializer;
14 import akka.serialization.Serialization;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.io.Externalizable;
17 import java.io.IOException;
18 import java.io.ObjectInput;
19 import java.io.ObjectOutput;
20 import java.io.Serializable;
21 import org.opendaylight.yangtools.concepts.Identifier;
24 * Represents a sliced message chunk.
26 * @author Thomas Pantelis
28 public class MessageSlice implements Serializable {
29 private static final long serialVersionUID = 1L;
31 private final Identifier identifier;
32 private final byte[] data;
33 private final int sliceIndex;
34 private final int totalSlices;
35 private final int lastSliceHashCode;
36 private final ActorRef replyTo;
38 MessageSlice(final Identifier identifier, final byte[] data, final int sliceIndex, final int totalSlices,
39 final int lastSliceHashCode, final ActorRef replyTo) {
40 this.identifier = requireNonNull(identifier);
41 this.data = requireNonNull(data);
42 this.sliceIndex = sliceIndex;
43 this.totalSlices = totalSlices;
44 this.lastSliceHashCode = lastSliceHashCode;
45 this.replyTo = requireNonNull(replyTo);
48 public Identifier getIdentifier() {
52 @SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "Exposes a mutable object stored in a field but "
53 + "this is OK since this class is merely a DTO and does not process the byte[] internally."
54 + "Also it would be inefficient to create a return copy as the byte[] could be large.")
55 public byte[] getData() {
59 public int getSliceIndex() {
63 public int getTotalSlices() {
67 public int getLastSliceHashCode() {
68 return lastSliceHashCode;
71 public ActorRef getReplyTo() {
76 public String toString() {
77 return "MessageSlice [identifier=" + identifier + ", data.length=" + data.length + ", sliceIndex="
78 + sliceIndex + ", totalSlices=" + totalSlices + ", lastSliceHashCode=" + lastSliceHashCode
79 + ", replyTo=" + replyTo + "]";
82 private Object writeReplace() {
83 return new Proxy(this);
86 private static class Proxy implements Externalizable {
87 private static final long serialVersionUID = 1L;
89 private MessageSlice messageSlice;
91 // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
92 // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
93 @SuppressWarnings("checkstyle:RedundantModifier")
97 Proxy(final MessageSlice messageSlice) {
98 this.messageSlice = messageSlice;
102 public void writeExternal(final ObjectOutput out) throws IOException {
103 out.writeObject(messageSlice.identifier);
104 out.writeInt(messageSlice.sliceIndex);
105 out.writeInt(messageSlice.totalSlices);
106 out.writeInt(messageSlice.lastSliceHashCode);
107 out.writeObject(messageSlice.data);
108 out.writeObject(Serialization.serializedActorPath(messageSlice.replyTo));
112 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
113 Identifier identifier = (Identifier) in.readObject();
114 int sliceIndex = in.readInt();
115 int totalSlices = in.readInt();
116 int lastSliceHashCode = in.readInt();
117 byte[] data = (byte[])in.readObject();
118 ActorRef replyTo = JavaSerializer.currentSystem().value().provider()
119 .resolveActorRef((String) in.readObject());
121 messageSlice = new MessageSlice(identifier, data, sliceIndex, totalSlices, lastSliceHashCode, replyTo);
124 private Object readResolve() {