summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-06 23:51:34 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-06 23:51:34 +0400
commit05787c18b88db130e178b19aee09c21398a16256 (patch)
tree775f73d9a95db54ad54999c6c189215464d0740f /src/Network/BitTorrent
parentb32f635684e88a4c9c4b873336624cdad05eb3e1 (diff)
+ Add torrent registration.
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Internal.hs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Internal.hs b/src/Network/BitTorrent/Internal.hs
index bd20aa16..fffd4727 100644
--- a/src/Network/BitTorrent/Internal.hs
+++ b/src/Network/BitTorrent/Internal.hs
@@ -89,6 +89,7 @@ import Data.IORef
89import Data.Default 89import Data.Default
90import Data.Function 90import Data.Function
91import Data.Foldable (mapM_) 91import Data.Foldable (mapM_)
92import Data.HashMap.Strict as HM
92import Data.Ord 93import Data.Ord
93import Data.Set as S 94import Data.Set as S
94import Data.Typeable 95import Data.Typeable
@@ -182,6 +183,33 @@ type ThreadCount = Int
182defaultThreadCount :: ThreadCount 183defaultThreadCount :: ThreadCount
183defaultThreadCount = 1000 184defaultThreadCount = 1000
184 185
186{- PERFORMANCE NOTE: keeping torrent metafiles in memory is a _bad_
187idea: for 1TB of data we need at least 100MB of metadata. (using 256KB
188piece size). This solution do not scale further. Solution with
189TorrentLoc is much better and takes much more less space, moreover it
190depends on count of torrents but not on count of data itself. To scale
191further, in future we might add something like database (for
192e.g. sqlite) for this kind of things.-}
193
194-- | Identifies location of
195data TorrentLoc = TorrentLoc {
196 metafilePath :: FilePath
197 , dataPath :: FilePath
198 }
199
200validateTorrent :: TorrentLoc -> IO ()
201validateTorrent = error "validateTorrent: not implemented"
202
203-- | TorrentMap is used to keep track all known torrents for the
204-- client. When some peer trying to connect to us it's necessary to
205-- dispatch appropriate 'SwarmSession' (or start new one if there are
206-- none) in the listener loop: we only know 'InfoHash' from
207-- 'Handshake' but nothing more. So to accept new 'PeerSession' we
208-- need to lookup torrent metainfo and content files (if there are
209-- some) by the 'InfoHash' and only after that enter exchange loop.
210--
211type TorrentMap = HashMap InfoHash TorrentLoc
212
185{- NOTE: basically, client session should contain options which user 213{- NOTE: basically, client session should contain options which user
186app store in configuration files. (related to the protocol) Moreover 214app store in configuration files. (related to the protocol) Moreover
187it should contain the all client identification info. (e.g. DHT) -} 215it should contain the all client identification info. (e.g. DHT) -}
@@ -226,6 +254,9 @@ data ClientSession = ClientSession {
226 254
227 -- | Used to keep track global client progress. 255 -- | Used to keep track global client progress.
228 , currentProgress :: !(TVar Progress) 256 , currentProgress :: !(TVar Progress)
257
258 -- | Used to keep track available torrents.
259 , torrentMap :: !(TVar TorrentMap)
229 } 260 }
230 261
231-- currentProgress field is reduntant: progress depends on the all swarm bitfields 262-- currentProgress field is reduntant: progress depends on the all swarm bitfields
@@ -272,6 +303,15 @@ newClient n exts = do
272 <*> newTVarIO S.empty 303 <*> newTVarIO S.empty
273 <*> pure mgr 304 <*> pure mgr
274 <*> newTVarIO (startProgress 0) 305 <*> newTVarIO (startProgress 0)
306 <*> newTVarIO HM.empty
307
308registerTorrent :: ClientSession -> InfoHash -> TorrentLoc -> STM ()
309registerTorrent ClientSession {..} ih tl = do
310 modifyTVar' torrentMap $ HM.insert ih tl
311
312unregisterTorrent :: ClientSession -> InfoHash -> STM ()
313unregisterTorrent ClientSession {..} ih = do
314 modifyTVar' torrentMap $ HM.delete ih
275 315
276{----------------------------------------------------------------------- 316{-----------------------------------------------------------------------
277 Swarm session 317 Swarm session