Model for reading created
State pattern implemented
This commit is contained in:
21
app/helpers/ConfirmingSyncPatternState.rb
Normal file
21
app/helpers/ConfirmingSyncPatternState.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
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 < 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
|
||||
Reference in New Issue
Block a user