Model for reading created

State pattern implemented
This commit is contained in:
Aart van Halteren
2013-06-26 15:05:20 +02:00
parent e6fc705e6a
commit 58eee12fb5
13 changed files with 236 additions and 18 deletions

View 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