5ed18f918a0d7ba87c7cf75e6567ee1548711c99
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / main / java / org / opendaylight / controller / cluster / raft / messages / RequestVote.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.raft.messages;
10
11 import java.io.Externalizable;
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15
16 /**
17  * Invoked by candidates to gather votes (§5.2).
18  */
19 public class RequestVote extends AbstractRaftRPC {
20     private static final long serialVersionUID = -6967509186297108657L;
21
22     // candidate requesting vote
23     private final String candidateId;
24
25     // index of candidate’s last log entry (§5.4)
26     private final long lastLogIndex;
27
28     // term of candidate’s last log entry (§5.4)
29     private final long lastLogTerm;
30
31     public RequestVote(long term, String candidateId, long lastLogIndex, long lastLogTerm) {
32         super(term);
33         this.candidateId = candidateId;
34         this.lastLogIndex = lastLogIndex;
35         this.lastLogTerm = lastLogTerm;
36     }
37
38     public String getCandidateId() {
39         return candidateId;
40     }
41
42     public long getLastLogIndex() {
43         return lastLogIndex;
44     }
45
46     public long getLastLogTerm() {
47         return lastLogTerm;
48     }
49
50     @Override
51     public String toString() {
52         StringBuilder builder = new StringBuilder();
53         builder.append("RequestVote [term=").append(getTerm()).append(", candidateId=").append(candidateId)
54                 .append(", lastLogIndex=").append(lastLogIndex).append(", lastLogTerm=").append(lastLogTerm)
55                 .append("]");
56         return builder.toString();
57     }
58
59     private Object writeReplace() {
60         return new Proxy(this);
61     }
62
63     private static class Proxy implements Externalizable {
64         private static final long serialVersionUID = 1L;
65
66         private RequestVote requestVote;
67
68         // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
69         // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
70         @SuppressWarnings("checkstyle:RedundantModifier")
71         public Proxy() {
72         }
73
74         Proxy(RequestVote requestVote) {
75             this.requestVote = requestVote;
76         }
77
78         @Override
79         public void writeExternal(ObjectOutput out) throws IOException {
80             out.writeLong(requestVote.getTerm());
81             out.writeObject(requestVote.candidateId);
82             out.writeLong(requestVote.lastLogIndex);
83             out.writeLong(requestVote.lastLogTerm);
84         }
85
86         @Override
87         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
88             long term = in.readLong();
89             String candidateId = (String) in.readObject();
90             long lastLogIndex = in.readLong();
91             long lastLogTerm = in.readLong();
92
93             requestVote = new RequestVote(term, candidateId, lastLogIndex, lastLogTerm);
94         }
95
96         private Object readResolve() {
97             return requestVote;
98         }
99     }
100 }