oi-0.0.5: sample/talk.hs
{-# LANGUAGE TypeOperators
#-}
module Main where
import Data.OI
import Control.Parallel
import System.Environment
main :: IO ()
main = do { kbd1:scr1:kbd2:scr2:_ <- getArgs
; run $ pmain (kbd1,scr1) (kbd2,scr2)
}
pmain :: (FilePath,FilePath)
-> (FilePath,FilePath)
-> ((String, [String], ()), (String, [String], ()))
:-> ()
pmain (kbd1,scr1) (kbd2,scr2)
= uncurry par . (talk "Alice" (kbd1,scr1) |><| talk "Bob" (kbd2,scr2))
talk :: String -- Talker's name
-> (FilePath,FilePath) -- (Talker's keybord, Talker's screen)
-> [String] -- Messages from the other end
-> OI (String,[String],()) -- (Talker's input,Merged messages, Process result)
-> ([String],()) -- (Messages to the other end, Process result)
talk name (kbd,scr) msg r = (ins,showscr scr (mergeOI ins msg os) us)
where
(is,os,us) = deTriple r
ins = map ((name ++ ": ")++) $ lines $ readkbd kbd is
readkbd :: FilePath -> String :-> String
readkbd = iooi . readFile
showscr :: FilePath -> [String] -> () :-> ()
showscr s = iooi . writeFile s . unlines