diff options
author | joe <joe@jerkface.net> | 2017-11-07 18:51:05 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-11-08 02:30:43 -0500 |
commit | dbce015d0137152f74f46dea3b00d2b51e7c53f7 (patch) | |
tree | 39947fb5a0d0d0aedb0121f4bdd95c41caf0e152 /src/Network/Kademlia/Bootstrap.hs | |
parent | 8c94bb53cc2eb09a5e1c550c3430935701c6f090 (diff) |
Moved BucketRefresher construction responsibility for greater
encapsulation.
Diffstat (limited to 'src/Network/Kademlia/Bootstrap.hs')
-rw-r--r-- | src/Network/Kademlia/Bootstrap.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/Network/Kademlia/Bootstrap.hs b/src/Network/Kademlia/Bootstrap.hs index 92a20ca5..87fdc22f 100644 --- a/src/Network/Kademlia/Bootstrap.hs +++ b/src/Network/Kademlia/Bootstrap.hs | |||
@@ -32,6 +32,7 @@ import Data.IP | |||
32 | import Data.Monoid | 32 | import Data.Monoid |
33 | import Data.Serialize (Serialize) | 33 | import Data.Serialize (Serialize) |
34 | import Data.Time.Clock.POSIX (POSIXTime) | 34 | import Data.Time.Clock.POSIX (POSIXTime) |
35 | import Data.Ord | ||
35 | import System.Entropy | 36 | import System.Entropy |
36 | import System.Timeout | 37 | import System.Timeout |
37 | import Text.PrettyPrint as PP hiding (($$), (<>)) | 38 | import Text.PrettyPrint as PP hiding (($$), (<>)) |
@@ -72,6 +73,39 @@ data BucketRefresher nid ni = forall tok addr. Ord addr => BucketRefresher | |||
72 | , refreshPing :: ni -> IO Bool | 73 | , refreshPing :: ni -> IO Bool |
73 | } | 74 | } |
74 | 75 | ||
76 | newBucketRefresher :: (Ord addr, Ord a, Hashable a) | ||
77 | => KademliaSpace a ni | ||
78 | -> ni | ||
79 | -> Search nid addr tok ni ni | ||
80 | -> (ni -> IO Bool) | ||
81 | -> STM (BucketRefresher nid ni) | ||
82 | newBucketRefresher spc template_ni sch ping = do | ||
83 | let nodeId = kademliaLocation spc | ||
84 | bkts <- newTVar $ R.nullTable (comparing nodeId) (\s -> hashWithSalt s . nodeId) template_ni R.defaultBucketCount | ||
85 | sched <- newTVar Int.empty | ||
86 | return BucketRefresher | ||
87 | { refreshInterval = 15 * 60 | ||
88 | , refreshQueue = sched | ||
89 | , refreshSearch = sch | ||
90 | , refreshBuckets = bkts | ||
91 | , refreshPing = ping | ||
92 | } | ||
93 | |||
94 | -- | This was added to avoid the compile error "Record update for | ||
95 | -- insufficiently polymorphic field" when trying to update the existentially | ||
96 | -- quantified field 'refreshSearch'. | ||
97 | updateRefresherIO :: Ord addr | ||
98 | => Search nid addr tok ni ni | ||
99 | -> (ni -> IO Bool) | ||
100 | -> BucketRefresher nid ni -> BucketRefresher nid ni | ||
101 | updateRefresherIO sch ping BucketRefresher{..} = BucketRefresher | ||
102 | { refreshSearch = sch | ||
103 | , refreshPing = ping | ||
104 | , refreshInterval = refreshInterval | ||
105 | , refreshBuckets = refreshBuckets | ||
106 | , refreshQueue = refreshQueue | ||
107 | } | ||
108 | |||
75 | -- | Fork a refresh loop. Kill the returned thread to terminate it. | 109 | -- | Fork a refresh loop. Kill the returned thread to terminate it. |
76 | forkPollForRefresh :: SensibleNodeId nid ni => BucketRefresher nid ni -> IO ThreadId | 110 | forkPollForRefresh :: SensibleNodeId nid ni => BucketRefresher nid ni -> IO ThreadId |
77 | forkPollForRefresh BucketRefresher{ refreshInterval | 111 | forkPollForRefresh BucketRefresher{ refreshInterval |