diff options
Diffstat (limited to 'src/Data/Torrent')
-rw-r--r-- | src/Data/Torrent/Block.hs | 30 | ||||
-rw-r--r-- | src/Data/Torrent/Piece.hs | 29 |
2 files changed, 27 insertions, 32 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 #-} | ||
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index 31680ce8..00d4b843 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs | |||
@@ -23,7 +23,6 @@ module Data.Torrent.Piece | |||
23 | -- * Piece data | 23 | -- * Piece data |
24 | , Piece (..) | 24 | , Piece (..) |
25 | , pieceSize | 25 | , pieceSize |
26 | , isPiece | ||
27 | 26 | ||
28 | -- * Piece control | 27 | -- * Piece control |
29 | , HashArray (..) | 28 | , HashArray (..) |
@@ -63,11 +62,9 @@ import Data.Typeable | |||
63 | import Text.PrettyPrint | 62 | import Text.PrettyPrint |
64 | import Text.PrettyPrint.Class | 63 | import Text.PrettyPrint.Class |
65 | 64 | ||
66 | import Data.Torrent.Block | 65 | -- TODO add torrent file validation |
67 | |||
68 | |||
69 | class Lint a where | 66 | class Lint a where |
70 | lint :: a -> Either String a | 67 | lint :: a -> Either String a |
71 | 68 | ||
72 | --class Validation a where | 69 | --class Validation a where |
73 | -- validate :: PieceInfo -> Piece a -> Bool | 70 | -- validate :: PieceInfo -> Piece a -> Bool |
@@ -76,9 +73,23 @@ class Lint a where | |||
76 | -- Piece attributes | 73 | -- Piece attributes |
77 | -----------------------------------------------------------------------} | 74 | -----------------------------------------------------------------------} |
78 | 75 | ||
76 | -- | Zero-based index of piece in torrent content. | ||
77 | type PieceIx = Int | ||
78 | |||
79 | -- | Size of piece in bytes. Should be a power of 2. | ||
80 | -- | ||
81 | -- NOTE: Have max and min size constrained to wide used | ||
82 | -- semi-standard values. This bounds should be used to make decision | ||
83 | -- about piece size for new torrents. | ||
84 | -- | ||
85 | type PieceSize = Int | ||
86 | |||
79 | -- | Number of pieces in torrent or a part of torrent. | 87 | -- | Number of pieces in torrent or a part of torrent. |
80 | type PieceCount = Int | 88 | type PieceCount = Int |
81 | 89 | ||
90 | defaultBlockSize :: Int | ||
91 | defaultBlockSize = 16 * 1024 | ||
92 | |||
82 | -- | Optimal number of pieces in torrent. | 93 | -- | Optimal number of pieces in torrent. |
83 | optimalPieceCount :: PieceCount | 94 | optimalPieceCount :: PieceCount |
84 | optimalPieceCount = 1000 | 95 | optimalPieceCount = 1000 |
@@ -86,7 +97,7 @@ optimalPieceCount = 1000 | |||
86 | 97 | ||
87 | -- | Piece size should not be less than this value. | 98 | -- | Piece size should not be less than this value. |
88 | minPieceSize :: Int | 99 | minPieceSize :: Int |
89 | minPieceSize = defaultTransferSize * 4 | 100 | minPieceSize = defaultBlockSize * 4 |
90 | {-# INLINE minPieceSize #-} | 101 | {-# INLINE minPieceSize #-} |
91 | 102 | ||
92 | -- | To prevent transfer degradation piece size should not exceed this | 103 | -- | To prevent transfer degradation piece size should not exceed this |
@@ -130,12 +141,6 @@ instance Pretty (Piece a) where | |||
130 | pieceSize :: Piece BL.ByteString -> PieceSize | 141 | pieceSize :: Piece BL.ByteString -> PieceSize |
131 | pieceSize Piece {..} = fromIntegral (BL.length pieceData) | 142 | pieceSize Piece {..} = fromIntegral (BL.length pieceData) |
132 | 143 | ||
133 | -- | Test if a block can be safely turned into a piece. | ||
134 | isPiece :: PieceSize -> Block BL.ByteString -> Bool | ||
135 | isPiece pieceLen blk @ (Block i offset _) = | ||
136 | offset == 0 && blockSize blk == pieceLen && i >= 0 | ||
137 | {-# INLINE isPiece #-} | ||
138 | |||
139 | {----------------------------------------------------------------------- | 144 | {----------------------------------------------------------------------- |
140 | -- Piece control | 145 | -- Piece control |
141 | -----------------------------------------------------------------------} | 146 | -----------------------------------------------------------------------} |