b23c76d06f9213d516f0f2a7345f85034c157333
[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 package org.opendaylight.controller.cluster.raft.messages;
9
10 import java.io.Externalizable;
11 import java.io.IOException;
12 import java.io.ObjectInput;
13 import java.io.ObjectOutput;
14
15 /**
16  * Invoked by candidates to gather votes (§5.2).
17  */
18 public final class RequestVote extends AbstractRaftRPC {
19     private static final long serialVersionUID = -6967509186297108657L;
20
21     // candidate requesting vote
22     private final String candidateId;
23
24     // index of candidate’s last log entry (§5.4)
25     private final long lastLogIndex;
26
27     // term of candidate’s last log entry (§5.4)
28     private final long lastLogTerm;
29
30     public RequestVote(final long term, final String candidateId, final long lastLogIndex, final long lastLogTerm) {
31         super(term);
32         this.candidateId = candidateId;
33         this.lastLogIndex = lastLogIndex;
34         this.lastLogTerm = lastLogTerm;
35     }
36
37     public String getCandidateId() {
38         return candidateId;
39     }
40
41     public long getLastLogIndex() {
42         return lastLogIndex;
43     }
44
45     public long getLastLogTerm() {
46         return lastLogTerm;
47     }
48
49     @Override
50     public String toString() {
51         return "RequestVote [term=" + getTerm()
52                 + ", candidateId=" + candidateId
53                 + ", lastLogIndex=" + lastLogIndex
54                 + ", lastLogTerm=" + lastLogTerm
55                 + "]";
56     }
57
58     @Override
59     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(final RequestVote requestVote) {
75             this.requestVote = requestVote;
76         }
77
78         @Override
79         public void writeExternal(final 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(final 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 }