-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.hs
165 lines (149 loc) · 4.98 KB
/
Test.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
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
-- |
-- Module: $HEADER$
-- Description: TODO
-- Copyright: (c) 2016 Peter Trško
-- License: BSD3
--
-- Stability: experimental
-- Portability: GHC specific language extensions.
--
-- TODO
module Test
where
import Control.Concurrent (ThreadId, forkIO{-, threadDelay-})
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TChan (TChan, dupTChan, newTChanIO, readTChan)
import Control.Exception (finally)
import Control.Monad ((>>=), forever, return)
import Data.Either (Either(Left, Right))
import Data.Function (($), (.))
import Data.Functor ((<$>))
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef)
import Data.Maybe (Maybe(Just, Nothing), fromMaybe)
import Data.String (IsString(fromString))
import System.IO (IO)
import System.IO.Unsafe (unsafePerformIO)
import Text.Show (Show(show))
import Data.Default.Class (Default(def))
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
( empty
, insert
, lookup
)
import Data.LogStr.Formatting ((%), format, shown)
import System.Lumberjack.Backend
( SomeLoggingBackend(SomeLoggingBackend)
, pushLogStrLn
)
import System.Lumberjack.FastLogger (fastLogger)
import Data.DHT.DKS.Internal
( DksHandle
, newDksHandle
)
import Data.DHT.DKS.Type.EVar (EVarIO, success)
import Data.DHT.DKS.Type.Hash (DksHash, dksHash)
import Data.DHT.DKS.Type.Message
( DksMessage(DksMessage, _header)
, DksMessageHeader(DksMessageHeader, _to)
)
import Data.DHT.DKS.Type.MessageChannel (StmMessageChannel(StmMessageChannel))
import Data.DHT.DKS.Type.Params (DksParams(_discovery, _logging))
node1, node2, node3 :: DksHash
node1 = dksHash "1464808099"
node2 = dksHash "1464808107"
node3 = dksHash "1464893893"
data S = S
{ _network :: HashMap DksHash (DksMessage -> EVarIO ())
, _networkBus :: TChan DksMessage
, _logger :: SomeLoggingBackend
, _knownNode :: Maybe DksHash
}
state :: IORef S
state = unsafePerformIO $ newIORef S
{ _network = HashMap.empty
, _networkBus = unsafePerformIO newTChanIO
, _logger = SomeLoggingBackend . unsafePerformIO $ fastLogger def
, _knownNode = Nothing
}
startNetworkMain :: IO ThreadId
startNetworkMain = do
l <- getLogging
forkIO $ networkMain l
`finally` pushLogStrLn l "networkMain: Thread terminated unexpectedly."
networkMain :: SomeLoggingBackend -> IO ()
networkMain l = do
bus <- readIORef state >>= atomically . dupTChan . _networkBus
forever $ do
pushLogStrLn l "networkMain: Iteration start."
msg@DksMessage{_header = DksMessageHeader{_to = toNode}}
<- atomically $ readTChan bus
pushLogStrLn l $ format ("networkMain: Processing: " % shown) msg
net <- _network <$> readIORef state
case HashMap.lookup toNode net of
Nothing -> pushLogStrLn l
$ format ("networkMain: Unknown \"to\" node: " % shown) toNode
Just send -> do
pushLogStrLn l $ format
("networkMain: Passing message to node: " % shown) toNode
send msg >>= handleSendFailure toNode
where
handleSendFailure toNode = \case
Right () -> return ()
Left e -> do
pushLogStrLn l $ format
( "networkMain: Encountered failure when passing message to "
% " node: " % shown % ": " % shown) toNode e
discoverNode :: DksHash -> EVarIO (Maybe DksHash)
discoverNode node =
atomicModifyIORef' state $ \s ->
let possibleNode = _knownNode s
in ( s{_knownNode = Just $ fromMaybe node possibleNode}
, success possibleNode
)
setLogging :: IO ()
setLogging = do
l <- fastLogger def
atomicModifyIORef' state $ \s -> (s{_logger = SomeLoggingBackend l}, ())
getLogging :: IO SomeLoggingBackend
getLogging = _logger <$> readIORef state
insertChan :: DksHash -> (DksMessage -> EVarIO ()) -> IO ()
insertChan node send =
atomicModifyIORef' state $ \s -> (, ()) s
{ _network = HashMap.insert node send (_network s)
}
newDksChan :: IO StmMessageChannel
newDksChan = StmMessageChannel insertChan . _networkBus <$> readIORef state
-- |
--
-- > stack ghci
-- > ghci> :l Test.hs
-- > ghci> import Data.DHT.DKS.Internal
-- > ghci> startNetworkMain
-- > ghci> --
-- > ghci> h1 <- test node1
-- > ghci> dksJoin printLog h1
-- > ghci> --
-- > ghci> h2 <- test node2
-- > ghci> dksJoin printLog h2
-- > ghci> --
-- > ghci> h3 <- test node3
-- > ghci> dksJoin printLog h3
test :: DksHash -> IO DksHandle
test node = do
l <- getLogging
dksChan <- newDksChan
let opts = def
{ _logging = l
-- , _yield = threadDelay 10000000
, _discovery = discoverNode node
}
newDksHandle dksChan opts node
printLog :: Show a => a -> IO ()
printLog a = do
l <- _logger <$> readIORef state
pushLogStrLn l . fromString $ show a