-- [todo] - Implement the peer protocol here
-- | Talks peer protocol to one peer
--
-- Reports the list of available pieces back to the control thread, which
-- eventually schedules the pieces to be downloaded depending on various
-- prioritization techniques.
loop :: PeerThread -> IO ()
loop pt@(PeerThread _ availabilityChan _ _) = do
    -- Assume a torrent with 32 pieces and the remote peer has a few of them.
    -- Report those to the control thread.
    g <- newStdGen
    let available = nub $ take 4 (randomRs (0, 31) g) :: [Integer]

    mapM_ report available

  where
    report :: Integer -> IO ()
    report block = writeChan availabilityChan (pt, block)

-- Drains the block request channel and writes contents to writer channel.
loop :: PeerThread -> IO ()
loop pt@(PeerThread _ availabilityChan _ _) = do
    -- Assume a torrent with 32 pieces and the remote peer has a few of them.
    -- Report those to the control thread.
    g <- newStdGen
    let available = nub $ take 4 (randomRs (0, 31) g) :: [Integer]

    mapM_ report available

  where
    report :: Integer -> IO ()
    report block = writeChan availabilityChan (pt, block)

-- Drains the block request channel and writes contents to writer channel.
loop pt@(PeerThread _ availabilityChan _ _) = do
    -- Assume a torrent with 32 pieces and the remote peer has a few of them.
    -- Report those to the control thread.
    g <- newStdGen
    let available = nub $ take 4 (randomRs (0, 31) g) :: [Integer]

    mapM_ report available

  where
    report :: Integer -> IO ()
    report block = writeChan availabilityChan (pt, block)

-- Drains the block request channel and writes contents to writer channel.