diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Torrent.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent.hs | 72 | ||||
-rw-r--r-- | src/Network/BitTorrent/Sessions.hs | 29 |
3 files changed, 22 insertions, 82 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index a9efbfce..ae40bef4 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -71,7 +71,6 @@ import Data.Char | |||
71 | import Data.Foldable | 71 | import Data.Foldable |
72 | import Data.Map (Map) | 72 | import Data.Map (Map) |
73 | import qualified Data.Map as M | 73 | import qualified Data.Map as M |
74 | import Data.ByteString (ByteString) | ||
75 | import qualified Data.ByteString as B | 74 | import qualified Data.ByteString as B |
76 | import Data.ByteString.Internal | 75 | import Data.ByteString.Internal |
77 | import qualified Data.ByteString.Char8 as BC (pack, unpack) | 76 | import qualified Data.ByteString.Char8 as BC (pack, unpack) |
@@ -88,8 +87,6 @@ import Network.URI | |||
88 | import System.FilePath | 87 | import System.FilePath |
89 | import Numeric | 88 | import Numeric |
90 | 89 | ||
91 | import Debug.Trace | ||
92 | |||
93 | {----------------------------------------------------------------------- | 90 | {----------------------------------------------------------------------- |
94 | Info hash | 91 | Info hash |
95 | -----------------------------------------------------------------------} | 92 | -----------------------------------------------------------------------} |
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 7c1e02e2..8c3189d3 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -7,88 +7,36 @@ | |||
7 | -- | 7 | -- |
8 | {-# LANGUAGE RecordWildCards #-} | 8 | {-# LANGUAGE RecordWildCards #-} |
9 | module Network.BitTorrent | 9 | module Network.BitTorrent |
10 | ( | 10 | ( module Data.Torrent |
11 | module Data.Torrent | ||
12 | 11 | ||
13 | -- * Session | 12 | , TorrentLoc(..), Progress(..) |
14 | , ThreadCount | 13 | , ThreadCount, SessionCount |
15 | , defaultThreadCount | ||
16 | 14 | ||
17 | -- ** Client | ||
18 | , ClientSession( clientPeerId, allowedExtensions ) | 15 | , ClientSession( clientPeerId, allowedExtensions ) |
16 | , withDefaultClient, defaultThreadCount | ||
17 | , addTorrent | ||
18 | , removeTorrent | ||
19 | 19 | ||
20 | , withDefaultClient | ||
21 | |||
22 | , Progress(..) | ||
23 | , getCurrentProgress | 20 | , getCurrentProgress |
24 | , getPeerCount | 21 | , getPeerCount |
25 | , getSwarmCount | 22 | , getSwarmCount |
26 | |||
27 | , TorrentLoc(..) | ||
28 | , addTorrent | ||
29 | , removeTorrent | ||
30 | |||
31 | -- ** Swarm | ||
32 | , SwarmSession(torrentMeta) | ||
33 | |||
34 | , newLeecher | ||
35 | , newSeeder | ||
36 | |||
37 | , SessionCount | ||
38 | , getSessionCount | 23 | , getSessionCount |
39 | 24 | ||
40 | -- * Discovery | ||
41 | , discover | ||
42 | , exchange | ||
43 | |||
44 | |||
45 | -- * Peer to Peer | ||
46 | , P2P | ||
47 | |||
48 | -- ** Session | ||
49 | , PeerSession( PeerSession, connectedPeerAddr | ||
50 | , swarmSession, enabledExtensions | ||
51 | ) | ||
52 | |||
53 | , getHaveCount | ||
54 | , getWantCount | ||
55 | , getPieceCount | ||
56 | |||
57 | |||
58 | -- ** Transfer | ||
59 | , Block(..), ppBlock | ||
60 | , BlockIx(..), ppBlockIx | ||
61 | |||
62 | -- ** Control | ||
63 | , SessionException | ||
64 | , disconnect | ||
65 | , protocolError | ||
66 | |||
67 | -- ** Events | ||
68 | , Event(..) | ||
69 | , awaitEvent, yieldEvent | ||
70 | |||
71 | -- * Extensions | 25 | -- * Extensions |
72 | , Extension, defaultExtensions, ppExtension | 26 | , Extension |
27 | , defaultExtensions | ||
28 | , ppExtension | ||
73 | ) where | 29 | ) where |
74 | 30 | ||
75 | import Control.Concurrent | 31 | import Control.Concurrent |
76 | import Control.Concurrent.STM | ||
77 | import Control.Exception | ||
78 | import Control.Monad | 32 | import Control.Monad |
79 | import Control.Monad.Reader | 33 | import Control.Monad.Trans |
80 | |||
81 | import Network | 34 | import Network |
82 | |||
83 | import Data.Bitfield as BF | ||
84 | import Data.Torrent | 35 | import Data.Torrent |
85 | import Network.BitTorrent.Sessions.Types | 36 | import Network.BitTorrent.Sessions.Types |
86 | import Network.BitTorrent.Sessions | 37 | import Network.BitTorrent.Sessions |
87 | import Network.BitTorrent.Peer | ||
88 | import Network.BitTorrent.Extension | 38 | import Network.BitTorrent.Extension |
89 | import Network.BitTorrent.Exchange | 39 | import Network.BitTorrent.Exchange |
90 | import Network.BitTorrent.Exchange.Protocol | ||
91 | import Network.BitTorrent.Tracker | ||
92 | 40 | ||
93 | import System.Torrent.Storage | 41 | import System.Torrent.Storage |
94 | 42 | ||
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 () |