This repository has been archived by the owner on Sep 6, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from xiangli-cmu/refactor_proto
WIP Refactor proto
- Loading branch information
Showing
32 changed files
with
4,971 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,73 @@ | ||
package raft | ||
|
||
import ( | ||
"code.google.com/p/goprotobuf/proto" | ||
"github.com/goraft/raft/protobuf" | ||
"io" | ||
"io/ioutil" | ||
|
||
"code.google.com/p/gogoprotobuf/proto" | ||
"github.com/goraft/raft/protobuf" | ||
) | ||
|
||
// The response returned from a server appending entries to the log. | ||
type AppendEntriesResponse struct { | ||
Term uint64 | ||
// the current index of the server | ||
Index uint64 | ||
Success bool | ||
CommitIndex uint64 | ||
peer string | ||
append bool | ||
pb *protobuf.AppendEntriesResponse | ||
peer string | ||
append bool | ||
} | ||
|
||
// Creates a new AppendEntries response. | ||
func newAppendEntriesResponse(term uint64, success bool, index uint64, commitIndex uint64) *AppendEntriesResponse { | ||
pb := &protobuf.AppendEntriesResponse{ | ||
Term: proto.Uint64(term), | ||
Index: proto.Uint64(index), | ||
Success: proto.Bool(success), | ||
CommitIndex: proto.Uint64(commitIndex), | ||
} | ||
|
||
return &AppendEntriesResponse{ | ||
Term: term, | ||
Success: success, | ||
Index: index, | ||
CommitIndex: commitIndex, | ||
pb: pb, | ||
} | ||
} | ||
|
||
func (aer *AppendEntriesResponse) Index() uint64 { | ||
return aer.pb.GetIndex() | ||
} | ||
|
||
func (aer *AppendEntriesResponse) CommitIndex() uint64 { | ||
return aer.pb.GetCommitIndex() | ||
} | ||
|
||
func (aer *AppendEntriesResponse) Term() uint64 { | ||
return aer.pb.GetTerm() | ||
} | ||
|
||
func (aer *AppendEntriesResponse) Success() bool { | ||
return aer.pb.GetSuccess() | ||
} | ||
|
||
// Encodes the AppendEntriesResponse to a buffer. Returns the number of bytes | ||
// written and any error that may have occurred. | ||
func (resp *AppendEntriesResponse) Encode(w io.Writer) (int, error) { | ||
pb := &protobuf.ProtoAppendEntriesResponse{ | ||
Term: proto.Uint64(resp.Term), | ||
Index: proto.Uint64(resp.Index), | ||
CommitIndex: proto.Uint64(resp.CommitIndex), | ||
Success: proto.Bool(resp.Success), | ||
} | ||
p, err := proto.Marshal(pb) | ||
b, err := proto.Marshal(resp.pb) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
return w.Write(p) | ||
return w.Write(b) | ||
} | ||
|
||
// Decodes the AppendEntriesResponse from a buffer. Returns the number of bytes read and | ||
// any error that occurs. | ||
func (resp *AppendEntriesResponse) Decode(r io.Reader) (int, error) { | ||
data, err := ioutil.ReadAll(r) | ||
|
||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
totalBytes := len(data) | ||
|
||
pb := &protobuf.ProtoAppendEntriesResponse{} | ||
if err := proto.Unmarshal(data, pb); err != nil { | ||
resp.pb = new(protobuf.AppendEntriesResponse) | ||
if err := proto.Unmarshal(data, resp.pb); err != nil { | ||
return -1, err | ||
} | ||
|
||
resp.Term = pb.GetTerm() | ||
resp.Index = pb.GetIndex() | ||
resp.CommitIndex = pb.GetCommitIndex() | ||
resp.Success = pb.GetSuccess() | ||
|
||
return totalBytes, nil | ||
return len(data), nil | ||
} |
Oops, something went wrong.