21 lines
642 B
Ruby
21 lines
642 B
Ruby
class ConfirmingSyncPatternState < StatePattern::State
|
|
# Assumes that bytes[0] == Synchronizer::SYNC_PATTERN[0]
|
|
def handle_byte_stream(bytes)
|
|
idx = 0;
|
|
sync_length = Synchronizer::SYNC_PATTERN.length
|
|
|
|
# confirm rest of sync pattern
|
|
while (idx < bytes.length && idx < sync_length && bytes[idx] == Synchronizer::SYNC_PATTERN[idx]) do idx = idx+1 end
|
|
|
|
if (idx == sync_length)
|
|
#p "Sync pattern confirmed"
|
|
transition_to(InSyncState)
|
|
else
|
|
#p "Back to SearchingForSync state. idx = #{idx}."
|
|
transition_to(SearchingForSyncState)
|
|
end
|
|
|
|
# return the rest
|
|
return bytes[idx+1..-1] || ""
|
|
end
|
|
end |