diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /dht/bittorrent/examples/Client.hs | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (diff) |
Factor out some new libraries
word64-map:
Data.Word64Map
network-addr:
Network.Address
tox-crypto:
Crypto.Tox
lifted-concurrent:
Control.Concurrent.Lifted.Instrument
Control.Concurrent.Async.Lifted.Instrument
psq-wrap:
Data.Wrapper.PSQInt
Data.Wrapper.PSQ
minmax-psq:
Data.MinMaxPSQ
tasks:
Control.Concurrent.Tasks
kad:
Network.Kademlia
Network.Kademlia.Bootstrap
Network.Kademlia.Routing
Network.Kademlia.CommonAPI
Network.Kademlia.Persistence
Network.Kademlia.Search
Diffstat (limited to 'dht/bittorrent/examples/Client.hs')
-rw-r--r-- | dht/bittorrent/examples/Client.hs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/dht/bittorrent/examples/Client.hs b/dht/bittorrent/examples/Client.hs new file mode 100644 index 00000000..26711676 --- /dev/null +++ b/dht/bittorrent/examples/Client.hs | |||
@@ -0,0 +1,74 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | ||
2 | {-# LANGUAGE CPP #-} | ||
3 | {-# LANGUAGE ExistentialQuantification #-} | ||
4 | {-# LANGUAGE RecordWildCards #-} | ||
5 | module Main (main) where | ||
6 | import Control.Concurrent | ||
7 | import Control.Monad.Trans | ||
8 | import Data.Maybe | ||
9 | import Options.Applicative | ||
10 | import System.Environment | ||
11 | import System.Exit | ||
12 | import System.IO | ||
13 | import Text.Read | ||
14 | |||
15 | import 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) | ||
20 | maybeReader f = eitherReader (maybe (Left ":(") Right . f) | ||
21 | #else | ||
22 | maybeReader f = f | ||
23 | #endif | ||
24 | |||
25 | {----------------------------------------------------------------------- | ||
26 | -- Command line arguments | ||
27 | -----------------------------------------------------------------------} | ||
28 | |||
29 | data TorrentBox = forall s. TorrentSource s => TorrentBox { unTorrentBox :: s } | ||
30 | |||
31 | data Args = Args | ||
32 | { topic :: TorrentBox | ||
33 | , contentDir :: FilePath | ||
34 | } | ||
35 | |||
36 | argsParser :: Parser Args | ||
37 | argsParser = 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 | |||
56 | argsInfo :: ParserInfo Args | ||
57 | argsInfo = info (helper <*> argsParser) | ||
58 | ( fullDesc | ||
59 | <> progDesc "A simple CLI bittorrent client" | ||
60 | <> header "foo" | ||
61 | ) | ||
62 | |||
63 | {----------------------------------------------------------------------- | ||
64 | -- Client | ||
65 | -----------------------------------------------------------------------} | ||
66 | |||
67 | run :: Args -> BitTorrent () | ||
68 | run (Args (TorrentBox t) dir) = do | ||
69 | h <- openHandle dir t | ||
70 | start h | ||
71 | liftIO $ threadDelay 10000000000 | ||
72 | |||
73 | main :: IO () | ||
74 | main = execParser argsInfo >>= simpleClient . run | ||