This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Lec20.hs
223 lines (159 loc) · 4.26 KB
/
Lec20.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
module Main where
{- LECTURE 20 : CONCURRENCY -}
import Control.Concurrent
import Control.Monad (forever, forM_)
import Network
import System.IO
import Text.Printf
-- forkIO :: IO () -> IO ThreadId
muddle :: IO ()
muddle = do
hSetBuffering stdout NoBuffering
forkIO (forM_ [1..1000] (\_ -> putChar 'A'))
forM_ [1..1000] (\_ -> putChar 'B')
-- threadDelay :: Int -> IO ()
setReminder :: String -> IO ()
setReminder s = do
let t = read s :: Int
printf "Ok, I'll remind you in %d seconds\n" t
threadDelay (10^6 * t)
printf "REMINDER!!! %d seconds are up\a\a\a\n" t
reminderMain :: IO ()
reminderMain = loop
where
loop = do
s <- getLine
if s == "end" then return ()
else do forkIO (setReminder s)
loop
{- PART II : MVARS -}
{- interface
newEmptyMVar :: IO (MVar a)
newMVar :: a -> IO (MVar a)
takeMVar :: MVar a -> IO a
putMVar :: MVar a -> a -> IO ()
newIORef :: a -> IO (IORef a)
getIORef :: IORef a -> IO a
putIORef :: IORef a -> a -> IO ()
-}
mvar1 = do
m <- newEmptyMVar
forkIO $ putMVar m 'x'
r <- takeMVar m
print r
mvar2 = do
m <- newEmptyMVar
forkIO $ do putMVar m 'x'
putMVar m 'y'
r <- takeMVar m
print r
r <- takeMVar m
print r
-- Logger example
{-
data Logger
initLogger :: IO Logger
logMessage :: Logger -> String -> IO ()
logStop :: Logger -> IO ()
-}
data Logger = Logger (MVar LogCommand)
data LogCommand = Message String | Stop (MVar ())
initLogger :: IO Logger
initLogger = do
m <- newEmptyMVar
let l = Logger m
forkIO (logger l)
return l
logger :: Logger -> IO ()
logger (Logger m) = loop
where
loop = do
threadDelay (10^6 * 1)
cmd <- takeMVar m
case cmd of
Message msg -> do
putStrLn ("LOG: " ++ msg)
loop
Stop s -> do
putStrLn "Stopping logger"
putMVar s ()
logStop :: Logger -> () -> IO ()
logStop (Logger m) () = do
s <- newEmptyMVar
putMVar m (Stop s)
takeMVar s
logMessage :: Logger -> String -> IO ()
logMessage (Logger m) s = putMVar m (Message s)
loggerMain :: IO ()
loggerMain = do
l <- initLogger
l `logMessage` "hello"
putStrLn "We didn't wait for the log message"
l `logMessage` "bye"
l `logStop` ()
putStrLn "End of program"
----------------------------------------------------------------------
-- A server
data CountingMsg = Inc | GetCount (MVar Int)
newtype Counter = MkCounter (MVar CountingMsg)
makeCounter :: IO Counter
makeCounter = do
m <- newEmptyMVar
forkIO (loop m (0 :: Int))
return (MkCounter m)
where
loop m c = do
cmd <- takeMVar m
case cmd of
Inc -> do
printf "New doubling served! %d doublings so far!\n" (c+1)
loop m (c+1)
GetCount r -> do
putMVar r c
loop m c
msgCounter :: Counter -> CountingMsg -> IO ()
msgCounter (MkCounter m) msg =
putMVar m msg
----------------------------------------------------------------------
-- A Key-Value server
updateMap :: MVar [(String,Int)] -> String -> Int -> IO ()
updateMap m k v = do
kvs <- takeMVar m
putMVar m ((k,v):kvs)
readMap :: MVar [(String,Int)] -> String -> IO (Maybe Int)
readMap m k = do
kvs <- takeMVar m
putMVar m kvs
return (lookup k kvs)
----------------------------------------------------------------------
talk :: Handle -> Counter -> IO ()
talk h c =
do hSetBuffering h LineBuffering
hSetNewlineMode h (NewlineMode { inputNL = CRLF, outputNL = CRLF })
loop
where
loop =
do line <- hGetLine h
case line of
"end" ->
hPutStrLn h "Bye!"
"count" -> do
r <- newEmptyMVar
c `msgCounter` (GetCount r)
c <- takeMVar r
hPutStrLn h ("Count is " ++ show c)
loop
line -> do
hPutStrLn h (show (2 * read line :: Integer))
c `msgCounter` Inc
loop
main = do
c <- makeCounter
sock <- listenOn (PortNumber 1234)
printf "Listening...\n"
forever $ do
(handle, host, port) <- accept sock
printf "Accepted connection (%s:%s)\n" host (show port)
forkFinally (talk handle c)
(\_ -> do printf "Connection closed (%s:%s)\n" host (show port)
hClose handle)