(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)
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)
+
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
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
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