From: Ramakrishnan Muthukrishnan Date: Sun, 19 Jul 2015 06:33:23 +0000 (+0530) Subject: helper function: havePiece X-Git-Url: https://git.rkrishnan.org/pf/content/en/frontends/flags/$rel_link?a=commitdiff_plain;h=41186aba417948342c47148e33053b3e3d3f2bd6;p=functorrent.git helper function: havePiece --- 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