Last active
December 28, 2015 14:49
-
-
Save ZacharyKamerling/7517011 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Random as R | |
main = (\(_,_,stuff,msgs) -> flow down (asText stuff :: map plainText msgs)) <~ | |
foldp stepFSM | |
(("Waiting",0),("Waiting",0),(0,0),[]) | |
(R.floatList ((fps 1) `sampleOn` constant 3)) | |
stepFSM rnds ((stateA,seqA),(stateB,seqB),(recA,recB),messages) = | |
let rs = map (\n -> floor (n*10.0)) rnds | |
ra = head rs | |
rb = head <| drop 1 rs | |
rc = head <| drop 2 rs | |
msgA = host "'A'" ra (stateA,seqA) recA | |
msgB = host "'B'" rb (stateB,seqB) recB | |
recA' = if ra == 0 && seqA > recA then recA + 1 else recA | |
recB' = if rb == 0 && seqB > recB then recB + 1 else recB | |
msgs = msgA ++ msgB ++ messages | |
stateC = if recA <= recB then "Routing A" else "Routing B" in | |
case stateC of | |
"Routing A" -> case rc of | |
0 -> (("Sending",recA+1),(stateB,seqB),(recA',recB'), | |
("'A' received ACK from 'C' for packet #" ++ show (recA+1)) :: msgs) | |
1 -> ((stateA,seqA),(stateB,seqB),(recA',recB'), | |
"An ACK going from 'C' to 'A' never made it or was corrupted." :: msgs) | |
_ -> ((stateA,seqA),(stateB,seqB),(recA',recB'),msgs) | |
"Routing B" -> case rc of | |
0 -> ((stateA,seqA),("Sending",recB+1),(recA',recB'), | |
("'B' received ACK from 'C' for packet #" ++ show (recB+1)) :: msgs) | |
1 -> ((stateA,seqA),(stateB,seqB),(recA',recB'), | |
"An ACK going from 'C' to 'B' never made it or was corrupted." :: msgs) | |
_ -> ((stateA,seqA),(stateB,seqB),(recA',recB'),msgs) | |
host name r (state,seq) rec = case state of | |
"Sending" -> case r of | |
0 -> if rec >= seq | |
then [name ++ " successfully sent packet " ++ show seq ++ ". 'C' already has it and will ignore it."] | |
else [name ++ " successfully sent packet " ++ show seq ++ "."] | |
1 -> ["A packet going from" ++ name ++ " to 'C' never made it or was corrupted."] | |
_ -> [] | |
"Waiting" -> [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment