]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Peer.hs
Peer: catchall case statement to catch any invalid msgs
[functorrent.git] / src / FuncTorrent / Peer.hs
index bd66f99a43ef5c0f0357a56fd32ec4cc00c3b781..9dd307d45c7a68898768d5b0c320440ed6409f6e 100644 (file)
@@ -19,7 +19,7 @@ import FuncTorrent.Metainfo (Metainfo(..))
 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
@@ -116,11 +116,11 @@ msgLoop pieceStatus msgchannel = do
             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
@@ -151,8 +151,14 @@ msgLoop pieceStatus msgchannel = do
           msgLoop pieceStatus msgchannel
         PortMsg _ ->
           msgLoop pieceStatus msgchannel
-        -- handle RequestMsg, HaveMsg. No need to handle PieceMsg here.
-        -- also BitFieldMsg
+        HaveMsg idx -> do
+          p <- gets peer
+          let pieceStatus' = updatePieceAvailability pieceStatus p [idx]
+          msgLoop pieceStatus' msgchannel
+        _ -> do
+          liftIO $ putStrLn $ ".. not doing anything with the msg"
+          msgLoop pieceStatus msgchannel
+        -- No need to handle PieceMsg and RequestMsg here.
 
 
 downloadPiece :: Handle -> Integer -> Integer -> IO ByteString