From 65107192f9c5f8ff134362a7b9d37cb3f73dad7e Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Fri, 3 Jan 2014 00:59:48 +0400 Subject: Add skeleton for gettorrent example --- bittorrent.cabal | 9 ++++++++ examples/GetTorrent.hs | 37 +++++++++++++++++++++++++++++++++ src/Network/BitTorrent/Core/Node.hs | 3 +++ src/Network/BitTorrent/Core/PeerAddr.hs | 7 +++++++ 4 files changed, 56 insertions(+) create mode 100644 examples/GetTorrent.hs diff --git a/bittorrent.cabal b/bittorrent.cabal index c197cb91..347cc38d 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal @@ -237,6 +237,15 @@ test-suite spec -- , bittorrent -- ghc-options: -O2 -Wall -fno-warn-orphans +executable gettorrent + default-language: Haskell2010 + hs-source-dirs: examples + main-is: GetTorrent.hs + build-depends: base == 4.* + , optparse-applicative + , data-default + , bittorrent + executable client-example default-language: Haskell2010 hs-source-dirs: examples diff --git a/examples/GetTorrent.hs b/examples/GetTorrent.hs new file mode 100644 index 00000000..a9d2a44f --- /dev/null +++ b/examples/GetTorrent.hs @@ -0,0 +1,37 @@ +module Main (main) where +import Data.Default + +import Options.Applicative +import Data.Torrent.InfoHash +import Network.BitTorrent.Core + + +data Params = Params + { infohash :: InfoHash + , thisNode :: NodeAddr IPv4 + , bootNode :: NodeAddr IPv4 + } deriving Show + +paramsParser :: Parser Params +paramsParser = Params + <$> option (long "infohash" <> short 'i' + <> metavar "SHA1" <> help "infohash of torrent file") + <*> option (long "node" <> short 'n' <> value def + <> metavar "NODE" <> help "this node address" + ) + <*> option (long "boot" <> short 'b' + <> metavar "NODE" <> help "bootstrap node address" + ) + +programInfo :: ParserInfo Params +programInfo = info (helper <*> paramsParser) + ( fullDesc + <> progDesc "" + <> header "gettorrent - get torrent file by infohash" + ) + +getTorrent :: Params -> IO () +getTorrent = print + +main :: IO () +main = execParser programInfo >>= getTorrent diff --git a/src/Network/BitTorrent/Core/Node.hs b/src/Network/BitTorrent/Core/Node.hs index da96ca43..116333c5 100644 --- a/src/Network/BitTorrent/Core/Node.hs +++ b/src/Network/BitTorrent/Core/Node.hs @@ -122,6 +122,9 @@ data NodeAddr a = NodeAddr $(deriveJSON omitRecordPrefix ''NodeAddr) +instance Read (NodeAddr IPv4) where + readsPrec i x = [ (fromPeerAddr a, s) | (a, s) <- readsPrec i x ] + -- | @127.0.0.1:6882@ instance Default (NodeAddr IPv4) where def = "127.0.0.1:6882" diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index d634716c..e3e09fd6 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs @@ -251,6 +251,13 @@ instance IsString (PeerAddr IPv4) where = PeerAddr Nothing hostAddr portNum | otherwise = error $ "fromString: unable to parse (PeerAddr IPv4): " ++ str +instance Read (PeerAddr IPv4) where + readsPrec i = RP.readP_to_S $ do + ipv4 <- RP.readS_to_P (readsPrec i) + _ <- RP.char ':' + port <- toEnum <$> RP.readS_to_P (readsPrec i) + return $ PeerAddr Nothing ipv4 port + readsIPv6_port :: String -> [((IPv6, PortNumber), String)] readsIPv6_port = RP.readP_to_S $ do ip <- RP.char '[' *> (RP.readS_to_P reads) <* RP.char ']' -- cgit v1.2.3