summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-14 15:50:23 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-14 15:50:23 +0400
commit9ee6f55b2aa3df52f8bdb9a53759644e4fd14694 (patch)
tree0004d2e492c029fbbd04fa24ddcde4aedaf18fdb /src/Network
parent07ac8270807140fac201b7c973e12f924ca4b36b (diff)
+ Add docs to progress and client session.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent.hs34
-rw-r--r--src/Network/BitTorrent/Exchange.hs16
-rw-r--r--src/Network/BitTorrent/Internal.hs113
3 files changed, 125 insertions, 38 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index ce9f0149..8a8879bb 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -11,10 +11,22 @@ module Network.BitTorrent
11 module Data.Torrent 11 module Data.Torrent
12 12
13 -- * Session 13 -- * Session
14 , ClientSession 14 -- ** Client
15 , newClient, defaultClient 15 , ClientSession( clientPeerID, allowedExtensions )
16 16
17 , SwarmSession 17 , ThreadCount
18 , defaultThreadCount
19
20 , newClient
21 , defaultClient
22
23 , getCurrentProgress
24 , getPeerCount
25 , getSwarmCount
26
27
28 -- ** Swarm
29 , SwarmSession(torrentMeta)
18 , newLeacher, newSeeder 30 , newLeacher, newSeeder
19 , getSessionCount 31 , getSessionCount
20 32
@@ -22,13 +34,22 @@ module Network.BitTorrent
22 , discover 34 , discover
23 35
24 -- * Peer to Peer 36 -- * Peer to Peer
25 , P2P
26 , Event(..)
27 , PeerSession ( connectedPeerAddr, enabledExtensions ) 37 , PeerSession ( connectedPeerAddr, enabledExtensions )
28 , Block(..), BlockIx(..), ppBlock, ppBlockIx 38 , P2P
39
40 -- ** Transfer
41 , Block(..), ppBlock
42 , BlockIx(..), ppBlockIx
29 43
44 -- ** Control
45 , disconnect
46 , protocolError
47
48 -- ** Events
49 , Event(..)
30 , awaitEvent, yieldEvent 50 , awaitEvent, yieldEvent
31 51
52 -- * Extensions
32 , Extension, defaultExtensions, ppExtension 53 , Extension, defaultExtensions, ppExtension
33 ) where 54 ) where
34 55
@@ -48,6 +69,7 @@ import Network.BitTorrent.Extension
48import Network.BitTorrent.Peer 69import Network.BitTorrent.Peer
49 70
50 71
72-- | Client session with default parameters. Use it for testing only.
51defaultClient :: IO ClientSession 73defaultClient :: IO ClientSession
52defaultClient = newClient defaultThreadCount defaultExtensions 74defaultClient = newClient defaultThreadCount defaultExtensions
53 75
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs
index 862611f9..98b19357 100644
--- a/src/Network/BitTorrent/Exchange.hs
+++ b/src/Network/BitTorrent/Exchange.hs
@@ -20,6 +20,8 @@ module Network.BitTorrent.Exchange
20 , P2P 20 , P2P
21 , runP2P, spawnP2P 21 , runP2P, spawnP2P
22 , awaitEvent, yieldEvent 22 , awaitEvent, yieldEvent
23
24 , disconnect, protocolError
23 ) where 25 ) where
24 26
25import Control.Applicative 27import Control.Applicative
@@ -181,13 +183,13 @@ requireExtension required = do
181-----------------------------------------------------------------------} 183-----------------------------------------------------------------------}
182 184
183-- | 185-- |
184-- +----------+---------+ 186-- > +----------+---------+
185-- | Leacher | Seeder | 187-- > | Leacher | Seeder |
186-- |----------+---------+ 188-- > |----------+---------+
187-- | Available| | 189-- > | Available| |
188-- | Want | Want | 190-- > | Want | Want |
189-- | Fragment | | 191-- > | Fragment | |
190-- +----------+---------+ 192-- > +----------+---------+
191-- 193--
192-- 194--
193-- properties: 195-- properties:
diff --git a/src/Network/BitTorrent/Internal.hs b/src/Network/BitTorrent/Internal.hs
index db84e879..e07698dd 100644
--- a/src/Network/BitTorrent/Internal.hs
+++ b/src/Network/BitTorrent/Internal.hs
@@ -27,9 +27,16 @@ module Network.BitTorrent.Internal
27 27
28 -- * Client 28 -- * Client
29 , ClientSession (clientPeerID, allowedExtensions) 29 , ClientSession (clientPeerID, allowedExtensions)
30 , newClient, getCurrentProgress
31 30
32 , ThreadCount, defaultThreadCount 31 , ThreadCount
32 , defaultThreadCount
33
34 , newClient
35
36 , getCurrentProgress
37 , getSwarmCount
38 , getPeerCount
39
33 40
34 -- * Swarm 41 -- * Swarm
35 , SwarmSession(SwarmSession, torrentMeta, clientSession) 42 , SwarmSession(SwarmSession, torrentMeta, clientSession)
@@ -95,52 +102,93 @@ import Network.BitTorrent.Tracker.Protocol as BT
95-----------------------------------------------------------------------} 102-----------------------------------------------------------------------}
96 103
97-- | 'Progress' contains upload/download/left stats about 104-- | 'Progress' contains upload/download/left stats about
98-- current client state. 105-- current client state and used to notify the tracker
99-- 106--
100-- This data is considered as dynamic within one session. 107-- This data is considered as dynamic within one client
108-- session. This data also should be shared across client
109-- application sessions (e.g. files), otherwise use 'startProgress'
110-- to get initial 'Progress'.
101-- 111--
102data Progress = Progress { 112data Progress = Progress {
103 prUploaded :: Integer -- ^ Total amount of bytes uploaded. 113 prUploaded :: !Integer -- ^ Total amount of bytes uploaded.
104 , prDownloaded :: Integer -- ^ Total amount of bytes downloaded. 114 , prDownloaded :: !Integer -- ^ Total amount of bytes downloaded.
105 , prLeft :: Integer -- ^ Total amount of bytes left. 115 , prLeft :: !Integer -- ^ Total amount of bytes left.
106 } deriving Show 116 } deriving (Show, Read, Eq)
107 117
118-- TODO make lenses
119
120-- | Initial progress is used when there are no session before.
121--
122-- Please note that tracker might penalize client some way if the do
123-- not accumulate progress. If possible and save 'Progress' between
124-- client sessions to avoid that.
125--
108startProgress :: Integer -> Progress 126startProgress :: Integer -> Progress
109startProgress = Progress 0 0 127startProgress = Progress 0 0
110 128
111
112{----------------------------------------------------------------------- 129{-----------------------------------------------------------------------
113 Client session 130 Client session
114-----------------------------------------------------------------------} 131-----------------------------------------------------------------------}
115 132
116-- TODO comment thread count bounding 133{- NOTE: If we will not restrict number of threads we could end up
134with thousands of connected swarm and make no particular progress.
135
136Note also we do not bound number of swarms! This is not optimal
137strategy because each swarm might have say 1 thread and we could end
138up bounded by the meaningless limit. Bounding global number of p2p
139sessions should work better, and simpler.-}
140
141-- | Each client might have a limited number of threads.
117type ThreadCount = Int 142type ThreadCount = Int
118 143
144-- | The number of threads suitable for a typical BT client.
119defaultThreadCount :: ThreadCount 145defaultThreadCount :: ThreadCount
120defaultThreadCount = 1000 146defaultThreadCount = 1000
121 147
122-- | In one application we could have many clients with difference 148{- NOTE: basically, client session should contain options which user
123-- ID's and different enabled extensions. 149app store in configuration files. (related to the protocol) Moreover
150it should contain the all client identification info. (e.g. DHT) -}
151
152-- | Client session is the basic unit of bittorrent network, it has:
153--
154-- * The /peer ID/ used as unique identifier of the client in
155-- network. Obviously, this value is not changed during client
156-- session.
157--
158-- * The number of /protocol extensions/ it might use. This value
159-- is static as well, but if you want to dynamically reconfigure
160-- the client you might kill the end the current session and
161-- create a new with the fresh required extensions.
162--
163-- * The number of /swarms/ to join, each swarm described by the
164-- 'SwarmSession'.
165--
166-- Normally, you would have one client session, however, if we need,
167-- in one application we could have many clients with different peer
168-- ID's and different enabled extensions at the same time.
169--
124data ClientSession = ClientSession { 170data ClientSession = ClientSession {
125 -- | Our peer ID used in handshaked and discovery mechanism. The 171 -- | Used in handshakes and discovery mechanism.
126 -- clientPeerID is unique 'ClientSession' identifier.
127 clientPeerID :: !PeerID 172 clientPeerID :: !PeerID
128 173
129 -- | Extensions we should try to use. Hovewer some particular peer 174 -- | Extensions we should try to use. Hovewer some particular peer
130 -- might not support some extension, so we keep enableExtension in 175 -- might not support some extension, so we keep enabledExtension in
131 -- 'PeerSession'. 176 -- 'PeerSession'.
132 , allowedExtensions :: [Extension] 177 , allowedExtensions :: [Extension]
133 178
134 -- | Semaphor used to bound number of active P2P sessions. 179 -- | Semaphor used to bound number of active P2P sessions.
135 , activeThreads :: MSem ThreadCount 180 , activeThreads :: !(MSem ThreadCount)
136 181
137 -- | Max number of active connections. 182 -- | Max number of active connections.
138 , maxActive :: ThreadCount 183 , maxActive :: !ThreadCount
139 184
140 , swarmSessions :: TVar (Set SwarmSession) 185 -- | Used to traverse the swarm session.
186 , swarmSessions :: !(TVar (Set SwarmSession))
141 187
142 , eventManager :: EventManager 188 , eventManager :: !EventManager
143 , currentProgress :: TVar Progress 189
190 -- | Used to keep track global client progress.
191 , currentProgress :: !(TVar Progress)
144 } 192 }
145 193
146instance Eq ClientSession where 194instance Eq ClientSession where
@@ -149,12 +197,27 @@ instance Eq ClientSession where
149instance Ord ClientSession where 197instance Ord ClientSession where
150 compare = comparing clientPeerID 198 compare = comparing clientPeerID
151 199
200-- | Get current global progress of the client. This value is usually
201-- shown to a user.
152getCurrentProgress :: MonadIO m => ClientSession -> m Progress 202getCurrentProgress :: MonadIO m => ClientSession -> m Progress
153getCurrentProgress = liftIO . readTVarIO . currentProgress 203getCurrentProgress = liftIO . readTVarIO . currentProgress
154 204
155newClient :: ThreadCount -- ^ Maximum count of active P2P Sessions. 205-- | Get number of swarms client aware of.
206getSwarmCount :: MonadIO m => ClientSession -> m SessionCount
207getSwarmCount ClientSession {..} = liftIO $
208 S.size <$> readTVarIO swarmSessions
209
210-- | Get number of peers the client currently connected to.
211getPeerCount :: MonadIO m => ClientSession -> m ThreadCount
212getPeerCount ClientSession {..} = liftIO $ do
213 unused <- peekAvail activeThreads
214 return (maxActive - unused)
215
216-- | Create a new client session. The data passed to this function are
217-- usually loaded from configuration file.
218newClient :: SessionCount -- ^ Maximum count of active P2P Sessions.
156 -> [Extension] -- ^ Extensions allowed to use. 219 -> [Extension] -- ^ Extensions allowed to use.
157 -> IO ClientSession 220 -> IO ClientSession -- ^ Client with unique peer ID.
158 221
159newClient n exts = do 222newClient n exts = do
160 mgr <- Ev.new 223 mgr <- Ev.new
@@ -183,11 +246,11 @@ defSeederConns = defaultUnchokeSlots
183defLeacherConns :: SessionCount 246defLeacherConns :: SessionCount
184defLeacherConns = defaultNumWant 247defLeacherConns = defaultNumWant
185 248
186 249-- | Swarm session is
187-- | Extensions are set globally by
188-- Swarm session are un
189data SwarmSession = SwarmSession { 250data SwarmSession = SwarmSession {
190 torrentMeta :: !Torrent 251 torrentMeta :: !Torrent
252
253 -- |
191 , clientSession :: !ClientSession 254 , clientSession :: !ClientSession
192 255
193 -- | Represent count of peers we _currently_ can connect to in the 256 -- | Represent count of peers we _currently_ can connect to in the