summaryrefslogtreecommitdiff
path: root/src/Data/Torrent.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-04 04:47:13 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-04 04:47:13 +0400
commit8f9796f613a844abd3e5018eba8d33eae7a278e5 (patch)
tree0d7c9cc9b1a3017d5975d9f2750a8356422d908e /src/Data/Torrent.hs
parenta34f903abae190a7f3d6b7b469cf1911164dd451 (diff)
+ Add Torrent Hashable instance.
Diffstat (limited to 'src/Data/Torrent.hs')
-rw-r--r--src/Data/Torrent.hs12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index bdd38630..8e6f9088 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -50,7 +50,8 @@ module Data.Torrent
50 50
51#if defined (TESTING) 51#if defined (TESTING)
52 -- * Internal 52 -- * Internal
53 , hash, hashlazy 53 , Data.Torrent.hash
54 , Data.Torrent.hashlazy
54#endif 55#endif
55 ) where 56 ) where
56 57
@@ -71,6 +72,7 @@ import qualified Data.ByteString.Lazy as Lazy
71import qualified Data.ByteString.Lazy.Builder as B 72import qualified Data.ByteString.Lazy.Builder as B
72import qualified Data.ByteString.Lazy.Builder.ASCII as B 73import qualified Data.ByteString.Lazy.Builder.ASCII as B
73import qualified Data.List as L 74import qualified Data.List as L
75import Data.Hashable as Hashable
74import Data.Text (Text) 76import Data.Text (Text)
75import Data.Serialize as S hiding (Result) 77import Data.Serialize as S hiding (Result)
76import Text.PrettyPrint 78import Text.PrettyPrint
@@ -127,6 +129,9 @@ data Torrent = Torrent {
127 -- the encrypted SHA-1 hash of the info dictionary). 129 -- the encrypted SHA-1 hash of the info dictionary).
128 } deriving (Show, Eq) 130 } deriving (Show, Eq)
129 131
132instance Hashable Torrent where
133 hash = Hashable.hash . tInfoHash
134
130{- note that info hash is actually reduntant field 135{- note that info hash is actually reduntant field
131 but it's better to keep it here to avoid heavy recomputations 136 but it's better to keep it here to avoid heavy recomputations
132-} 137-}
@@ -377,7 +382,7 @@ checkPiece :: ContentInfo -> Int -> ByteString -> Bool
377checkPiece ci ix piece @ (PS _ off si) 382checkPiece ci ix piece @ (PS _ off si)
378 | traceShow (ix, off, si) True 383 | traceShow (ix, off, si) True
379 = B.length piece == ciPieceLength ci 384 = B.length piece == ciPieceLength ci
380 && hash piece == InfoHash (pieceHash ci ix) 385 && C.hash piece == pieceHash ci ix
381 386
382-- | Read and decode a .torrent file. 387-- | Read and decode a .torrent file.
383fromFile :: FilePath -> IO Torrent 388fromFile :: FilePath -> IO Torrent
@@ -395,6 +400,9 @@ fromFile filepath = do
395newtype InfoHash = InfoHash { getInfoHash :: ByteString } 400newtype InfoHash = InfoHash { getInfoHash :: ByteString }
396 deriving (Eq, Ord) 401 deriving (Eq, Ord)
397 402
403instance Hashable InfoHash where
404 hash = Hashable.hash . getInfoHash
405
398instance BEncodable InfoHash where 406instance BEncodable InfoHash where
399 toBEncode = toBEncode . getInfoHash 407 toBEncode = toBEncode . getInfoHash
400 fromBEncode be = InfoHash <$> fromBEncode be 408 fromBEncode be = InfoHash <$> fromBEncode be