diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/DHT.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Query.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Session.hs | 21 |
3 files changed, 15 insertions, 11 deletions
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs index b5dc868d..92d1ec63 100644 --- a/src/Network/BitTorrent/DHT.hs +++ b/src/Network/BitTorrent/DHT.hs | |||
@@ -53,6 +53,7 @@ module Network.BitTorrent.DHT | |||
53 | 53 | ||
54 | import Control.Applicative | 54 | import Control.Applicative |
55 | import Control.Monad.Logger | 55 | import Control.Monad.Logger |
56 | import Control.Monad.Reader | ||
56 | import Control.Monad.Trans | 57 | import Control.Monad.Trans |
57 | import Data.ByteString as BS | 58 | import Data.ByteString as BS |
58 | import Data.Conduit as C | 59 | import Data.Conduit as C |
@@ -133,7 +134,7 @@ resolveHostName NodeAddr {..} = do | |||
133 | bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () | 134 | bootstrap :: Address ip => [NodeAddr ip] -> DHT ip () |
134 | bootstrap startNodes = do | 135 | bootstrap startNodes = do |
135 | $(logInfoS) "bootstrap" "Start node bootstrapping" | 136 | $(logInfoS) "bootstrap" "Start node bootstrapping" |
136 | nid <- getNodeId | 137 | nid <- asks thisNodeId |
137 | -- TODO filter duplicated in startNodes list | 138 | -- TODO filter duplicated in startNodes list |
138 | aliveNodes <- queryParallel (ping <$> startNodes) | 139 | aliveNodes <- queryParallel (ping <$> startNodes) |
139 | _ <- sourceList [aliveNodes] $= search nid (findNodeQ nid) $$ C.consume | 140 | _ <- sourceList [aliveNodes] $= search nid (findNodeQ nid) $$ C.consume |
diff --git a/src/Network/BitTorrent/DHT/Query.hs b/src/Network/BitTorrent/DHT/Query.hs index 627bc096..724c07e6 100644 --- a/src/Network/BitTorrent/DHT/Query.hs +++ b/src/Network/BitTorrent/DHT/Query.hs | |||
@@ -54,7 +54,7 @@ nodeHandler action = handler $ \ sockAddr (Query remoteId q) -> do | |||
54 | Nothing -> throwIO BadAddress | 54 | Nothing -> throwIO BadAddress |
55 | Just naddr -> do | 55 | Just naddr -> do |
56 | insertNode (NodeInfo remoteId naddr) | 56 | insertNode (NodeInfo remoteId naddr) |
57 | Response <$> getNodeId <*> action naddr q | 57 | Response <$> asks thisNodeId <*> action naddr q |
58 | 58 | ||
59 | pingH :: Address ip => NodeHandler ip | 59 | pingH :: Address ip => NodeHandler ip |
60 | pingH = nodeHandler $ \ _ Ping -> do | 60 | pingH = nodeHandler $ \ _ Ping -> do |
diff --git a/src/Network/BitTorrent/DHT/Session.hs b/src/Network/BitTorrent/DHT/Session.hs index cbdc9ff8..11e1c81a 100644 --- a/src/Network/BitTorrent/DHT/Session.hs +++ b/src/Network/BitTorrent/DHT/Session.hs | |||
@@ -23,12 +23,16 @@ module Network.BitTorrent.DHT.Session | |||
23 | -- * Session | 23 | -- * Session |
24 | , Node | 24 | , Node |
25 | , options | 25 | , options |
26 | , thisNodeId | ||
27 | |||
28 | -- ** Initialization | ||
26 | , LogFun | 29 | , LogFun |
27 | , NodeHandler | 30 | , NodeHandler |
28 | , startNode | 31 | , startNode |
29 | 32 | ||
30 | -- * DHT | 33 | -- * DHT |
31 | -- | Use @asks options@ to get options passed to 'startNode'. | 34 | -- | Use @asks options@ to get options passed to 'startNode' |
35 | -- or @asks thisNodeId@ to get id of locally running node. | ||
32 | , DHT | 36 | , DHT |
33 | , runDHT | 37 | , runDHT |
34 | 38 | ||
@@ -38,7 +42,6 @@ module Network.BitTorrent.DHT.Session | |||
38 | 42 | ||
39 | -- * Routing table | 43 | -- * Routing table |
40 | , getTable | 44 | , getTable |
41 | , getNodeId | ||
42 | , getClosest | 45 | , getClosest |
43 | , insertNode | 46 | , insertNode |
44 | 47 | ||
@@ -225,8 +228,12 @@ type LogFun = Loc -> LogSource -> LogLevel -> LogStr -> IO () | |||
225 | 228 | ||
226 | -- | DHT session keep track state of /this/ node. | 229 | -- | DHT session keep track state of /this/ node. |
227 | data Node ip = Node | 230 | data Node ip = Node |
228 | { options :: !Options -- ^ session configuration; | 231 | { -- | Session configuration; |
229 | , thisNodeId :: !NodeId -- ^ session identifier; | 232 | options :: !Options |
233 | |||
234 | -- | Pseudo-unique self-assigned session identifier. This value is | ||
235 | -- constant during DHT session and (optionally) between sessions. | ||
236 | , thisNodeId :: !NodeId | ||
230 | , manager :: !(Manager (DHT ip)) -- ^ RPC manager; | 237 | , manager :: !(Manager (DHT ip)) -- ^ RPC manager; |
231 | , routingTable :: !(MVar (Table ip)) -- ^ search table; | 238 | , routingTable :: !(MVar (Table ip)) -- ^ search table; |
232 | , contactInfo :: !(TVar (PeerStore ip)) -- ^ published by other nodes; | 239 | , contactInfo :: !(TVar (PeerStore ip)) -- ^ published by other nodes; |
@@ -361,10 +368,6 @@ getTable = do | |||
361 | var <- asks routingTable | 368 | var <- asks routingTable |
362 | liftIO (readMVar var) | 369 | liftIO (readMVar var) |
363 | 370 | ||
364 | -- | Get id of /this/ node. This value is constant during DHT session. | ||
365 | getNodeId :: DHT ip NodeId | ||
366 | getNodeId = asks thisNodeId | ||
367 | |||
368 | -- | Find a set of closest nodes from routing table of this node. (in | 371 | -- | Find a set of closest nodes from routing table of this node. (in |
369 | -- no particular order) | 372 | -- no particular order) |
370 | -- | 373 | -- |
@@ -440,7 +443,7 @@ deleteTopic ih p = do | |||
440 | queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b) | 443 | queryNode :: forall a b ip. Address ip => KRPC (Query a) (Response b) |
441 | => NodeAddr ip -> a -> DHT ip (NodeId, b) | 444 | => NodeAddr ip -> a -> DHT ip (NodeId, b) |
442 | queryNode addr q = do | 445 | queryNode addr q = do |
443 | nid <- getNodeId | 446 | nid <- asks thisNodeId |
444 | Response remoteId r <- query (toSockAddr addr) (Query nid q) | 447 | Response remoteId r <- query (toSockAddr addr) (Query nid q) |
445 | insertNode (NodeInfo remoteId addr) | 448 | insertNode (NodeInfo remoteId addr) |
446 | return (remoteId, r) | 449 | return (remoteId, r) |