From: Ramakrishnan Muthukrishnan Date: Fri, 24 Jul 2015 11:00:24 +0000 (+0530) Subject: get the piece length and store it as piecestate X-Git-Url: https://git.rkrishnan.org/pf/components/com_hotproperty/css/frontends/FTP-and-SFTP.txt?a=commitdiff_plain;h=08bb2b2c3c7695e62b7476a005e8566b7ba2802d;p=functorrent.git get the piece length and store it as piecestate This needs to be revisited. Piece Length is the same for all pieces but we are storing it for every piece number, which is a waste. --- diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index 33d72a3..bd44076 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -47,7 +47,8 @@ data PieceDlState = Pending -- 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 , state :: PieceDlState -- ^ state of the piece from download perspective. - , hash :: ByteString } -- ^ piece hash + , hash :: ByteString -- ^ piece hash + , len :: Integer } -- ^ piece length -- which piece is with which peers type PieceMap = Map Integer PieceData @@ -71,11 +72,13 @@ 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 -> PieceMap -mkPieceMap numPieces pieceHash = fromList kvs +mkPieceMap :: Integer -> ByteString -> Integer -> PieceMap +mkPieceMap numPieces pieceHash pLen = fromList kvs where kvs = [(i, PieceData { peers = [] , state = Pending - , hash = h }) | (i, h) <- zip [0..numPieces] hashes] + , hash = h + , len = pLen }) + | (i, h) <- zip [0..numPieces] hashes] hashes = splitN (fromIntegral numPieces) pieceHash havePiece :: PieceMap -> Integer -> Bool @@ -207,7 +210,8 @@ msgLoop state pieceStatus | meInterested state == False && case pickPiece pieceStatus of Nothing -> putStrLn "Nothing to download" Just workPiece -> do - sendMsg (handle state) (RequestMsg workPiece 0 16384) + let pLen = len (pieceStatus ! workPiece) + sendMsg (handle state) (RequestMsg workPiece 0 pLen) putStrLn $ "--> RequestMsg for Piece " ++ (show workPiece) ++ "to peer: " ++ show (peer state) msg <- getMsg (handle state) putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer state) @@ -260,8 +264,9 @@ handlePeerMsgs p m peerId logFn = do , heChoking = True , meInterested = False , meChoking = True } - pieceHash = (pieces (info m)) + pieceHash = pieces (info m) numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash - pieceStatus = mkPieceMap numPieces pieceHash + pLen = pieceLength (info m) :: Integer + pieceStatus = mkPieceMap numPieces pieceHash pLen msgLoop state pieceStatus