summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-10-31 19:17:08 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-10-31 19:17:08 +0400
commit0853bc857b370ed5f7a453a18a7b03b28cd3b410 (patch)
tree0437b785e9f91d53dfda170bdb48a9cd395d97da /src
parente3b584173f9a58c4c662ec5e933e97c09334910e (diff)
Introduce PieceCount newtype
Diffstat (limited to 'src')
-rw-r--r--src/Data/Torrent/Block.hs1
-rw-r--r--src/Data/Torrent/Piece.hs29
2 files changed, 15 insertions, 15 deletions
diff --git a/src/Data/Torrent/Block.hs b/src/Data/Torrent/Block.hs
index e0507aed..ca3bef45 100644
--- a/src/Data/Torrent/Block.hs
+++ b/src/Data/Torrent/Block.hs
@@ -56,6 +56,7 @@ newtype BlockSize = BlockSize { unBlockSize :: Int }
56-- | Widely used semi-official block size. 56-- | Widely used semi-official block size.
57instance Default BlockSize where 57instance Default BlockSize where
58 def = 16 * 1024 58 def = 16 * 1024
59 {-# INLINE def #-}
59 60
60type BlockLIx = Int 61type BlockLIx = Int
61type PieceLIx = Int 62type PieceLIx = Int
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs
index 58bc9ebc..96624729 100644
--- a/src/Data/Torrent/Piece.hs
+++ b/src/Data/Torrent/Piece.hs
@@ -12,14 +12,11 @@
12{-# LANGUAGE GeneralizedNewtypeDeriving #-} 12{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13module Data.Torrent.Piece 13module Data.Torrent.Piece
14 ( -- * Piece attributes 14 ( -- * Piece attributes
15 -- ** Piece size 15 PieceIx
16 PieceSize (..) 16 , PieceCount
17 , optimalPieceCount 17 , PieceSize (..)
18 , defaultPieceSize 18 , defaultPieceSize
19 19
20 -- ** Piece index
21 , PieceIx
22
23 -- * Piece data 20 -- * Piece data
24 , Piece (..) 21 , Piece (..)
25 , ppPiece 22 , ppPiece
@@ -33,7 +30,6 @@ module Data.Torrent.Piece
33 , pieceCount 30 , pieceCount
34 , checkPieceLazy 31 , checkPieceLazy
35 32
36
37 -- * Internal 33 -- * Internal
38 , getPieceInfo 34 , getPieceInfo
39 , putPieceInfo 35 , putPieceInfo
@@ -65,9 +61,15 @@ import Data.Torrent.Block
65class Lint a where 61class Lint a where
66 lint :: a -> Either String a 62 lint :: a -> Either String a
67 63
68type PieceCount = Int -- TODO newtype
69type PieceIx = Int -- TODO remove 64type PieceIx = Int -- TODO remove
70 65
66newtype PieceCount = PieceCount { unPieceCount :: Int }
67
68-- | TODO
69instance Default PieceCount where
70 def = PieceCount 1000
71 {-# INLINE def #-}
72
71newtype PieceIndex = PieceIndex Int 73newtype PieceIndex = PieceIndex Int
72 74
73-- | An int used to denote piece size. 75-- | An int used to denote piece size.
@@ -97,10 +99,6 @@ instance Bounded PieceSize where
97 minBound = PieceSize minPieceSize 99 minBound = PieceSize minPieceSize
98 {-# INLINE minBound #-} 100 {-# INLINE minBound #-}
99 101
100-- | TODO
101optimalPieceCount :: Int
102optimalPieceCount = 1000
103{-# INLINE optimalPieceCount #-}
104 102
105toPow2 :: Int -> Int 103toPow2 :: Int -> Int
106toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x) 104toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x)
@@ -109,7 +107,7 @@ toPow2 x = bit $ fromIntegral (leadingZeros (0 :: Int) - leadingZeros x)
109defaultPieceSize :: Int64 -> Int 107defaultPieceSize :: Int64 -> Int
110defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc 108defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc
111 where 109 where
112 pc = fromIntegral (x `div` fromIntegral optimalPieceCount) 110 pc = fromIntegral (x `div` fromIntegral (unPieceCount def))
113 111
114-- TODO check if pieceLength is power of 2 112-- TODO check if pieceLength is power of 2
115-- | Piece payload should be strict or lazy bytestring. 113-- | Piece payload should be strict or lazy bytestring.
@@ -201,10 +199,11 @@ pieceHash PieceInfo {..} i = slice (hashsize * i) hashsize (unHashArray piPieceH
201-- | Find count of pieces in the torrent. If torrent size is not a 199-- | Find count of pieces in the torrent. If torrent size is not a
202-- multiple of piece size then the count is rounded up. 200-- multiple of piece size then the count is rounded up.
203pieceCount :: PieceInfo -> PieceCount 201pieceCount :: PieceInfo -> PieceCount
204pieceCount PieceInfo {..} = BS.length (unHashArray piPieceHashes) `quot` hashsize 202pieceCount PieceInfo {..}
203 = PieceCount (BS.length (unHashArray piPieceHashes) `quot` hashsize)
205 204
206isLastPiece :: PieceInfo -> PieceIx -> Bool 205isLastPiece :: PieceInfo -> PieceIx -> Bool
207isLastPiece ci i = pieceCount ci == succ i 206isLastPiece ci i = unPieceCount (pieceCount ci) == succ i
208 207
209class Validation a where 208class Validation a where
210 validate :: PieceInfo -> Piece a -> Bool 209 validate :: PieceInfo -> Piece a -> Bool