From 41186aba417948342c47148e33053b3e3d3f2bd6 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sun, 19 Jul 2015 12:03:23 +0530 Subject: [PATCH] helper function: havePiece --- src/FuncTorrent/Peer.hs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index e0a27c9..490f080 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -18,7 +18,7 @@ import Control.Monad (replicateM, liftM, forever) 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) @@ -40,7 +40,7 @@ type Pieces = [PieceData] 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 @@ -69,13 +69,17 @@ data PeerMsg = KeepAliveMsg -- 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 -- 2.37.2