-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.hs
75 lines (62 loc) · 1.97 KB
/
Main.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
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
module Main where
import Control.Concurrent
import Control.Monad
import qualified Network.Mosquitto as M
import Network.Mosquitto.Internal.Types
import Control.Applicative
import Options
data MainOptions = MainOptions
{ caCert :: String
, userCert :: String
, userKey :: String
, server :: String
, serverName :: String
, port :: Int
, keepAlive :: Int
}
instance Options MainOptions where
defineOptions = pure MainOptions
<*> simpleOption "ca" "rootCA.pem"
"server's CA"
<*> simpleOption "cert" "cert.pem"
"client cert"
<*> simpleOption "key" "cert.key"
"client key"
<*> simpleOption "server" "localhost"
"server"
<*> simpleOption "name" "server-demo"
"server's name"
<*> simpleOption "port" 8883
"server's port"
<*> simpleOption "keep-alive" 1200
"keepalive time in seconds"
main :: IO ()
main = runCommand $ \MainOptions{..} args -> M.withMosquittoLibrary $ do
print M.version
m <- M.newMosquitto True serverName (Just ())
M.setTls m caCert userCert userKey
M.setTlsInsecure m True
M.setReconnectDelay m True 2 30
M.onMessage m print
M.onLog m $ const putStrLn
M.onConnect m $ \c -> do
print ("onConnect received", c)
M.subscribe m 0 "#"
M.onDisconnect m print
M.onSubscribe m $ curry print
conRes <- M.connect m server port keepAlive
conResStr <- M.strerror(conRes)
print ("connect returned", conRes, conResStr)
forkIO $ forever $ do
M.publish m False 0 "hello" "bla"
threadDelay 5000000
forkIO $ do
threadDelay 60000000
res <- M.disconnect m
print ("disconnect returned", res)
loopRes <- M.loopForeverExt m 0
loopResStr <- M.strerror(loopRes)
print ("loopForeverExt returned", loopRes, loopResStr)
M.destroyMosquitto m
print "The end"