summaryrefslogtreecommitdiff
path: root/exsamples/Main.hs
blob: 887966422c39120bfd17eac077222314e4684773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE ViewPatterns #-}
module Main (main) where

import Control.Concurrent
import Data.Bitfield
import Network.BitTorrent
import System.Environment
import Control.Monad.Reader
import Data.IORef

main :: IO ()
main = do
  [path] <- getArgs
  torrent <- fromFile path

  client  <- newClient []
  swarm   <- newLeacher  client torrent

  ref <- newIORef 0

  discover swarm $ do
    addr <- asks connectedPeerAddr
    liftIO $ print $ "connected to" ++ show addr

    forever $ do
      e <- awaitEvent
      case e of
        Available bf
          | Just m <- findMin bf -> yieldEvent (Want (BlockIx m 0 10))
          |     otherwise        -> return ()
        Want     bix -> liftIO $ print bix
        Fragment blk -> do

          liftIO $ do
            readIORef ref >>= print
            modifyIORef ref succ
            print (ppBlock blk)

          yieldEvent (Want (BlockIx 0 0 (16 * 1024)))


  print "Bye-bye! =_="