module miTask import StdDebug, StdMisc from Text import class Text(concat,join,split), instance Text String import iTasks import mTask Start :: *World -> *World Start world = startEngine (withShared ([], False, [], False) mTaskTask) world //Start world = startEngine mTaskTask world mTaskTask :: (Shared ([String],Bool,[String],Bool)) -> Task () mTaskTask ch = syncNetworkChannel "localhost" 8124 "\n" id id ch ||- viewSharedInformation "channels" [ViewWith lens] ch ||- sendString (makemTask 500 bc) ch @! () where lens :: ([String],Bool,[String],Bool) -> String lens (r,_,s,_) = "channels" bc :: Main (ByteCode Int Expr) bc = sds \x=0 In {main = x =. x +. lit 1} makemTask :: Int (Main (ByteCode a Expr)) -> String makemTask to bc # (bc, st) = toRealByteCode (unMain bc) = "t" +++ toString (toChar (to / 265)) +++ toString (toChar (to rem 265)) +++ toString bc +++ "\n" sendString :: String (Shared ([String],Bool,[String],Bool)) -> Task () sendString m ch = upd (\(r,rs,s,ss)->(r,rs,s ++ [m],ss)) ch @! () syncNetworkChannel :: String Int String (String -> m) (m -> String) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m syncNetworkChannel server port msgSeparator decodeFun encodeFun channel = tcpconnect server port channel {ConnectionHandlers|onConnect=onConnect,whileConnected=whileConnected,onDisconnect=onDisconnect} @! () where onConnect _ (received,receiveStopped,send,sendStopped) = (Ok "",if (not (isEmpty send)) (Just (received,False,[],sendStopped)) Nothing, map encodeFun send,False) whileConnected Nothing acc (received,receiveStopped,send,sendStopped) = (Ok acc, Nothing, [], False) whileConnected (Just newData) acc (received,receiveStopped,send,sendStopped) # [acc:msgs] = reverse (split msgSeparator (concat [acc,newData])) # write = if (not (isEmpty msgs && isEmpty send)) (Just (received ++ map decodeFun (reverse msgs),receiveStopped,[],sendStopped)) Nothing = (Ok acc,write,map encodeFun send,False) onDisconnect l (received,receiveStopped,send,sendStopped) = (Ok l,Just (received,True,send,sendStopped)) consumeNetworkStream :: ([m] -> Task ()) (Shared ([m],Bool,[m],Bool)) -> Task () | iTask m consumeNetworkStream processTask channel = ((watch channel >>* [OnValue (ifValue ifProcess process)]) >| if (isEmpty received) (return ()) (processTask received) @! receiveStopped empty :: ([m],Bool,[m],Bool) -> ([m],Bool,[m],Bool) empty (_,rs,s,ss) = ([],rs,s,ss)