summaryrefslogtreecommitdiff
path: root/src/Network/Kademlia/Bootstrap.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-07 18:51:05 -0500
committerjoe <joe@jerkface.net>2017-11-08 02:30:43 -0500
commitdbce015d0137152f74f46dea3b00d2b51e7c53f7 (patch)
tree39947fb5a0d0d0aedb0121f4bdd95c41caf0e152 /src/Network/Kademlia/Bootstrap.hs
parent8c94bb53cc2eb09a5e1c550c3430935701c6f090 (diff)
Moved BucketRefresher construction responsibility for greater
encapsulation.
Diffstat (limited to 'src/Network/Kademlia/Bootstrap.hs')
-rw-r--r--src/Network/Kademlia/Bootstrap.hs34
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
32import Data.Monoid 32import Data.Monoid
33import Data.Serialize (Serialize) 33import Data.Serialize (Serialize)
34import Data.Time.Clock.POSIX (POSIXTime) 34import Data.Time.Clock.POSIX (POSIXTime)
35import Data.Ord
35import System.Entropy 36import System.Entropy
36import System.Timeout 37import System.Timeout
37import Text.PrettyPrint as PP hiding (($$), (<>)) 38import 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
76newBucketRefresher :: (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)
82newBucketRefresher 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'.
97updateRefresherIO :: Ord addr
98 => Search nid addr tok ni ni
99 -> (ni -> IO Bool)
100 -> BucketRefresher nid ni -> BucketRefresher nid ni
101updateRefresherIO 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.
76forkPollForRefresh :: SensibleNodeId nid ni => BucketRefresher nid ni -> IO ThreadId 110forkPollForRefresh :: SensibleNodeId nid ni => BucketRefresher nid ni -> IO ThreadId
77forkPollForRefresh BucketRefresher{ refreshInterval 111forkPollForRefresh BucketRefresher{ refreshInterval