import FuncTorrent.PeerMsgs (Peer(..), PeerMsg(..), sendMsg, getMsg, genHandshakeMsg)
import FuncTorrent.Utils (splitNum, verifyHash)
import FuncTorrent.PieceManager (PieceDlState(..), PieceData(..), PieceMap, pickPiece, updatePieceAvailability)
-import qualified FuncTorrent.FileSystem as FS (MsgChannel, writePiece, Piece(..))
+import qualified FuncTorrent.FileSystem as FS (MsgChannel, writePieceToDisk, Piece(..))
data PState = PState { handle :: Handle
, peer :: Peer
liftIO $ putStrLn "Hash mismatch"
else do
liftIO $ putStrLn $ "Write piece: " ++ show workPiece
- liftIO $ FS.writePiece msgchannel workPiece pBS
+ liftIO $ FS.writePieceToDisk msgchannel workPiece pBS
msgLoop (adjust (\pieceData -> pieceData { dlstate = Have }) workPiece pieceStatus) msgchannel
_ -> do
msg <- liftIO $ getMsg h
- gets peer >>= (\p -> liftIO $ putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show p)
+ gets peer >>= (\p -> liftIO $ putStrLn $ "<-- " ++ show msg ++ " from peer: " ++ show p)
case msg of
KeepAliveMsg -> do
liftIO $ sendMsg h KeepAliveMsg
msgLoop pieceStatus msgchannel
PortMsg _ ->
msgLoop pieceStatus msgchannel
- -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here.
+ HaveMsg idx -> do
+ p <- gets peer
+ let pieceStatus' = updatePieceAvailability pieceStatus p [idx]
+ msgLoop pieceStatus' msgchannel
+ -- handle RequestMsg. No need to handle PieceMsg here.
-- also BitFieldMsg