diff options
-rw-r--r-- | examples/Main.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent.hs | 26 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/Sessions.hs | 24 | ||||
-rw-r--r-- | src/Network/BitTorrent/Sessions/Types.lhs | 2 |
5 files changed, 27 insertions, 30 deletions
diff --git a/examples/Main.hs b/examples/Main.hs index 5128b290..ef12cc70 100644 --- a/examples/Main.hs +++ b/examples/Main.hs | |||
@@ -12,7 +12,7 @@ main = do | |||
12 | print (contentLayout "./" (tInfo torrent)) | 12 | print (contentLayout "./" (tInfo torrent)) |
13 | let loc = TorrentLoc path "/tmp" | 13 | let loc = TorrentLoc path "/tmp" |
14 | 14 | ||
15 | withDefaultClient 51413 3000 $ \ client -> do | 15 | withDefaultClient (head defaultPorts) 3000 $ \ client -> do |
16 | openSwarmSession client loc | 16 | openSwarmSession client loc |
17 | threadDelay 1000000000000 | 17 | threadDelay 1000000000000 |
18 | return () | 18 | return () |
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 26824724..ad96a1b8 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -9,11 +9,11 @@ | |||
9 | module Network.BitTorrent | 9 | module Network.BitTorrent |
10 | ( module Data.Torrent | 10 | ( module Data.Torrent |
11 | 11 | ||
12 | , TorrentLoc(..), Progress(..) | 12 | , TorrentLoc(..), TorrentMap, Progress(..) |
13 | , ThreadCount, SessionCount | 13 | , ThreadCount, SessionCount |
14 | 14 | ||
15 | , ClientSession( clientPeerId, allowedExtensions ) | 15 | , ClientSession( clientPeerId, allowedExtensions ) |
16 | , withDefaultClient, defaultThreadCount | 16 | , withDefaultClient, defaultThreadCount, defaultPorts |
17 | , addTorrent | 17 | , addTorrent |
18 | , removeTorrent | 18 | , removeTorrent |
19 | 19 | ||
@@ -21,6 +21,8 @@ module Network.BitTorrent | |||
21 | , getPeerCount | 21 | , getPeerCount |
22 | , getSwarmCount | 22 | , getSwarmCount |
23 | , getSessionCount | 23 | , getSessionCount |
24 | , getSwarm | ||
25 | , getStorage | ||
24 | 26 | ||
25 | -- * Extensions | 27 | -- * Extensions |
26 | , Extension | 28 | , Extension |
@@ -33,6 +35,7 @@ import Data.Torrent | |||
33 | import Network.BitTorrent.Sessions.Types | 35 | import Network.BitTorrent.Sessions.Types |
34 | import Network.BitTorrent.Sessions | 36 | import Network.BitTorrent.Sessions |
35 | import Network.BitTorrent.Extension | 37 | import Network.BitTorrent.Extension |
38 | import Network.BitTorrent.Tracker | ||
36 | 39 | ||
37 | -- TODO remove fork from Network.BitTorrent.Exchange | 40 | -- TODO remove fork from Network.BitTorrent.Exchange |
38 | -- TODO make all forks in Internal. | 41 | -- TODO make all forks in Internal. |
@@ -46,22 +49,11 @@ withDefaultClient listPort dhtPort action = do | |||
46 | Torrent management | 49 | Torrent management |
47 | -----------------------------------------------------------------------} | 50 | -----------------------------------------------------------------------} |
48 | 51 | ||
49 | -- | Used to check torrent location before register torrent. | ||
50 | validateLocation :: TorrentLoc -> IO Torrent | ||
51 | validateLocation TorrentLoc {..} = do | ||
52 | t <- fromFile metafilePath | ||
53 | -- exists <- doesDirectoryExist dataDirPath | ||
54 | -- unless exists $ do | ||
55 | -- throw undefined | ||
56 | return t | ||
57 | |||
58 | |||
59 | -- | Register torrent and start downloading. | 52 | -- | Register torrent and start downloading. |
60 | addTorrent :: ClientSession -> TorrentLoc -> IO () | 53 | addTorrent :: ClientSession -> TorrentLoc -> IO () |
61 | addTorrent clientSession loc @ TorrentLoc {..} = do | 54 | addTorrent cs loc @ TorrentLoc {..} = do |
62 | torrent <- validateLocation loc | 55 | registerTorrent cs loc |
63 | -- registerTorrent loc tInfoHash | 56 | openSwarmSession cs loc |
64 | -- when (bf is not full) | ||
65 | return () | 57 | return () |
66 | 58 | ||
67 | -- | Unregister torrent and stop all running sessions. | 59 | -- | Unregister torrent and stop all running sessions. |
@@ -73,4 +65,4 @@ removeTorrent ses loc = undefined -- atomically $ unregisterTorrent ses loc | |||
73 | -- content files. | 65 | -- content files. |
74 | deleteTorrent :: ClientSession -> TorrentLoc -> IO () | 66 | deleteTorrent :: ClientSession -> TorrentLoc -> IO () |
75 | deleteTorrent ClientSession {..} TorrentLoc {..} = undefined | 67 | deleteTorrent ClientSession {..} TorrentLoc {..} = undefined |
76 | -} \ No newline at end of file | 68 | -} |
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs index b2d639f6..dc1b2752 100644 --- a/src/Network/BitTorrent/Exchange.hs +++ b/src/Network/BitTorrent/Exchange.hs | |||
@@ -464,16 +464,15 @@ exchange :: Storage -> P2P () | |||
464 | exchange storage = {-# SCC exchange #-} awaitEvent >>= handler | 464 | exchange storage = {-# SCC exchange #-} awaitEvent >>= handler |
465 | where | 465 | where |
466 | handler (Available bf) = do | 466 | handler (Available bf) = do |
467 | liftIO $ print (completeness bf) | ||
468 | ixs <- selBlk (findMin bf) storage | 467 | ixs <- selBlk (findMin bf) storage |
469 | mapM_ (yieldEvent . Want) ixs -- TODO yield vectored | 468 | mapM_ (yieldEvent . Want) ixs -- TODO yield vectored |
470 | 469 | ||
471 | handler (Want bix) = do | 470 | handler (Want bix) = do |
471 | liftIO $ print bix | ||
472 | blk <- liftIO $ getBlk bix storage | 472 | blk <- liftIO $ getBlk bix storage |
473 | yieldEvent (Fragment blk) | 473 | yieldEvent (Fragment blk) |
474 | 474 | ||
475 | handler (Fragment blk @ Block {..}) = do | 475 | handler (Fragment blk @ Block {..}) = do |
476 | liftIO $ print (ppBlock blk) | ||
477 | done <- liftIO $ putBlk blk storage | 476 | done <- liftIO $ putBlk blk storage |
478 | when done $ do | 477 | when done $ do |
479 | yieldEvent $ Available $ singleton blkPiece (succ blkPiece) | 478 | yieldEvent $ Available $ singleton blkPiece (succ blkPiece) |
diff --git a/src/Network/BitTorrent/Sessions.hs b/src/Network/BitTorrent/Sessions.hs index 6d99213a..43a34df9 100644 --- a/src/Network/BitTorrent/Sessions.hs +++ b/src/Network/BitTorrent/Sessions.hs | |||
@@ -26,11 +26,13 @@ module Network.BitTorrent.Sessions | |||
26 | , TorrentLoc(..) | 26 | , TorrentLoc(..) |
27 | , registerTorrent | 27 | , registerTorrent |
28 | , unregisterTorrent | 28 | , unregisterTorrent |
29 | , getRegistered | ||
29 | 30 | ||
30 | , getCurrentProgress | 31 | , getCurrentProgress |
31 | , getSwarmCount | 32 | , getSwarmCount |
32 | , getPeerCount | 33 | , getPeerCount |
33 | , getSwarm | 34 | , getSwarm |
35 | , getStorage | ||
34 | , openSwarmSession | 36 | , openSwarmSession |
35 | 37 | ||
36 | -- * Swarm | 38 | -- * Swarm |
@@ -199,9 +201,7 @@ discover swarm @ SwarmSession {..} = {-# SCC discover #-} do | |||
199 | withTracker progress conn $ \tses -> do | 201 | withTracker progress conn $ \tses -> do |
200 | forever $ do | 202 | forever $ do |
201 | addr <- getPeerAddr tses | 203 | addr <- getPeerAddr tses |
202 | print addr | ||
203 | forkThrottle swarm $ do | 204 | forkThrottle swarm $ do |
204 | print addr | ||
205 | initiatePeerSession swarm addr $ \conn -> do | 205 | initiatePeerSession swarm addr $ \conn -> do |
206 | print addr | 206 | print addr |
207 | runP2P conn p2p | 207 | runP2P conn p2p |
@@ -252,6 +252,10 @@ getSwarm cs @ ClientSession {..} ih = do | |||
252 | Active sw -> return sw | 252 | Active sw -> return sw |
253 | Registered loc -> openSwarmSession cs loc | 253 | Registered loc -> openSwarmSession cs loc |
254 | 254 | ||
255 | -- TODO do not spawn session! | ||
256 | getStorage :: ClientSession -> InfoHash -> IO Storage | ||
257 | getStorage cs ih = storage <$> getSwarm cs ih | ||
258 | |||
255 | -- | Get the number of connected peers in the given swarm. | 259 | -- | Get the number of connected peers in the given swarm. |
256 | getSessionCount :: SwarmSession -> IO SessionCount | 260 | getSessionCount :: SwarmSession -> IO SessionCount |
257 | getSessionCount SwarmSession {..} = do | 261 | getSessionCount SwarmSession {..} = do |
@@ -294,12 +298,17 @@ forkThrottle se action = do | |||
294 | validateLocation :: TorrentLoc -> IO Torrent | 298 | validateLocation :: TorrentLoc -> IO Torrent |
295 | validateLocation = fromFile . metafilePath | 299 | validateLocation = fromFile . metafilePath |
296 | 300 | ||
297 | registerTorrent :: TVar TorrentMap -> TorrentLoc -> IO () | 301 | registerTorrent :: ClientSession -> TorrentLoc -> IO () |
298 | registerTorrent = error "registerTorrent" | 302 | registerTorrent ClientSession {..} loc @ TorrentLoc {..} = do |
303 | torrent <- fromFile metafilePath | ||
304 | atomically $ modifyTVar' torrentMap $ HM.insert (tInfoHash torrent) loc | ||
299 | 305 | ||
300 | unregisterTorrent :: TVar TorrentMap -> InfoHash -> IO () | 306 | unregisterTorrent :: TVar TorrentMap -> InfoHash -> IO () |
301 | unregisterTorrent = error "unregisterTorrent" | 307 | unregisterTorrent = error "unregisterTorrent" |
302 | 308 | ||
309 | getRegistered :: ClientSession -> IO TorrentMap | ||
310 | getRegistered ClientSession {..} = readTVarIO torrentMap | ||
311 | |||
303 | {----------------------------------------------------------------------- | 312 | {----------------------------------------------------------------------- |
304 | Peer session creation | 313 | Peer session creation |
305 | ------------------------------------------------------------------------ | 314 | ------------------------------------------------------------------------ |
@@ -368,11 +377,10 @@ runSession connector opener action = | |||
368 | -- | Used then the client want to connect to a peer. | 377 | -- | Used then the client want to connect to a peer. |
369 | initiatePeerSession :: SwarmSession -> PeerAddr -> Exchange -> IO () | 378 | initiatePeerSession :: SwarmSession -> PeerAddr -> Exchange -> IO () |
370 | initiatePeerSession ss @ SwarmSession {..} addr | 379 | initiatePeerSession ss @ SwarmSession {..} addr |
371 | = runSession (putStrLn ("trying to connect" ++ show addr) *> connectToPeer addr <* putStrLn "connected") initiated | 380 | = runSession (connectToPeer addr) initiated |
372 | where | 381 | where |
373 | initiated sock = do | 382 | initiated sock = do |
374 | phs <- handshake sock (swarmHandshake ss) | 383 | phs <- handshake sock (swarmHandshake ss) |
375 | putStrLn "handshaked" | ||
376 | ps <- openSession ss addr phs | 384 | ps <- openSession ss addr phs |
377 | return ps | 385 | return ps |
378 | 386 | ||
@@ -396,8 +404,6 @@ listener :: ClientSession -> Exchange -> PortNumber -> IO () | |||
396 | listener cs action serverPort = bracket openListener close loop | 404 | listener cs action serverPort = bracket openListener close loop |
397 | where | 405 | where |
398 | loop sock = forever $ handle isIOError $ do | 406 | loop sock = forever $ handle isIOError $ do |
399 | putStrLn "listen" | ||
400 | print =<< getSocketName sock | ||
401 | (conn, addr) <- accept sock | 407 | (conn, addr) <- accept sock |
402 | putStrLn "accepted" | 408 | putStrLn "accepted" |
403 | case addr of | 409 | case addr of |
@@ -413,5 +419,5 @@ listener cs action serverPort = bracket openListener close loop | |||
413 | openListener = do | 419 | openListener = do |
414 | sock <- socket AF_INET Stream =<< getProtocolNumber "tcp" | 420 | sock <- socket AF_INET Stream =<< getProtocolNumber "tcp" |
415 | bindSocket sock (SockAddrInet serverPort iNADDR_ANY) | 421 | bindSocket sock (SockAddrInet serverPort iNADDR_ANY) |
416 | listen sock 1 | 422 | listen sock maxListenQueue |
417 | return sock | 423 | return sock |
diff --git a/src/Network/BitTorrent/Sessions/Types.lhs b/src/Network/BitTorrent/Sessions/Types.lhs index f3a3e789..69411d4e 100644 --- a/src/Network/BitTorrent/Sessions/Types.lhs +++ b/src/Network/BitTorrent/Sessions/Types.lhs | |||
@@ -146,7 +146,7 @@ To avoid this we keep just *metainfo* about *metainfo*: | |||
146 | > -- | Full path to directory contating content files associated | 146 | > -- | Full path to directory contating content files associated |
147 | > -- with the metafile. | 147 | > -- with the metafile. |
148 | > , dataDirPath :: FilePath | 148 | > , dataDirPath :: FilePath |
149 | > } | 149 | > } deriving Show |
150 | 150 | ||
151 | TorrentMap is used to keep track all known torrents for the | 151 | TorrentMap is used to keep track all known torrents for the |
152 | client. When some peer trying to connect to us it's necessary to | 152 | client. When some peer trying to connect to us it's necessary to |