summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange/Block.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-02-25 17:27:34 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-02-25 17:27:34 +0400
commit22d6d17dc8b1d864ad0752f052bf89f622ada2de (patch)
tree3d64a37bcab016570cba2e1c8c91858c1f85f41d /src/Network/BitTorrent/Exchange/Block.hs
parent627b44ab54111322eeb273de87c6d8a24d7f37c1 (diff)
Implement spans function
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Block.hs')
-rw-r--r--src/Network/BitTorrent/Exchange/Block.hs20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/Exchange/Block.hs b/src/Network/BitTorrent/Exchange/Block.hs
index 32e4f99f..4f49c6c9 100644
--- a/src/Network/BitTorrent/Exchange/Block.hs
+++ b/src/Network/BitTorrent/Exchange/Block.hs
@@ -284,9 +284,23 @@ size bkt = go bkt
284 go (Span sz xs) = sz + go xs 284 go (Span sz xs) = sz + go xs
285 go (Fill sz _ xs) = sz + go xs 285 go (Fill sz _ xs) = sz + go xs
286 286
287-- | /O(n)/. List incomplete blocks to download. 287-- | /O(n)/. List incomplete blocks to download. If some block have
288-- size more than the specified 'BlockSize' then block is split into
289-- smaller blocks to satisfy given 'BlockSize'. Small (for
290-- e.g. trailing) blocks is not ignored, but returned in-order.
288spans :: BlockSize -> Bucket -> [(BlockOffset, BlockSize)] 291spans :: BlockSize -> Bucket -> [(BlockOffset, BlockSize)]
289spans = undefined 292spans expectedSize = go 0
293 where
294 go _ Nil = []
295 go off (Span sz xs) = listChunks off sz ++ go (off + sz) xs
296 go off (Fill sz _ xs) = go (off + sz) xs
297
298 listChunks off restSize
299 | restSize <= 0 = []
300 | otherwise = (off, blkSize)
301 : listChunks (off + blkSize) (restSize - blkSize)
302 where
303 blkSize = min expectedSize restSize
290 304
291{----------------------------------------------------------------------- 305{-----------------------------------------------------------------------
292-- Bucket contstruction 306-- Bucket contstruction
@@ -336,7 +350,7 @@ insertLazy pos bl = Network.BitTorrent.Exchange.Block.insert pos (BL.toStrict bl
336 350
337-- | /O(n)/. 351-- | /O(n)/.
338merge :: Bucket -> Bucket -> Bucket 352merge :: Bucket -> Bucket -> Bucket
339merge = undefined 353merge = error "Bucket.merge: not implemented"
340 354
341-- | /O(1)/. 355-- | /O(1)/.
342toPiece :: Bucket -> Maybe BL.ByteString 356toPiece :: Bucket -> Maybe BL.ByteString