diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-05 03:40:11 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-05 03:40:11 +0400 |
commit | aa86e6191cfdd0585808ae1f12355918996d3ee5 (patch) | |
tree | 94c22d905d28628a1b9de70f31ca0917e5cbf49c /src/Data/Torrent/Block.hs | |
parent | 32b0f3570237e4d4742fc8874980f2b479c1ae75 (diff) |
Move piece attributes to the Piece module
Diffstat (limited to 'src/Data/Torrent/Block.hs')
-rw-r--r-- | src/Data/Torrent/Block.hs | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/src/Data/Torrent/Block.hs b/src/Data/Torrent/Block.hs index 089217fa..88f7f352 100644 --- a/src/Data/Torrent/Block.hs +++ b/src/Data/Torrent/Block.hs | |||
@@ -13,12 +13,8 @@ | |||
13 | {-# LANGUAGE DeriveDataTypeable #-} | 13 | {-# LANGUAGE DeriveDataTypeable #-} |
14 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 14 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
15 | module Data.Torrent.Block | 15 | module Data.Torrent.Block |
16 | ( -- * Piece attributes | 16 | ( -- * Block attributes |
17 | PieceIx | 17 | BlockOffset |
18 | , PieceSize | ||
19 | |||
20 | -- * Block attributes | ||
21 | , BlockOffset | ||
22 | , BlockCount | 18 | , BlockCount |
23 | , BlockSize | 19 | , BlockSize |
24 | , defaultTransferSize | 20 | , defaultTransferSize |
@@ -32,6 +28,7 @@ module Data.Torrent.Block | |||
32 | , blockIx | 28 | , blockIx |
33 | , blockSize | 29 | , blockSize |
34 | , blockRange | 30 | , blockRange |
31 | , isPiece | ||
35 | ) where | 32 | ) where |
36 | 33 | ||
37 | import Control.Applicative | 34 | import Control.Applicative |
@@ -44,20 +41,7 @@ import Data.Typeable | |||
44 | import Text.PrettyPrint | 41 | import Text.PrettyPrint |
45 | import Text.PrettyPrint.Class | 42 | import Text.PrettyPrint.Class |
46 | 43 | ||
47 | {----------------------------------------------------------------------- | 44 | import Data.Torrent.Piece |
48 | -- Piece attributes | ||
49 | -----------------------------------------------------------------------} | ||
50 | |||
51 | -- | Zero-based index of piece in torrent content. | ||
52 | type PieceIx = Int | ||
53 | |||
54 | -- | Size of piece in bytes. Should be a power of 2. | ||
55 | -- | ||
56 | -- NOTE: Have max and min size constrained to wide used | ||
57 | -- semi-standard values. This bounds should be used to make decision | ||
58 | -- about piece size for new torrents. | ||
59 | -- | ||
60 | type PieceSize = Int | ||
61 | 45 | ||
62 | {----------------------------------------------------------------------- | 46 | {----------------------------------------------------------------------- |
63 | -- Block attributes | 47 | -- Block attributes |
@@ -171,3 +155,9 @@ blockIx = BlockIx <$> blkPiece <*> blkOffset <*> blockSize | |||
171 | blockRange :: (Num a, Integral a) => PieceSize -> Block Lazy.ByteString -> (a, a) | 155 | blockRange :: (Num a, Integral a) => PieceSize -> Block Lazy.ByteString -> (a, a) |
172 | blockRange pieceSize = blockIxRange pieceSize . blockIx | 156 | blockRange pieceSize = blockIxRange pieceSize . blockIx |
173 | {-# INLINE blockRange #-} | 157 | {-# INLINE blockRange #-} |
158 | |||
159 | -- | Test if a block can be safely turned into a piece. | ||
160 | isPiece :: PieceSize -> Block Lazy.ByteString -> Bool | ||
161 | isPiece pieceLen blk @ (Block i offset _) = | ||
162 | offset == 0 && blockSize blk == pieceLen && i >= 0 | ||
163 | {-# INLINE isPiece #-} | ||