diff options
author | joe <joe@jerkface.net> | 2018-06-13 20:50:00 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-13 20:50:00 -0400 |
commit | d8ac778e803ac6de7c01ab4c8af767647ebc2d07 (patch) | |
tree | 98b4ff2acb4533c078ff0ced369a7cac69bc089c /src/Network/Tox/DHT | |
parent | 2ad144b323122c5eecfc9156a6d65a7018f003ad (diff) |
tox: mechanism to register node-info callbacks.
Diffstat (limited to 'src/Network/Tox/DHT')
-rw-r--r-- | src/Network/Tox/DHT/Handlers.hs | 22 |
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 | ||
133 | registerNodeCallback :: Routing -> NodeInfoCallback -> STM () | ||
134 | registerNodeCallback 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 | |||
143 | unregisterNodeCallback :: Int -> Routing -> NodeId -> STM () | ||
144 | unregisterNodeCallback 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 | |||
133 | sched4 :: Routing -> TVar (Int.PSQ POSIXTime) | 155 | sched4 :: Routing -> TVar (Int.PSQ POSIXTime) |
134 | sched4 Routing { refresher4 = BucketRefresher { refreshQueue } } = refreshQueue | 156 | sched4 Routing { refresher4 = BucketRefresher { refreshQueue } } = refreshQueue |
135 | 157 | ||