summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-02 16:51:04 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-02 16:51:04 +0400
commit787c68a0d847c60546693765180b1fa62734bdd1 (patch)
tree7b83e368b72a4ce03405a9e53fd059d8e5f4608a /src
parent5d576086294166fd97b24585c54c2b3820bab6aa (diff)
Add DHT options
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent/DHT.hs9
-rw-r--r--src/Network/BitTorrent/DHT/Session.hs57
2 files changed, 57 insertions, 9 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index 7eef0c67..41a76886 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -82,8 +82,13 @@ handlers = [pingH, findNodeH, getPeersH, announceH]
82-----------------------------------------------------------------------} 82-----------------------------------------------------------------------}
83 83
84-- | Run DHT on specified port. <add note about resources> 84-- | Run DHT on specified port. <add note about resources>
85dht :: Address ip => NodeAddr ip -> DHT ip a -> IO a 85dht :: Address ip
86dht addr = runDHT addr handlers 86 => Options -- ^ normally you need to use 'Data.Default.def';
87 -> NodeAddr ip -- ^ address to bind this node;
88 -> DHT ip a -- ^ actions to run: 'bootstrap', 'lookup', etc;
89 -> IO a -- ^ result.
90dht = runDHT handlers
91{-# INLINE dht #-}
87 92
88-- | One good node may be sufficient. The list of bootstrapping nodes 93-- | One good node may be sufficient. The list of bootstrapping nodes
89-- usually obtained from 'Data.Torrent.tNodes' field. 94-- usually obtained from 'Data.Torrent.tNodes' field.
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs
index 4ac1bee9..debe9694 100644
--- a/src/Network/BitTorrent/DHT/Session.hs
+++ b/src/Network/BitTorrent/DHT/Session.hs
@@ -7,8 +7,15 @@
7{-# LANGUAGE TypeFamilies #-} 7{-# LANGUAGE TypeFamilies #-}
8{-# LANGUAGE TemplateHaskell #-} 8{-# LANGUAGE TemplateHaskell #-}
9module Network.BitTorrent.DHT.Session 9module Network.BitTorrent.DHT.Session
10 ( -- * Session 10 ( -- * Options
11 DHT 11 Alpha
12 , defaultAlpha
13 , K
14 , defaultK
15 , Options (..)
16
17 -- * Session
18 , DHT
12 , runDHT 19 , runDHT
13 20
14 -- * Tokens 21 -- * Tokens
@@ -62,6 +69,37 @@ import Network.BitTorrent.DHT.Message
62import Network.BitTorrent.DHT.Routing as R 69import Network.BitTorrent.DHT.Routing as R
63import Network.BitTorrent.DHT.Token as T 70import Network.BitTorrent.DHT.Token as T
64 71
72{-----------------------------------------------------------------------
73-- Options
74-----------------------------------------------------------------------}
75
76-- | Node lookups can proceed asynchronously.
77type Alpha = Int
78
79-- | The quantity of simultaneous lookups is typically three.
80defaultAlpha :: Alpha
81defaultAlpha = 3
82
83data Options = Options
84 { -- | the degree of parallelism in 'find_node' queries.
85 optAlpha :: {-# UNPACK #-} !Alpha
86
87 -- | number of nodes to return in 'find_node' responses.
88 , optK :: {-# UNPACK #-} !K
89
90 -- | RPC timeout.
91 , optTimeout :: {-# UNPACK #-} !NominalDiffTime
92
93-- , optReannounceInterval :: NominalDiffTime
94-- , optDataExpiredTimeout :: NominalDiffTime
95 } deriving (Show, Eq)
96
97instance Default Options where
98 def = Options
99 { optAlpha = defaultAlpha
100 , optK = defaultK
101 , optTimeout = 5 -- seconds
102 }
65 103
66{----------------------------------------------------------------------- 104{-----------------------------------------------------------------------
67-- Tokens policy 105-- Tokens policy
@@ -93,7 +131,8 @@ invalidateTokens curTime ts @ SessionTokens {..}
93-----------------------------------------------------------------------} 131-----------------------------------------------------------------------}
94 132
95data Node ip = Node 133data Node ip = Node
96 { manager :: !(Manager (DHT ip)) 134 { options :: !Options
135 , manager :: !(Manager (DHT ip))
97 , routingTable :: !(MVar (Table ip)) 136 , routingTable :: !(MVar (Table ip))
98 , contactInfo :: !(TVar (PeerStore ip)) 137 , contactInfo :: !(TVar (PeerStore ip))
99 , sessionTokens :: !(TVar SessionTokens) 138 , sessionTokens :: !(TVar SessionTokens)
@@ -126,15 +165,16 @@ instance MonadLogger (DHT ip) where
126 liftIO $ logger loc src lvl (toLogStr msg) 165 liftIO $ logger loc src lvl (toLogStr msg)
127 166
128runDHT :: forall ip a. Address ip 167runDHT :: forall ip a. Address ip
129 => NodeAddr ip -- ^ node address to bind; 168 => [Handler (DHT ip)] -- ^ handlers to run on accepted queries;
130 -> [Handler (DHT ip)] -- ^ handlers to run on accepted queries; 169 -> Options -- ^ various dht options;
170 -> NodeAddr ip -- ^ node address to bind;
131 -> DHT ip a -- ^ DHT action to run; 171 -> DHT ip a -- ^ DHT action to run;
132 -> IO a -- ^ result. 172 -> IO a -- ^ result.
133runDHT naddr handlers action = runResourceT $ do 173runDHT handlers opts naddr action = runResourceT $ do
134 runStderrLoggingT $ LoggingT $ \ logger -> do 174 runStderrLoggingT $ LoggingT $ \ logger -> do
135 (_, m) <- allocate (newManager (toSockAddr naddr) handlers) closeManager 175 (_, m) <- allocate (newManager (toSockAddr naddr) handlers) closeManager
136 myId <- liftIO genNodeId 176 myId <- liftIO genNodeId
137 node <- liftIO $ Node m 177 node <- liftIO $ Node opts m
138 <$> newMVar (nullTable myId) 178 <$> newMVar (nullTable myId)
139 <*> newTVarIO def 179 <*> newTVarIO def
140 <*> (newTVarIO =<< nullSessionTokens) 180 <*> (newTVarIO =<< nullSessionTokens)
@@ -157,6 +197,9 @@ ping addr = do
157 let _ = result :: Either SomeException Ping 197 let _ = result :: Either SomeException Ping
158 return $ either (const False) (const True) result 198 return $ either (const False) (const True) result
159 199
200-- /pick a random ID/ in the range of the bucket and perform a
201-- find_nodes search on it.
202
160-- FIXME do not use getClosest sinse we should /refresh/ them 203-- FIXME do not use getClosest sinse we should /refresh/ them
161refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip] 204refreshNodes :: Address ip => NodeId -> DHT ip [NodeInfo ip]
162refreshNodes nid = do 205refreshNodes nid = do