summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Sessions.hs29
1 files changed, 12 insertions, 17 deletions
diff --git a/src/Network/BitTorrent/Sessions.hs b/src/Network/BitTorrent/Sessions.hs
index da57ec3b..2e51fde6 100644
--- a/src/Network/BitTorrent/Sessions.hs
+++ b/src/Network/BitTorrent/Sessions.hs
@@ -43,16 +43,6 @@ module Network.BitTorrent.Sessions
43 , newSeeder 43 , newSeeder
44 , getClientBitfield 44 , getClientBitfield
45 45
46 -- * Peer
47 , PeerSession( PeerSession, connectedPeerAddr
48 , swarmSession, enabledExtensions
49 , sessionState
50 )
51 , SessionState
52 , initiatePeerSession
53 , acceptPeerSession
54 , listener
55
56 -- * Timeouts 46 -- * Timeouts
57 , updateIncoming, updateOutcoming 47 , updateIncoming, updateOutcoming
58 , discover 48 , discover
@@ -64,13 +54,11 @@ import Control.Applicative
64import Control.Concurrent 54import Control.Concurrent
65import Control.Concurrent.STM 55import Control.Concurrent.STM
66import Control.Concurrent.MSem as MSem 56import Control.Concurrent.MSem as MSem
67import Control.Lens
68import Control.Monad (when, forever, (>=>)) 57import Control.Monad (when, forever, (>=>))
69import Control.Exception 58import Control.Exception
70import Control.Monad.Trans 59import Control.Monad.Trans
71 60
72import Data.IORef 61import Data.IORef
73import Data.Foldable (mapM_)
74import Data.Map as M 62import Data.Map as M
75import Data.HashMap.Strict as HM 63import Data.HashMap.Strict as HM
76import Data.Set as S 64import Data.Set as S
@@ -265,10 +253,10 @@ unregisterSwarmSession SwarmSession {..} =
265 253
266getSwarm :: ClientSession -> InfoHash -> IO SwarmSession 254getSwarm :: ClientSession -> InfoHash -> IO SwarmSession
267getSwarm cs @ ClientSession {..} ih = do 255getSwarm cs @ ClientSession {..} ih = do
268 ss <- readTVarIO $ swarmSessions 256 ss <- readTVarIO swarmSessions
269 case M.lookup ih ss of 257 case M.lookup ih ss of
270 Just sw -> return sw 258 Just sw -> return sw
271 Nothing -> undefined -- openSwarm cs 259 Nothing -> undefined -- openSwarmSession cs
272 260
273newSeeder :: ClientSession -> Torrent -> IO SwarmSession 261newSeeder :: ClientSession -> Torrent -> IO SwarmSession
274newSeeder cs t @ Torrent {..} 262newSeeder cs t @ Torrent {..}
@@ -361,6 +349,14 @@ TODO: utilize peer Id.
361TODO: use STM semaphore 349TODO: use STM semaphore
362-----------------------------------------------------------------------} 350-----------------------------------------------------------------------}
363 351
352registerPeerSession :: PeerSession -> IO ()
353registerPeerSession ps @ PeerSession {..} =
354 atomically $ modifyTVar' (connectedPeers swarmSession) (S.insert ps)
355
356unregisterPeerSession :: PeerSession -> IO ()
357unregisterPeerSession ps @ PeerSession {..} =
358 atomically $ modifyTVar' (connectedPeers swarmSession) (S.delete ps)
359
364openSession :: SwarmSession -> PeerAddr -> Handshake -> IO PeerSession 360openSession :: SwarmSession -> PeerAddr -> Handshake -> IO PeerSession
365openSession ss @ SwarmSession {..} addr Handshake {..} = do 361openSession ss @ SwarmSession {..} addr Handshake {..} = do
366 let clientCaps = encodeExts $ allowedExtensions $ clientSession 362 let clientCaps = encodeExts $ allowedExtensions $ clientSession
@@ -372,12 +368,11 @@ openSession ss @ SwarmSession {..} addr Handshake {..} = do
372 <*> (newIORef . initialSessionState . totalCount =<< readTVarIO clientBitfield) 368 <*> (newIORef . initialSessionState . totalCount =<< readTVarIO clientBitfield)
373 -- TODO we could implement more interesting throtling scheme 369 -- TODO we could implement more interesting throtling scheme
374 -- using connected peer information 370 -- using connected peer information
375 atomically $ modifyTVar' connectedPeers (S.insert ps) 371 registerPeerSession ps
376 return ps 372 return ps
377 373
378closeSession :: PeerSession -> IO () 374closeSession :: PeerSession -> IO ()
379closeSession ps @ PeerSession {..} = do 375closeSession = unregisterPeerSession
380 atomically $ modifyTVar' (connectedPeers swarmSession) (S.delete ps)
381 376
382type PeerConn = (Socket, PeerSession) 377type PeerConn = (Socket, PeerSession)
383type Exchange = PeerConn -> IO () 378type Exchange = PeerConn -> IO ()