summaryrefslogtreecommitdiff
path: root/src/Network/Tox/DHT
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-13 20:50:00 -0400
committerjoe <joe@jerkface.net>2018-06-13 20:50:00 -0400
commitd8ac778e803ac6de7c01ab4c8af767647ebc2d07 (patch)
tree98b4ff2acb4533c078ff0ced369a7cac69bc089c /src/Network/Tox/DHT
parent2ad144b323122c5eecfc9156a6d65a7018f003ad (diff)
tox: mechanism to register node-info callbacks.
Diffstat (limited to 'src/Network/Tox/DHT')
-rw-r--r--src/Network/Tox/DHT/Handlers.hs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/Network/Tox/DHT/Handlers.hs b/src/Network/Tox/DHT/Handlers.hs
index 25244a9b..091374f5 100644
--- a/src/Network/Tox/DHT/Handlers.hs
+++ b/src/Network/Tox/DHT/Handlers.hs
@@ -130,6 +130,28 @@ data Routing = Routing
130 , nodesOfInterest :: TVar (HashMap NodeId [NodeInfoCallback]) 130 , nodesOfInterest :: TVar (HashMap NodeId [NodeInfoCallback])
131 } 131 }
132 132
133registerNodeCallback :: Routing -> NodeInfoCallback -> STM ()
134registerNodeCallback Routing{nodesOfInterest} cb = do
135 cbm <- readTVar nodesOfInterest
136 let ns = fromMaybe [] $ HashMap.lookup (interestingNodeId cb) cbm
137 bs = filter nonMatching ns
138 where nonMatching n = (listenerId n /= listenerId cb)
139 writeTVar nodesOfInterest $ HashMap.insert (interestingNodeId cb)
140 (cb : bs)
141 cbm
142
143unregisterNodeCallback :: Int -> Routing -> NodeId -> STM ()
144unregisterNodeCallback callbackId Routing{nodesOfInterest} nid = do
145 cbm <- readTVar nodesOfInterest
146 let ns = fromMaybe [] $ HashMap.lookup nid cbm
147 bs = filter nonMatching ns
148 where nonMatching n = (listenerId n /= callbackId)
149 writeTVar nodesOfInterest
150 $ if null bs
151 then HashMap.delete nid cbm
152 else HashMap.insert nid bs cbm
153
154
133sched4 :: Routing -> TVar (Int.PSQ POSIXTime) 155sched4 :: Routing -> TVar (Int.PSQ POSIXTime)
134sched4 Routing { refresher4 = BucketRefresher { refreshQueue } } = refreshQueue 156sched4 Routing { refresher4 = BucketRefresher { refreshQueue } } = refreshQueue
135 157