summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Sessions.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Sessions.hs')
-rw-r--r--src/Network/BitTorrent/Sessions.hs24
1 files changed, 15 insertions, 9 deletions
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