summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT/Session.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT/Session.hs')
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index aa6ee396..bec2dabc 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -106,9 +106,11 @@ import Data.Serialize as S
106import Data.Torrent as Torrent 106import Data.Torrent as Torrent
107import Network.KRPC as KRPC hiding (Options, def) 107import Network.KRPC as KRPC hiding (Options, def)
108import qualified Network.KRPC as KRPC (def) 108import qualified Network.KRPC as KRPC (def)
109import Network.KRPC.Message (KMessageOf)
110#ifdef VERSION_bencoding 109#ifdef VERSION_bencoding
111import Data.BEncode (BValue) 110import Data.BEncode (BValue)
111import Network.KRPC.Message (KMessageOf)
112#else
113import Data.Tox as Tox
112#endif 114#endif
113import Network.BitTorrent.Address 115import Network.BitTorrent.Address
114import Network.BitTorrent.DHT.ContactInfo (PeerStore) 116import Network.BitTorrent.DHT.ContactInfo (PeerStore)
@@ -257,11 +259,19 @@ data Node ip = Node
257 259
258 -- | Pseudo-unique self-assigned session identifier. This value is 260 -- | Pseudo-unique self-assigned session identifier. This value is
259 -- constant during DHT session and (optionally) between sessions. 261 -- constant during DHT session and (optionally) between sessions.
260 , tentativeNodeId :: !NodeId 262#ifdef VERSION_bencoding
263 , tentativeNodeId :: !(NodeId KMessageOf)
264#else
265 , tentativeNodeId :: !(NodeId Tox.Message)
266#endif
261 267
262 , resources :: !InternalState 268 , resources :: !InternalState
263 , manager :: !(Manager (DHT ip )) -- ^ RPC manager; 269 , manager :: !(Manager (DHT ip )) -- ^ RPC manager;
264 , routingInfo :: !(TVar (Maybe (R.Info ip))) -- ^ search table; 270#ifdef VERSION_bencoding
271 , routingInfo :: !(TVar (Maybe (R.Info KMessageOf ip ()))) -- ^ search table;
272#else
273 , routingInfo :: !(TVar (Maybe (R.Info Tox.Message ip Bool))) -- ^ search table;
274#endif
265 , contactInfo :: !(TVar (PeerStore ip )) -- ^ published by other nodes; 275 , contactInfo :: !(TVar (PeerStore ip )) -- ^ published by other nodes;
266 , announceInfo :: !(TVar AnnounceSet ) -- ^ to publish by this node; 276 , announceInfo :: !(TVar AnnounceSet ) -- ^ to publish by this node;
267 , sessionTokens :: !(TVar SessionTokens ) -- ^ query session IDs. 277 , sessionTokens :: !(TVar SessionTokens ) -- ^ query session IDs.
@@ -319,7 +329,7 @@ instance MonadLogger (DHT ip) where
319#ifdef VERSION_bencoding 329#ifdef VERSION_bencoding
320type NodeHandler ip = Handler (DHT ip) KMessageOf BValue 330type NodeHandler ip = Handler (DHT ip) KMessageOf BValue
321#else 331#else
322type NodeHandler ip = Handler (DHT ip) KMessageOf ByteString 332type NodeHandler ip = Handler (DHT ip) Tox.Message ByteString
323#endif 333#endif
324 334
325-- | Run DHT session. You /must/ properly close session using 335-- | Run DHT session. You /must/ properly close session using
@@ -330,7 +340,11 @@ newNode :: Address ip
330 -> Options -- ^ various dht options; 340 -> Options -- ^ various dht options;
331 -> NodeAddr ip -- ^ node address to bind; 341 -> NodeAddr ip -- ^ node address to bind;
332 -> LogFun -- ^ 342 -> LogFun -- ^
333 -> Maybe NodeId -- ^ use this NodeId, if not given a new one is generated. 343#ifdef VERSION_bencoding
344 -> Maybe (NodeId KMessageOf) -- ^ use this NodeId, if not given a new one is generated.
345#else
346 -> Maybe (NodeId Tox.Message) -- ^ use this NodeId, if not given a new one is generated.
347#endif
334 -> IO (Node ip) -- ^ a new DHT node running at given address. 348 -> IO (Node ip) -- ^ a new DHT node running at given address.
335newNode hs opts naddr logger mbid = do 349newNode hs opts naddr logger mbid = do
336 s <- createInternalState 350 s <- createInternalState
@@ -406,7 +420,11 @@ routableAddress = do
406 return $ myAddress <$> info 420 return $ myAddress <$> info
407 421
408-- | The current NodeId that the given remote node should know us by. 422-- | The current NodeId that the given remote node should know us by.
409myNodeIdAccordingTo :: NodeAddr ip -> DHT ip NodeId 423#ifdef VERSION_bencoding
424myNodeIdAccordingTo :: NodeAddr ip -> DHT ip (NodeId KMessageOf)
425#else
426myNodeIdAccordingTo :: NodeAddr ip -> DHT ip (NodeId Tox.Message)
427#endif
410myNodeIdAccordingTo _ = do 428myNodeIdAccordingTo _ = do
411 info <- asks routingInfo >>= liftIO . atomically . readTVar 429 info <- asks routingInfo >>= liftIO . atomically . readTVar
412 maybe (asks tentativeNodeId) 430 maybe (asks tentativeNodeId)
@@ -415,7 +433,11 @@ myNodeIdAccordingTo _ = do
415 433
416-- | Get current routing table. Normally you don't need to use this 434-- | Get current routing table. Normally you don't need to use this
417-- function, but it can be usefull for debugging and profiling purposes. 435-- function, but it can be usefull for debugging and profiling purposes.
418getTable :: Eq ip => DHT ip (Table ip) 436#ifdef VERSION_bencoding
437getTable :: Eq ip => DHT ip (Table KMessageOf ip ())
438#else
439getTable :: Eq ip => DHT ip (Table Tox.Message ip Bool)
440#endif
419getTable = do 441getTable = do
420 Node { tentativeNodeId = myId 442 Node { tentativeNodeId = myId
421 , routingInfo = var 443 , routingInfo = var
@@ -452,7 +474,11 @@ allPeers ih = do
452-- 474--
453-- This operation used for 'find_nodes' query. 475-- This operation used for 'find_nodes' query.
454-- 476--
455getClosest :: Eq ip => TableKey k => k -> DHT ip [NodeInfo KMessageOf ip ()] 477#ifdef VERSION_bencoding
478getClosest :: Eq ip => TableKey KMessageOf k => k -> DHT ip [NodeInfo KMessageOf ip ()]
479#else
480getClosest :: Eq ip => TableKey Tox.Message k => k -> DHT ip [NodeInfo Tox.Message ip Bool]
481#endif
456getClosest node = do 482getClosest node = do
457 k <- asks (optK . options) 483 k <- asks (optK . options)
458 kclosest k node <$> getTable 484 kclosest k node <$> getTable