diff options
Diffstat (limited to 'examples/Client.hs')
-rw-r--r-- | examples/Client.hs | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/examples/Client.hs b/examples/Client.hs index 320b4269..abf62657 100644 --- a/examples/Client.hs +++ b/examples/Client.hs | |||
@@ -1,26 +1,65 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | ||
2 | {-# LANGUAGE ExistentialQuantification #-} | ||
3 | {-# LANGUAGE RecordWildCards #-} | ||
1 | module Main (main) where | 4 | module Main (main) where |
2 | import Control.Concurrent | 5 | import Control.Concurrent |
3 | import Control.Monad.Trans | 6 | import Control.Monad.Trans |
7 | import Options.Applicative | ||
4 | import System.Environment | 8 | import System.Environment |
5 | import System.Exit | 9 | import System.Exit |
6 | import System.IO | 10 | import System.IO |
11 | import Text.Read | ||
12 | |||
7 | import Network.BitTorrent | 13 | import Network.BitTorrent |
8 | 14 | ||
9 | 15 | ||
10 | parseArgs :: IO FilePath | 16 | {----------------------------------------------------------------------- |
11 | parseArgs = do | 17 | -- Command line arguments |
12 | args <- getArgs | 18 | -----------------------------------------------------------------------} |
13 | case args of | 19 | |
14 | [path] -> return path | 20 | data TorrentBox = forall s. TorrentSource s => TorrentBox { unTorrentBox :: s } |
15 | _ -> do | 21 | |
16 | hPutStrLn stderr "Usage: client file.torrent" | 22 | data Args = Args |
17 | exitFailure | 23 | { topic :: TorrentBox |
24 | , contentDir :: FilePath | ||
25 | } | ||
26 | |||
27 | argsParser :: Parser Args | ||
28 | argsParser = Args <$> (TorrentBox <$> infohashP <|> TorrentBox <$> torrentP) | ||
29 | <*> destDirP | ||
30 | where | ||
31 | infohashP :: Parser InfoHash | ||
32 | infohashP = argument readMaybe | ||
33 | (metavar "SHA1" <> help "infohash of torrent file") | ||
34 | |||
35 | torrentP :: Parser FilePath | ||
36 | torrentP = argument Just | ||
37 | ( metavar "FILE" | ||
38 | <> help "A .torrent file" | ||
39 | ) | ||
40 | |||
41 | destDirP :: Parser FilePath | ||
42 | destDirP = argument Just | ||
43 | ( metavar "DIR" | ||
44 | <> help "Directory to put content" | ||
45 | ) | ||
46 | |||
47 | argsInfo :: ParserInfo Args | ||
48 | argsInfo = info (helper <*> argsParser) | ||
49 | ( fullDesc | ||
50 | <> progDesc "A simple CLI bittorrent client" | ||
51 | <> header "foo" | ||
52 | ) | ||
53 | |||
54 | {----------------------------------------------------------------------- | ||
55 | -- Client | ||
56 | -----------------------------------------------------------------------} | ||
57 | |||
58 | run :: Args -> BitTorrent () | ||
59 | run (Args (TorrentBox t) dir) = do | ||
60 | h <- openHandle dir t | ||
61 | start h | ||
62 | liftIO $ threadDelay 10000000000 | ||
18 | 63 | ||
19 | main :: IO () | 64 | main :: IO () |
20 | main = do | 65 | main = execParser argsInfo >>= simpleClient . run |
21 | path <- parseArgs | ||
22 | torrent <- fromFile path | ||
23 | simpleClient $ do | ||
24 | h <- openTorrent "data" torrent | ||
25 | start h | ||
26 | liftIO $ threadDelay 10000000000 | ||