From d03410277fbd01d3605d363688e186e41e7c5cb3 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Sun, 2 Aug 2015 20:57:28 +0530 Subject: [PATCH] write to the file name specified in the info structure --- src/FuncTorrent/Peer.hs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index e809f3f..afc2028 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -83,16 +83,16 @@ bitfieldToList bs = go bs 0 setBits ++ go bs' (pos + 1) -- recvMsg :: Peer -> Handle -> Msg -msgLoop :: PeerState -> PieceMap -> IO () -msgLoop pState@(PeerState { meInterested = False, heChoking = True }) pieceStatus = +msgLoop :: PeerState -> PieceMap -> FilePath -> IO () +msgLoop pState@(PeerState { meInterested = False, heChoking = True }) pieceStatus file = do -- if me NOT Interested and she is Choking, tell her that -- I am interested. let h = handle pState sendMsg h InterestedMsg putStrLn $ "--> InterestedMsg to peer: " ++ show (peer pState) - msgLoop (pState { meInterested = True }) pieceStatus -msgLoop pState@(PeerState { meInterested = True, heChoking = False }) pieceStatus = + msgLoop (pState { meInterested = True }) pieceStatus file +msgLoop pState@(PeerState { meInterested = True, heChoking = False }) pieceStatus file = -- if me Interested and she not Choking, send her a request -- for a piece. case pickPiece pieceStatus of @@ -107,16 +107,16 @@ msgLoop pState@(PeerState { meInterested = True, heChoking = False }) pieceStatu else do let fileOffset = if workPiece == 0 then 0 else workPiece * len (pieceStatus ! (workPiece - 1)) putStrLn $ "Write into file at offset: " ++ show fileOffset - writeFileAtOffset "/tmp/download.file" fileOffset pBS - msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus) -msgLoop pState pieceStatus = do + writeFileAtOffset file fileOffset pBS + msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus) file +msgLoop pState pieceStatus file = do msg <- getMsg (handle pState) putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState) case msg of KeepAliveMsg -> do sendMsg (handle pState) KeepAliveMsg putStrLn $ "--> " ++ "KeepAliveMsg to peer: " ++ show (peer pState) - msgLoop pState pieceStatus + msgLoop pState pieceStatus file BitFieldMsg bss -> do let pieceList = bitfieldToList (unpack bss) pieceStatus' = updatePieceAvailability pieceStatus (peer pState) pieceList @@ -124,11 +124,11 @@ msgLoop pState pieceStatus = do -- for each pieceIndex in pieceList, make an entry in the pieceStatus -- map with pieceIndex as the key and modify the value to add the peer. -- download each of the piece in order - msgLoop pState pieceStatus' + msgLoop pState pieceStatus' file UnChokeMsg -> - msgLoop (pState { heChoking = False }) pieceStatus + msgLoop (pState { heChoking = False }) pieceStatus file _ -> - msgLoop pState pieceStatus + msgLoop pState pieceStatus file -- simple algorithm to pick piece. -- pick the first piece from 0 that is not downloaded yet. @@ -161,9 +161,10 @@ handlePeerMsgs p m peerId = do numPieces = (toInteger . (`quot` 20) . BC.length) pieceHash pLen = pieceLength (info m) fileLen = lengthInBytes (info m) + fileName = name (info m) pieceStatus = mkPieceMap numPieces pieceHash (splitNum fileLen pLen) - createDummyFile "/tmp/download.file" (fromIntegral fileLen) - msgLoop state pieceStatus + createDummyFile fileName (fromIntegral fileLen) + msgLoop state pieceStatus fileName downloadPiece :: Handle -> Integer -> Integer -> IO ByteString downloadPiece h index pieceLength = do -- 2.37.2