PageRenderTime 55ms CodeModel.GetById 45ms app.highlight 1ms RepoModel.GetById 8ms app.codeStats 0ms

/examples/Chat.hs

http://github.com/DanBurton/netspec
Haskell | 26 lines | 18 code | 3 blank | 5 comment | 5 complexity | f3c580c3559d25e990a2aed2c09d0e5d MD5 | raw file
Possible License(s): BSD-3-Clause
 1{-# LANGUAGE OverloadedStrings #-}
 2
 3import Network.NetSpec
 4import Network.NetSpec.Text
 5
 6-- Notice the restrictions NetSpec places on how you can communicate.
 7-- Working within NetSpec isn't well-suited for all situations;
 8--  it is specialized for situations where there is
 9--  a deterministic sequence of communication.
10
11main :: IO ()
12main = runSpec ServerSpec {
13    _ports = [PortNumber 5001, PortNumber 5002]
14  , _begin = \hs -> do
15       hs ! "Welcome to the one-for-one chat program."
16       hs ! "Send a message in order to receive one."
17       hs ! "Say \"bye\" to stop."
18  , _loop = \[a, b] () -> do
19       lineA <- receive a
20       lineB <- receive b
21       a ! lineB
22       b ! lineA
23       if lineA == "bye\r" || lineB == "bye\r"
24         then stop_ else continue_
25  , _end = \hs () -> hs ! "That's all folks."
26  }