module miTask import StdDebug, StdMisc from Text import class Text(concat,join,split), instance Text String import iTasks import mTask derive class iTask MTaskMessage Start :: *World -> *World Start world = startEngine ( enterInformation "Port Number?" [] >>= \port->withShared ([], False, [], False) (mTaskTask port) ) world Start world = startEngine mTaskTask world mTaskTask :: Int (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () mTaskTask port ch = syncNetworkChannel "localhost" port "\n" decode encode ch ||- ( sendMsg msgs ch >>= \_-> viewSharedInformation "channels" [ViewWith lens] ch @! () ) >>* [OnAction ActionFinish (always shutDown)] where lens :: ([MTaskMessage],Bool,[MTaskMessage],Bool) -> ([String], [String]) lens (r,_,s,_) = (f r, f s) where f [] = [] f [MTEmpty:xs] = f xs f [x:xs] = [toString x:f xs] msgs | not (trace_tn (fst (toReadableByteCode (unMain bc)))) = undef = toMessages 500 (toRealByteCode (unMain bc)) bc :: Main (ByteCode Int Stmt) bc = sds \x=1 In {main = If (x ==. lit 3) (x =. lit 1) (x =. x +. lit 1) :. pub x} sendMsg :: [MTaskMessage] (Shared ([MTaskMessage],Bool,[MTaskMessage],Bool)) -> Task () sendMsg m ch | not (trace_tn (join "\n" (map (toString o toJSON) m))) = undef = upd (\(r,rs,s,ss)->(r,rs,s ++ m,ss)) ch @! () syncNetworkChannel :: String Int String (String -> m) (n -> String) (Shared ([m],Bool,[n],Bool)) -> Task () | iTask m & iTask n 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,[n],Bool)) -> Task () | iTask m & iTask n //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)