Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
cfrederrrr committed Aug 29, 2019
1 parent 2761b2f commit 920779e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
24 changes: 17 additions & 7 deletions src/raft/log.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
class Raft::Log
@entries : Array(Raft::Log::Entry)

def self.from_io(io : IO, fm : IO::ByteFormat)
entries = [] of Raft::Log::Entry
new entries
end

def to_io(io : IO, fm : IO::ByteFormat)
@entries.each do |entry|
entry.to_io(io, fm)
Expand All @@ -14,10 +9,25 @@ class Raft::Log

def initialize(@entries : Array(Raft::Log::Entry))
end

def self.from_io(io : IO::Buffered, fm : IO::ByteFormat = IO::ByteFormat::NetworkEndian)
entries = [] of Raft::Log::Entry
while io.peek.any?
typeid = io.read_bytes(Int32, fm)
{% begin %}
case typeid
{% for t in Raft::Log::Entry.all_subclasses %}
when {{t}}::TYPEID then entries.push {{t.id}}.from_io(io, fm)
{% end %}
end
{% end %}
end

new entries
end
end

abstract class Raft::Log::Entry
abstract def to_io(io : IO, fm : IO::ByteFormat) : IO
abstract def to_io(io : IO, fm : IO::ByteFormat)
abstract def from_io(io : IO, fm : IO::ByteFormat)
abstract def typeid : UInt32
end
8 changes: 4 additions & 4 deletions src/raft/rpc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ module Raft::RPC
typeid = io.read_bytes(Int16, fm)
case typeid
when Raft::RPC::AppendEntries::TYPEID
Raft::RPC::AppendEntries.new(io, fm)
Raft::RPC::AppendEntries.from_io(io, fm)
when Raft::RPC::AppendEntries::Result::TYPEID
Raft::RPC::AppendEntries::Result.new(io, fm)
Raft::RPC::AppendEntries::Result.from_io(io, fm)
when Raft::RPC::RequestVote::TYPEID
Raft::RPC::RequestVote.new(io, fm)
Raft::RPC::RequestVote.from_io(io, fm)
when Raft::RPC::RequestVote::Result::TYPEID
Raft::RPC::RequestVote::Result.new(io, fm)
Raft::RPC::RequestVote::Result.from_io(io, fm)
else
raise "[#{io.remote_address}] - invalid typeid '#{typeid}'"
end
Expand Down
7 changes: 3 additions & 4 deletions src/raft/rpc/append-entries.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ struct Raft::RPC::AppendEntries < Raft::RPC::Packet
count = 0
entries = [] of Raft::Log::Entry
while count < size

entries.push Raft::Log::Entry.from_io(io, fm)
io.read_bytes(UInt8, io)
end

new term, leader_id, prev_log_idx, prev_log_term, leader_commit, entries
Expand Down Expand Up @@ -54,7 +55,6 @@ struct Raft::RPC::AppendEntries < Raft::RPC::Packet
entry.to_io(io, fm)
RS.to_io(io, fm)
end
EOT.to_io(io, fm)
end
end

Expand Down Expand Up @@ -92,10 +92,9 @@ struct Raft::RPC::AppendEntries::Result < Raft::RPC::Packet
# +--------------------------------+
# ```
def to_io(io : IO, fm : IO::ByteFM = FM)
::Raft::Version.to_io(io, fm)
Raft::Version.to_io(io, fm)
TYPEID.to_io(io, fm)
@term.to_io(io, fm)
@success ? ACK.to_io(io, fm) : NAK.to_io(io, fm)
EOT.to_io(io, fm)
end
end
4 changes: 2 additions & 2 deletions src/raft/version.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct Raft::Version
# Converts the current version to an unsigned integer
# to simplify comparison
def self.to_u
version = 0_u + MAJOR
version = 0_u32 + MAJOR
version <<= 8
version += MINOR
version <<= 8
Expand All @@ -76,7 +76,7 @@ struct Raft::Version
# Converts the version of this instance to an unsigned integer
# to simplify comparison
def to_u
version = 0_u + @major
version = 0_u32 + @major
version <<= 8
version += @minor
version <<= 8
Expand Down

0 comments on commit 920779e

Please sign in to comment.