summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent.hs3
-rw-r--r--src/Network/BitTorrent.hs72
-rw-r--r--src/Network/BitTorrent/Sessions.hs29
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
71import Data.Foldable 71import Data.Foldable
72import Data.Map (Map) 72import Data.Map (Map)
73import qualified Data.Map as M 73import qualified Data.Map as M
74import Data.ByteString (ByteString)
75import qualified Data.ByteString as B 74import qualified Data.ByteString as B
76import Data.ByteString.Internal 75import Data.ByteString.Internal
77import qualified Data.ByteString.Char8 as BC (pack, unpack) 76import qualified Data.ByteString.Char8 as BC (pack, unpack)
@@ -88,8 +87,6 @@ import Network.URI
88import System.FilePath 87import System.FilePath
89import Numeric 88import Numeric
90 89
91import 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 #-}
9module Network.BitTorrent 9module 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
75import Control.Concurrent 31import Control.Concurrent
76import Control.Concurrent.STM
77import Control.Exception
78import Control.Monad 32import Control.Monad
79import Control.Monad.Reader 33import Control.Monad.Trans
80
81import Network 34import Network
82
83import Data.Bitfield as BF
84import Data.Torrent 35import Data.Torrent
85import Network.BitTorrent.Sessions.Types 36import Network.BitTorrent.Sessions.Types
86import Network.BitTorrent.Sessions 37import Network.BitTorrent.Sessions
87import Network.BitTorrent.Peer
88import Network.BitTorrent.Extension 38import Network.BitTorrent.Extension
89import Network.BitTorrent.Exchange 39import Network.BitTorrent.Exchange
90import Network.BitTorrent.Exchange.Protocol
91import Network.BitTorrent.Tracker
92 40
93import System.Torrent.Storage 41import 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
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 ()