summaryrefslogtreecommitdiff
path: root/examples/Main.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-20 21:39:18 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-20 21:39:18 +0400
commit33b7d7badb2e6d96c8a0f532b40bcd7e3e67d1e9 (patch)
tree76ebbcf51be64fbc5a63e3da4b0dfe9b7505676b /examples/Main.hs
parente028ac4b464d73f6afee1727d68cedc4e3ed24d5 (diff)
~ Fix typo in dirname.
Diffstat (limited to 'examples/Main.hs')
-rw-r--r--examples/Main.hs46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/Main.hs b/examples/Main.hs
new file mode 100644
index 00000000..d0404405
--- /dev/null
+++ b/examples/Main.hs
@@ -0,0 +1,46 @@
1{-# LANGUAGE ViewPatterns #-}
2{-# LANGUAGE PatternGuards #-}
3module Main (main) where
4
5import Control.Concurrent
6import Data.Bitfield
7import Network.BitTorrent
8import System.Environment
9import Control.Monad.Reader
10import Data.IORef
11
12
13main :: IO ()
14main = do
15 [path] <- getArgs
16 torrent <- fromFile path
17
18 print (contentLayout "./" (tInfo torrent))
19
20 client <- newClient 100 []
21 swarm <- newLeecher client torrent
22
23 ref <- liftIO $ newIORef 0
24 discover swarm $ do
25 forever $ do
26 e <- awaitEvent
27 case e of
28 Available bf
29 | Just m <- findMin bf -> yieldEvent (Want (BlockIx m 0 10))
30 | otherwise -> return ()
31 Want bix -> liftIO $ print bix
32 Fragment blk -> do
33
34 sc <- liftIO $ getSessionCount swarm
35 addr <- asks connectedPeerAddr
36
37 liftIO $ do
38 x <- atomicModifyIORef ref (\x -> (succ x, x))
39 if x `mod` 100 == 0
40 then print (x, sc, addr)
41 else return ()
42
43 yieldEvent (Want (BlockIx 0 0 (16 * 1024)))
44
45
46 print "Bye-bye! =_=" \ No newline at end of file