diff options
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/Sessions.hs | 29 |
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 | |||
64 | import Control.Concurrent | 54 | import Control.Concurrent |
65 | import Control.Concurrent.STM | 55 | import Control.Concurrent.STM |
66 | import Control.Concurrent.MSem as MSem | 56 | import Control.Concurrent.MSem as MSem |
67 | import Control.Lens | ||
68 | import Control.Monad (when, forever, (>=>)) | 57 | import Control.Monad (when, forever, (>=>)) |
69 | import Control.Exception | 58 | import Control.Exception |
70 | import Control.Monad.Trans | 59 | import Control.Monad.Trans |
71 | 60 | ||
72 | import Data.IORef | 61 | import Data.IORef |
73 | import Data.Foldable (mapM_) | ||
74 | import Data.Map as M | 62 | import Data.Map as M |
75 | import Data.HashMap.Strict as HM | 63 | import Data.HashMap.Strict as HM |
76 | import Data.Set as S | 64 | import Data.Set as S |
@@ -265,10 +253,10 @@ unregisterSwarmSession SwarmSession {..} = | |||
265 | 253 | ||
266 | getSwarm :: ClientSession -> InfoHash -> IO SwarmSession | 254 | getSwarm :: ClientSession -> InfoHash -> IO SwarmSession |
267 | getSwarm cs @ ClientSession {..} ih = do | 255 | getSwarm 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 | ||
273 | newSeeder :: ClientSession -> Torrent -> IO SwarmSession | 261 | newSeeder :: ClientSession -> Torrent -> IO SwarmSession |
274 | newSeeder cs t @ Torrent {..} | 262 | newSeeder cs t @ Torrent {..} |
@@ -361,6 +349,14 @@ TODO: utilize peer Id. | |||
361 | TODO: use STM semaphore | 349 | TODO: use STM semaphore |
362 | -----------------------------------------------------------------------} | 350 | -----------------------------------------------------------------------} |
363 | 351 | ||
352 | registerPeerSession :: PeerSession -> IO () | ||
353 | registerPeerSession ps @ PeerSession {..} = | ||
354 | atomically $ modifyTVar' (connectedPeers swarmSession) (S.insert ps) | ||
355 | |||
356 | unregisterPeerSession :: PeerSession -> IO () | ||
357 | unregisterPeerSession ps @ PeerSession {..} = | ||
358 | atomically $ modifyTVar' (connectedPeers swarmSession) (S.delete ps) | ||
359 | |||
364 | openSession :: SwarmSession -> PeerAddr -> Handshake -> IO PeerSession | 360 | openSession :: SwarmSession -> PeerAddr -> Handshake -> IO PeerSession |
365 | openSession ss @ SwarmSession {..} addr Handshake {..} = do | 361 | openSession 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 | ||
378 | closeSession :: PeerSession -> IO () | 374 | closeSession :: PeerSession -> IO () |
379 | closeSession ps @ PeerSession {..} = do | 375 | closeSession = unregisterPeerSession |
380 | atomically $ modifyTVar' (connectedPeers swarmSession) (S.delete ps) | ||
381 | 376 | ||
382 | type PeerConn = (Socket, PeerSession) | 377 | type PeerConn = (Socket, PeerSession) |
383 | type Exchange = PeerConn -> IO () | 378 | type Exchange = PeerConn -> IO () |