summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent')
-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
3 files changed, 17 insertions, 12 deletions
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