FileSystem: do not expose msgs outside the module, instead expose helper fns
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 11 Dec 2015 15:49:59 +0000 (21:19 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 11 Dec 2015 15:50:09 +0000 (21:20 +0530)
src/FuncTorrent/FileSystem.hs
src/FuncTorrent/Peer.hs

index e064b713c68607bd05941e22e5bce8ae64d86ddb..df203b09b752c0f8cf17bd5c398e72b5ea136df1 100644 (file)
@@ -2,18 +2,17 @@ module FuncTorrent.FileSystem
        (startThread,
         MsgChannel,
         createMsgChannel,
-        Msg(..),
+        writePiece,
         Piece(..),
         pieceMapFromFile
        )
        where
 
 import           Control.Concurrent (ThreadId, forkIO)
-import           Control.Concurrent.Chan (Chan, newChan, readChan)
+import           Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
 import           Control.Concurrent.MVar (MVar, putMVar)
 import           Control.Monad (forever)
 import           Data.Map (traverseWithKey)
-
 import qualified Data.ByteString as BS
 import           Data.Map ((!))
 import           System.IO (Handle, IOMode (ReadWriteMode), withFile)
@@ -75,3 +74,8 @@ pieceMapFromFile filePath fileLen pieceMap = do
       if isHashValid
         then return $ v { dlstate = Have }
         else return v
+
+writePiece :: MsgChannel -> PieceNum -> BS.ByteString -> IO ()
+writePiece c pieceNum bs = do
+  writeChan c $ WritePiece (Piece pieceNum bs)
+
index 834c8086032c700f921e7034975156f2b1653e6e..bd66f99a43ef5c0f0357a56fd32ec4cc00c3b781 100644 (file)
@@ -7,7 +7,6 @@ module FuncTorrent.Peer
 
 import Prelude hiding (lookup, concat, replicate, splitAt, take, drop)
 
-import Control.Concurrent.Chan (writeChan)
 import Control.Monad.State
 import Data.ByteString (ByteString, unpack, concat, hGet, hPut, take, drop, empty)
 import Data.Bits
@@ -20,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, Msg(..), Piece(..))
+import qualified FuncTorrent.FileSystem as FS (MsgChannel, writePiece, Piece(..))
 
 data PState = PState { handle :: Handle
                      , peer :: Peer
@@ -117,7 +116,7 @@ msgLoop pieceStatus msgchannel = do
             liftIO $ putStrLn "Hash mismatch"
             else do
             liftIO $ putStrLn $ "Write piece: " ++ show workPiece
-            liftIO $ writeChan msgchannel $ FS.WritePiece (FS.Piece workPiece pBS)
+            liftIO $ FS.writePiece msgchannel workPiece pBS
             msgLoop (adjust (\pieceData -> pieceData { dlstate = Have }) workPiece pieceStatus) msgchannel
     _ -> do
       msg <- liftIO $ getMsg h