3 import StdDebug, StdMisc
5 from Text import class Text(concat,join,split), instance Text String
10 Start :: *World -> *World
11 Start world = startEngine (withShared ([], False, [], False) mTaskTask) world
12 //Start world = startEngine mTaskTask world
14 mTaskTask :: (Shared ([String],Bool,[String],Bool)) -> Task ()
16 syncNetworkChannel "localhost" 8124 "\n" id id ch ||-
17 viewSharedInformation "channels" [ViewWith lens] ch ||-
18 sendString (makemTask 500 bc) ch @! ()
20 lens :: ([String],Bool,[String],Bool) -> String
21 lens (r,_,s,_) = "channels"
23 bc :: Main (ByteCode Int Expr)
24 bc = sds \x=0 In {main = x =. x +. lit 1}
26 makemTask :: Int (Main (ByteCode a Expr)) -> String
28 # (bc, st) = toRealByteCode (unMain bc)
29 = "t" +++ toString (toChar (to / 265))
30 +++ toString (toChar (to rem 265)) +++ toString bc +++ "\n"
32 sendString :: String (Shared ([String],Bool,[String],Bool)) -> Task ()
33 sendString m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ [m],ss)) ch @! ()
35 syncNetworkChannel :: String Int String (String -> m) (m -> String) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m
36 syncNetworkChannel server port msgSeparator decodeFun encodeFun channel
37 = tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! ()
39 onConnect _ (received,receiveStopped,send,sendStopped)
40 = (Ok "",if (not (isEmpty send)) (Just (received,False,[],sendStopped)) Nothing, map encodeFun send,False)
41 whileConnected Nothing acc (received,receiveStopped,send,sendStopped)
42 = (Ok acc, Nothing, [], False)
43 whileConnected (Just newData) acc (received,receiveStopped,send,sendStopped)
44 # [acc:msgs] = reverse (split msgSeparator (concat [acc,newData]))
45 # write = if (not (isEmpty msgs && isEmpty send))
46 (Just (received ++ map decodeFun (reverse msgs),receiveStopped,[],sendStopped))
48 = (Ok acc,write,map encodeFun send,False)
50 onDisconnect l (received,receiveStopped,send,sendStopped)
51 = (Ok l,Just (received,True,send,sendStopped))
53 consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m
54 consumeNetworkStream processTask channel
55 = ((watch channel >>* [OnValue (ifValue ifProcess process)]) <! id) @! ()
57 ifProcess (received,receiveStopped,_,_)
58 = receiveStopped || (not (isEmpty received))
60 process (received,receiveStopped,_,_)
62 >>| if (isEmpty received) (return ()) (processTask received)
65 empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool)
66 empty (_,rs,s,ss) = ([],rs,s,ss)