Model for reading created
State pattern implemented
This commit is contained in:
5
app/helpers/ConfirmingSyncLossState.rb
Normal file
5
app/helpers/ConfirmingSyncLossState.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class ConfirmingSyncLossState < StatePattern::State
|
||||
def handle_byte_stream(bytes)
|
||||
p "Please override"
|
||||
end
|
||||
end
|
||||
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
|
||||
18
app/helpers/InSyncState.rb
Normal file
18
app/helpers/InSyncState.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
class InSyncState < StatePattern::State
|
||||
|
||||
END_OF_FRAME = "!\n"
|
||||
|
||||
def handle_byte_stream(bytes)
|
||||
idx = 0
|
||||
frame = ""
|
||||
while (idx < bytes.length && bytes[idx] != END_OF_FRAME[0]) do
|
||||
frame = frame + bytes[idx]
|
||||
idx = idx +1
|
||||
end
|
||||
p "------ FRAME -----"
|
||||
frame_lines = frame.split("\n")
|
||||
p frame_lines # should call to higher level
|
||||
p "##################"
|
||||
return ""
|
||||
end
|
||||
end
|
||||
13
app/helpers/SearchingForSyncState.rb
Normal file
13
app/helpers/SearchingForSyncState.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class SearchingForSyncState < StatePattern::State
|
||||
def handle_byte_stream(bytes)
|
||||
idx = 0;
|
||||
# spool unwanted bytes
|
||||
while (bytes[idx] != Synchronizer::SYNC_PATTERN[0]) do idx = idx+1 end
|
||||
|
||||
#p "Found pattern at idx = #{idx}"
|
||||
transition_to(ConfirmingSyncPatternState)
|
||||
|
||||
# return
|
||||
return bytes[idx..-1]
|
||||
end
|
||||
end
|
||||
8
app/helpers/Synchronizer.rb
Normal file
8
app/helpers/Synchronizer.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Synchronizer
|
||||
include StatePattern
|
||||
|
||||
SYNC_PATTERN = "\n/ISk5\\2ME382-1003\n\n"
|
||||
|
||||
set_initial_state SearchingForSyncState
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user