Have each facility's init function return a Facility structure.
Chris Pressey
5 years ago
0 | 0 |
module Language.Robin.Facilities.LineTerminal where
|
1 | 1 |
|
|
2 |
import Control.Concurrent (forkIO, myThreadId)
|
|
3 |
import Control.Concurrent.Chan
|
2 | 4 |
import qualified Data.Char as Char
|
3 | 5 |
import System.IO
|
4 | 6 |
|
|
6 | 8 |
import Language.Robin.Facilities
|
7 | 9 |
|
8 | 10 |
|
9 | |
import Control.Concurrent (forkIO, myThreadId)
|
10 | |
import Control.Concurrent.Chan
|
|
11 |
init :: IO Facility
|
|
12 |
init = do
|
|
13 |
--FIXME causes tests to fail!
|
|
14 |
--chan <- newChan
|
|
15 |
--threadId <- forkIO $ produceEvents chan
|
|
16 |
return Facility{ threadId=Nothing {-Just threadId-}, handler=handleEvent, waiter=waitForEvent }
|
11 | 17 |
|
12 | 18 |
|
13 | |
-- sketch
|
14 | |
init :: IO FacilityHandler
|
15 | |
init = do
|
16 | |
chan <- newChan
|
17 | |
threadId <- forkIO $ produceEvents chan
|
18 | |
return handleEvent
|
19 | |
|
20 | |
|
21 | |
-- sketch
|
22 | 19 |
produceEvents :: Chan Event -> IO ()
|
23 | 20 |
produceEvents chan = do
|
24 | 21 |
event <- waitForEvent
|
5 | 5 |
import Language.Robin.Facilities
|
6 | 6 |
|
7 | 7 |
|
|
8 |
init :: IO Facility
|
|
9 |
init = do
|
|
10 |
return Facility{ threadId=Nothing, handler=handleEvent, waiter=nullWaiter }
|
|
11 |
|
|
12 |
|
8 | 13 |
handleEvent :: FacilityHandler
|
9 | 14 |
handleEvent (List [Symbol "obtain-random-u16", payload]) = do
|
10 | 15 |
v <- randomRIO (0, 65535)
|
0 | 0 |
module Language.Robin.Facilities where
|
|
1 |
|
|
2 |
import Control.Concurrent (ThreadId)
|
1 | 3 |
|
2 | 4 |
import Language.Robin.Expr
|
3 | 5 |
|
4 | 6 |
type Event = Either String [Expr]
|
5 | 7 |
type FacilityHandler = Expr -> IO [Expr]
|
6 | 8 |
type WaitForEvents = IO Event
|
|
9 |
|
|
10 |
data Facility = Facility {
|
|
11 |
threadId :: Maybe ThreadId,
|
|
12 |
handler :: FacilityHandler,
|
|
13 |
waiter :: WaitForEvents
|
|
14 |
}
|
|
15 |
|
|
16 |
nullWaiter :: WaitForEvents
|
|
17 |
nullWaiter = return $ Right []
|
11 | 11 |
import Language.Robin.Builtins (robinBuiltins)
|
12 | 12 |
import qualified Language.Robin.TopLevel as TopLevel
|
13 | 13 |
import Language.Robin.EventLoop (eventLoop)
|
|
14 |
import Language.Robin.Facilities (handler)
|
14 | 15 |
import qualified Language.Robin.Facilities.LineTerminal as LineTerminal
|
15 | 16 |
import qualified Language.Robin.Facilities.RandomSource as RandomSource
|
16 | 17 |
|
|
24 | 25 |
let (args', env', showEvents) = processFlags args (mergeEnvs robinIntrinsics robinBuiltins) False
|
25 | 26 |
(_, reactors, results) <- processArgs args' env'
|
26 | 27 |
writeResults $ reverse results
|
27 | |
let waitForEvents = LineTerminal.waitForEvent
|
28 | |
-- let waitForEvents = waitForLineTerminalEventAsync -- TODO: compose several of these
|
29 | |
eventLoop showEvents [LineTerminal.handleEvent, RandomSource.handleEvent] waitForEvents reactors
|
|
28 |
lineTerminal <- LineTerminal.init
|
|
29 |
randomSource <- RandomSource.init
|
|
30 |
let waitForEvents = LineTerminal.waitForEvent -- TODO: compose all waiters
|
|
31 |
eventLoop showEvents [handler lineTerminal, handler randomSource] waitForEvents reactors
|
30 | 32 |
exitWith ExitSuccess
|
31 | 33 |
|
32 | 34 |
|