import Control.Applicative ((<$>), liftA3)
import Data.Bits
import Data.Word (Word8)
-import Data.Map (Map(..), fromList)
+import Data.Map (Map(..), fromList, (!))
import FuncTorrent.Metainfo (Info(..), Metainfo(..))
import FuncTorrent.Utils (splitN)
data PieceDlState = Pending
| InProgress
| Have
- deriving (Show)
+ deriving (Show, Eq)
-- todo - map with index to a new data structure (peers who have that piece amd state)
data PieceData = PieceData { peers :: [Peer] -- ^ list of peers who have this piece
-- Make the initial Piece map, with the assumption that no peer has the
-- piece and that every piece is pending download.
-mkPieceMap :: Integer -> ByteString -> Map Integer PieceData
+mkPieceMap :: Integer -> ByteString -> PieceMap
mkPieceMap numPieces pieceHash = fromList kvs
where kvs = [(i, PieceData { peers = []
, state = Pending
, hash = h }) | (i, h) <- zip [0..numPieces] hashes]
hashes = splitN (fromIntegral numPieces) pieceHash
+havePiece :: PieceMap -> Integer -> Bool
+havePiece pm index =
+ state (pm ! index) == Have
+
genHandShakeMsg :: ByteString -> String -> ByteString
genHandShakeMsg infoHash peer_id = concat [pstrlen, pstr, reserved, infoHash, peerID]
where pstrlen = singleton 19