From 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Sat, 28 Sep 2019 13:43:29 -0400 Subject: 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 --- dht/bittorrent/examples/Client.hs | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 dht/bittorrent/examples/Client.hs (limited to 'dht/bittorrent/examples/Client.hs') 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 @@ +{-# LANGUAGE RankNTypes #-} +{-# LANGUAGE CPP #-} +{-# LANGUAGE ExistentialQuantification #-} +{-# LANGUAGE RecordWildCards #-} +module Main (main) where +import Control.Concurrent +import Control.Monad.Trans +import Data.Maybe +import Options.Applicative +import System.Environment +import System.Exit +import System.IO +import Text.Read + +import Network.BitTorrent + +#if MIN_VERSION_optparse_applicative(0,13,0) +-- maybeReader imported from Options.Applicative.Builder +#elif MIN_VERSION_optparse_applicative(0,11,0) +maybeReader f = eitherReader (maybe (Left ":(") Right . f) +#else +maybeReader f = f +#endif + +{----------------------------------------------------------------------- +-- Command line arguments +-----------------------------------------------------------------------} + +data TorrentBox = forall s. TorrentSource s => TorrentBox { unTorrentBox :: s } + +data Args = Args + { topic :: TorrentBox + , contentDir :: FilePath + } + +argsParser :: Parser Args +argsParser = Args <$> (TorrentBox <$> infohashP <|> TorrentBox <$> torrentP) + <*> destDirP + where + infohashP :: Parser InfoHash + infohashP = argument (maybeReader readMaybe) + (metavar "SHA1" <> help "infohash of torrent file") + + torrentP :: Parser FilePath + torrentP = argument (maybeReader Just) + ( metavar "FILE" + <> help "A .torrent file" + ) + + destDirP :: Parser FilePath + destDirP = argument (maybeReader Just) + ( metavar "DIR" + <> help "Directory to put content" + ) + +argsInfo :: ParserInfo Args +argsInfo = info (helper <*> argsParser) + ( fullDesc + <> progDesc "A simple CLI bittorrent client" + <> header "foo" + ) + +{----------------------------------------------------------------------- +-- Client +-----------------------------------------------------------------------} + +run :: Args -> BitTorrent () +run (Args (TorrentBox t) dir) = do + h <- openHandle dir t + start h + liftIO $ threadDelay 10000000000 + +main :: IO () +main = execParser argsInfo >>= simpleClient . run -- cgit v1.2.3