type Port = Integer
data PeerState = PeerState { handle :: Handle
- , am_choking :: Bool
- , am_interested :: Bool
- , peer_choking :: Bool
- , peer_interested :: Bool}
+ , amChoking :: Bool
+ , amInterested :: Bool
+ , peerChoking :: Bool
+ , peerInterested :: Bool}
-- Maintain info on every piece and the current state of it.
-- should probably be a TVar.
bitfieldToList bs = go bs 0
where go [] _ = []
go (b:bs') pos =
- let setBits = [pos*8 + (toInteger i) | i <- [0..8], testBit b i]
+ let setBits = [pos*8 + toInteger i | i <- [0..8], testBit b i]
in
- setBits ++ (go bs' (pos + 1))
+ setBits ++ go bs' (pos + 1)
-- downloadPiece :: Integer -> Handle -> IO ()
createDummyFile :: FilePath -> Int -> IO ()
-createDummyFile path size = do
+createDummyFile path size =
writeFile path (BC.replicate size '\0')
-- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData.
case msg of
BitFieldMsg bss -> do
let pieceList = bitfieldToList (unpack bss)
- putStrLn (show pieceList)
+ print pieceList
-- download each of the piece in order
- _ -> putStrLn (show msg)
+ _ -> print msg
handlePeerMsgs :: Peer -> Metainfo -> String -> (String -> IO ()) -> IO ()
handlePeerMsgs p m peerId logFn = do
h <- handShake p (infoHash m) peerId
- logFn $ "handShake"
+ logFn "handShake"
msgLoop h (pieces (info m))