summaryrefslogtreecommitdiff
path: root/examples/Client.hs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/Client.hs')
-rw-r--r--examples/Client.hs74
1 files changed, 0 insertions, 74 deletions
diff --git a/examples/Client.hs b/examples/Client.hs
deleted file mode 100644
index 26711676..00000000
--- a/examples/Client.hs
+++ /dev/null
@@ -1,74 +0,0 @@
1{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE ExistentialQuantification #-}
4{-# LANGUAGE RecordWildCards #-}
5module Main (main) where
6import Control.Concurrent
7import Control.Monad.Trans
8import Data.Maybe
9import Options.Applicative
10import System.Environment
11import System.Exit
12import System.IO
13import Text.Read
14
15import Network.BitTorrent
16
17#if MIN_VERSION_optparse_applicative(0,13,0)
18-- maybeReader imported from Options.Applicative.Builder
19#elif MIN_VERSION_optparse_applicative(0,11,0)
20maybeReader f = eitherReader (maybe (Left ":(") Right . f)
21#else
22maybeReader f = f
23#endif
24
25{-----------------------------------------------------------------------
26-- Command line arguments
27-----------------------------------------------------------------------}
28
29data TorrentBox = forall s. TorrentSource s => TorrentBox { unTorrentBox :: s }
30
31data Args = Args
32 { topic :: TorrentBox
33 , contentDir :: FilePath
34 }
35
36argsParser :: Parser Args
37argsParser = Args <$> (TorrentBox <$> infohashP <|> TorrentBox <$> torrentP)
38 <*> destDirP
39 where
40 infohashP :: Parser InfoHash
41 infohashP = argument (maybeReader readMaybe)
42 (metavar "SHA1" <> help "infohash of torrent file")
43
44 torrentP :: Parser FilePath
45 torrentP = argument (maybeReader Just)
46 ( metavar "FILE"
47 <> help "A .torrent file"
48 )
49
50 destDirP :: Parser FilePath
51 destDirP = argument (maybeReader Just)
52 ( metavar "DIR"
53 <> help "Directory to put content"
54 )
55
56argsInfo :: ParserInfo Args
57argsInfo = info (helper <*> argsParser)
58 ( fullDesc
59 <> progDesc "A simple CLI bittorrent client"
60 <> header "foo"
61 )
62
63{-----------------------------------------------------------------------
64-- Client
65-----------------------------------------------------------------------}
66
67run :: Args -> BitTorrent ()
68run (Args (TorrentBox t) dir) = do
69 h <- openHandle dir t
70 start h
71 liftIO $ threadDelay 10000000000
72
73main :: IO ()
74main = execParser argsInfo >>= simpleClient . run