package org.opendaylight.controller.cluster.raft.messages;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
/**
* Invoked by candidates to gather votes (ยง5.2).
*/
.append("]");
return builder.toString();
}
+
+ private Object writeReplace() {
+ return new Proxy(this);
+ }
+
+ private static class Proxy implements Externalizable {
+ private static final long serialVersionUID = 1L;
+
+ private RequestVote requestVote;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ }
+
+ Proxy(RequestVote requestVote) {
+ this.requestVote = requestVote;
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeLong(requestVote.getTerm());
+ out.writeObject(requestVote.candidateId);
+ out.writeLong(requestVote.lastLogIndex);
+ out.writeLong(requestVote.lastLogTerm);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ long term = in.readLong();
+ String candidateId = (String) in.readObject();
+ long lastLogIndex = in.readLong();
+ long lastLogTerm = in.readLong();
+
+ requestVote = new RequestVote(term, candidateId, lastLogIndex, lastLogTerm);
+ }
+
+ private Object readResolve() {
+ return requestVote;
+ }
+ }
}
package org.opendaylight.controller.cluster.raft.messages;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
public final class RequestVoteReply extends AbstractRaftRPC {
private static final long serialVersionUID = 8427899326488775660L;
public String toString() {
return "RequestVoteReply [term=" + getTerm() + ", voteGranted=" + voteGranted + "]";
}
+
+ private Object writeReplace() {
+ return new Proxy(this);
+ }
+
+ private static class Proxy implements Externalizable {
+ private static final long serialVersionUID = 1L;
+
+ private RequestVoteReply requestVoteReply;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ }
+
+ Proxy(RequestVoteReply requestVoteReply) {
+ this.requestVoteReply = requestVoteReply;
+ }
+
+ @Override
+ public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeLong(requestVoteReply.getTerm());
+ out.writeBoolean(requestVoteReply.voteGranted);
+ }
+
+ @Override
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ long term = in.readLong();
+ boolean voteGranted = in.readBoolean();
+
+ requestVoteReply = new RequestVoteReply(term, voteGranted);
+ }
+
+ private Object readResolve() {
+ return requestVoteReply;
+ }
+ }
}
--- /dev/null
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.messages;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for RequestVoteReply.
+ *
+ * @author Thomas Pantelis
+ */
+public class RequestVoteReplyTest {
+
+ @Test
+ public void testSerialization() {
+ RequestVoteReply expected = new RequestVoteReply(5, true);
+ RequestVoteReply cloned = (RequestVoteReply) SerializationUtils.clone(expected);
+
+ assertEquals("getTerm", expected.getTerm(), cloned.getTerm());
+ assertEquals("isVoteGranted", expected.isVoteGranted(), cloned.isVoteGranted());
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.cluster.raft.messages;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.commons.lang.SerializationUtils;
+import org.junit.Test;
+
+/**
+ * Unit tests for RequestVote.
+ *
+ * @author Thomas Pantelis
+ */
+public class RequestVoteTest {
+
+ @Test
+ public void testSerialization() {
+ RequestVote expected = new RequestVote(4, "candidateId", 3, 2);
+ RequestVote cloned = (RequestVote) SerializationUtils.clone(expected);
+
+ assertEquals("getTerm", expected.getTerm(), cloned.getTerm());
+ assertEquals("getCandidateId", expected.getCandidateId(), cloned.getCandidateId());
+ assertEquals("getLastLogIndex", expected.getLastLogIndex(), cloned.getLastLogIndex());
+ assertEquals("getLastLogTerm", expected.getLastLogTerm(), cloned.getLastLogTerm());
+ }
+}