From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Fri, 11 Dec 2015 15:49:59 +0000 (+0530)
Subject: FileSystem: do not expose msgs outside the module, instead expose helper fns
X-Git-Url: https://git.rkrishnan.org/specifications/components/com_hotproperty/frontends/index.php?a=commitdiff_plain;h=4e20d9e8c878c6f2125ea25d062ccfb427cf96f5;p=functorrent.git

FileSystem: do not expose msgs outside the module, instead expose helper fns
---

diff --git a/src/FuncTorrent/FileSystem.hs b/src/FuncTorrent/FileSystem.hs
index e064b71..df203b0 100644
--- a/src/FuncTorrent/FileSystem.hs
+++ b/src/FuncTorrent/FileSystem.hs
@@ -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)
+
diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 834c808..bd66f99 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -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