summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Network/BitTorrent.hs26
-rw-r--r--src/Network/BitTorrent/Exchange.hs3
-rw-r--r--src/Network/BitTorrent/Sessions.hs24
-rw-r--r--src/Network/BitTorrent/Sessions/Types.lhs2
4 files changed, 26 insertions, 29 deletions
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 @@
9module Network.BitTorrent 9module 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
33import Network.BitTorrent.Sessions.Types 35import Network.BitTorrent.Sessions.Types
34import Network.BitTorrent.Sessions 36import Network.BitTorrent.Sessions
35import Network.BitTorrent.Extension 37import Network.BitTorrent.Extension
38import 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.
50validateLocation :: TorrentLoc -> IO Torrent
51validateLocation 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.
60addTorrent :: ClientSession -> TorrentLoc -> IO () 53addTorrent :: ClientSession -> TorrentLoc -> IO ()
61addTorrent clientSession loc @ TorrentLoc {..} = do 54addTorrent 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.
74deleteTorrent :: ClientSession -> TorrentLoc -> IO () 66deleteTorrent :: ClientSession -> TorrentLoc -> IO ()
75deleteTorrent ClientSession {..} TorrentLoc {..} = undefined 67deleteTorrent 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 ()
464exchange storage = {-# SCC exchange #-} awaitEvent >>= handler 464exchange 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!
256getStorage :: ClientSession -> InfoHash -> IO Storage
257getStorage 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.
256getSessionCount :: SwarmSession -> IO SessionCount 260getSessionCount :: SwarmSession -> IO SessionCount
257getSessionCount SwarmSession {..} = do 261getSessionCount SwarmSession {..} = do
@@ -294,12 +298,17 @@ forkThrottle se action = do
294validateLocation :: TorrentLoc -> IO Torrent 298validateLocation :: TorrentLoc -> IO Torrent
295validateLocation = fromFile . metafilePath 299validateLocation = fromFile . metafilePath
296 300
297registerTorrent :: TVar TorrentMap -> TorrentLoc -> IO () 301registerTorrent :: ClientSession -> TorrentLoc -> IO ()
298registerTorrent = error "registerTorrent" 302registerTorrent ClientSession {..} loc @ TorrentLoc {..} = do
303 torrent <- fromFile metafilePath
304 atomically $ modifyTVar' torrentMap $ HM.insert (tInfoHash torrent) loc
299 305
300unregisterTorrent :: TVar TorrentMap -> InfoHash -> IO () 306unregisterTorrent :: TVar TorrentMap -> InfoHash -> IO ()
301unregisterTorrent = error "unregisterTorrent" 307unregisterTorrent = error "unregisterTorrent"
302 308
309getRegistered :: ClientSession -> IO TorrentMap
310getRegistered 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.
369initiatePeerSession :: SwarmSession -> PeerAddr -> Exchange -> IO () 378initiatePeerSession :: SwarmSession -> PeerAddr -> Exchange -> IO ()
370initiatePeerSession ss @ SwarmSession {..} addr 379initiatePeerSession 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 ()
396listener cs action serverPort = bracket openListener close loop 404listener 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
151TorrentMap is used to keep track all known torrents for the 151TorrentMap is used to keep track all known torrents for the
152client. When some peer trying to connect to us it's necessary to 152client. When some peer trying to connect to us it's necessary to