+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeShort(RaftVersions.CURRENT_VERSION);
+ out.writeLong(getTerm());
+ out.writeUTF(leaderId);
+ out.writeLong(lastIncludedIndex);
+ out.writeLong(lastIncludedTerm);
+ out.writeInt(chunkIndex);
+ out.writeInt(totalChunks);
+
+ out.writeByte(lastChunkHashCode.isPresent() ? 1 : 0);
+ if(lastChunkHashCode.isPresent()) {
+ out.writeInt(lastChunkHashCode.get().intValue());
+ }
+
+ out.writeByte(serverConfig.isPresent() ? 1 : 0);
+ if(serverConfig.isPresent()) {
+ out.writeObject(serverConfig.get());
+ }
+
+ out.writeObject(data);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ in.readShort(); // raft version - not currently used
+ setTerm(in.readLong());
+ leaderId = in.readUTF();
+ lastIncludedIndex = in.readLong();
+ lastIncludedTerm = in.readLong();
+ chunkIndex = in.readInt();
+ totalChunks = in.readInt();
+
+ lastChunkHashCode = Optional.absent();
+ boolean chunkHashCodePresent = in.readByte() == 1;
+ if(chunkHashCodePresent) {
+ lastChunkHashCode = Optional.of(in.readInt());
+ }
+
+ serverConfig = Optional.absent();
+ boolean serverConfigPresent = in.readByte() == 1;
+ if(serverConfigPresent) {
+ serverConfig = Optional.of((ServerConfigurationPayload)in.readObject());
+ }
+
+ data = (byte[])in.readObject();
+ }
+
+ public <T extends Object> Object toSerializable(short version) {
+ if(version >= RaftVersions.BORON_VERSION) {
+ return this;
+ } else {
+ InstallSnapshotMessages.InstallSnapshot.Builder builder = InstallSnapshotMessages.InstallSnapshot.newBuilder()
+ .setTerm(this.getTerm())
+ .setLeaderId(this.getLeaderId())
+ .setChunkIndex(this.getChunkIndex())
+ .setData(ByteString.copyFrom(getData()))
+ .setLastIncludedIndex(this.getLastIncludedIndex())
+ .setLastIncludedTerm(this.getLastIncludedTerm())
+ .setTotalChunks(this.getTotalChunks());
+
+ if(lastChunkHashCode.isPresent()){
+ builder.setLastChunkHashCode(lastChunkHashCode.get());
+ }
+ return builder.build();
+ }
+ }