37 lines
1.0 KiB
Ruby
37 lines
1.0 KiB
Ruby
class InSyncState < StatePattern::State
|
|
|
|
END_OF_FRAME = "!\n"
|
|
|
|
def handle_byte_stream(bytes)
|
|
idx = 0
|
|
#sync_pattern_length = Synchronizer::SYNC_PATTERN.length
|
|
sync_pattern_length = END_OF_FRAME.length
|
|
#p "Sync length: #{sync_length}"
|
|
|
|
frame = ""
|
|
|
|
while (idx+sync_pattern_length < bytes.length && !new_frame_starts(bytes,idx,sync_pattern_length)) do
|
|
frame = frame + bytes[idx]
|
|
idx = idx +1
|
|
|
|
end
|
|
|
|
# did we reach the end of the frame?
|
|
if new_frame_starts(bytes,idx,sync_pattern_length)
|
|
p "------ FRAME -----"
|
|
frame_lines = frame.split("\n")
|
|
p frame_lines # should call to higher level
|
|
p "##################"
|
|
return bytes[idx+sync_pattern_length..-1]
|
|
else
|
|
return nil
|
|
end
|
|
|
|
end
|
|
|
|
private
|
|
def new_frame_starts(bytes,idx,sync_pattern_length)
|
|
#return bytes[idx..idx+sync_pattern_length-1].eql?(Synchronizer::SYNC_PATTERN)
|
|
return bytes[idx..idx+sync_pattern_length-1].eql?(END_OF_FRAME)
|
|
end
|
|
end |