diff options
-rw-r--r-- | bittorrent.cabal | 9 | ||||
-rw-r--r-- | examples/GetTorrent.hs | 37 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/Node.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 7 |
4 files changed, 56 insertions, 0 deletions
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 | |||
237 | -- , bittorrent | 237 | -- , bittorrent |
238 | -- ghc-options: -O2 -Wall -fno-warn-orphans | 238 | -- ghc-options: -O2 -Wall -fno-warn-orphans |
239 | 239 | ||
240 | executable gettorrent | ||
241 | default-language: Haskell2010 | ||
242 | hs-source-dirs: examples | ||
243 | main-is: GetTorrent.hs | ||
244 | build-depends: base == 4.* | ||
245 | , optparse-applicative | ||
246 | , data-default | ||
247 | , bittorrent | ||
248 | |||
240 | executable client-example | 249 | executable client-example |
241 | default-language: Haskell2010 | 250 | default-language: Haskell2010 |
242 | hs-source-dirs: examples | 251 | 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 @@ | |||
1 | module Main (main) where | ||
2 | import Data.Default | ||
3 | |||
4 | import Options.Applicative | ||
5 | import Data.Torrent.InfoHash | ||
6 | import Network.BitTorrent.Core | ||
7 | |||
8 | |||
9 | data Params = Params | ||
10 | { infohash :: InfoHash | ||
11 | , thisNode :: NodeAddr IPv4 | ||
12 | , bootNode :: NodeAddr IPv4 | ||
13 | } deriving Show | ||
14 | |||
15 | paramsParser :: Parser Params | ||
16 | paramsParser = Params | ||
17 | <$> option (long "infohash" <> short 'i' | ||
18 | <> metavar "SHA1" <> help "infohash of torrent file") | ||
19 | <*> option (long "node" <> short 'n' <> value def | ||
20 | <> metavar "NODE" <> help "this node address" | ||
21 | ) | ||
22 | <*> option (long "boot" <> short 'b' | ||
23 | <> metavar "NODE" <> help "bootstrap node address" | ||
24 | ) | ||
25 | |||
26 | programInfo :: ParserInfo Params | ||
27 | programInfo = info (helper <*> paramsParser) | ||
28 | ( fullDesc | ||
29 | <> progDesc "" | ||
30 | <> header "gettorrent - get torrent file by infohash" | ||
31 | ) | ||
32 | |||
33 | getTorrent :: Params -> IO () | ||
34 | getTorrent = print | ||
35 | |||
36 | main :: IO () | ||
37 | 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 | |||
122 | 122 | ||
123 | $(deriveJSON omitRecordPrefix ''NodeAddr) | 123 | $(deriveJSON omitRecordPrefix ''NodeAddr) |
124 | 124 | ||
125 | instance Read (NodeAddr IPv4) where | ||
126 | readsPrec i x = [ (fromPeerAddr a, s) | (a, s) <- readsPrec i x ] | ||
127 | |||
125 | -- | @127.0.0.1:6882@ | 128 | -- | @127.0.0.1:6882@ |
126 | instance Default (NodeAddr IPv4) where | 129 | instance Default (NodeAddr IPv4) where |
127 | def = "127.0.0.1:6882" | 130 | 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 | |||
251 | = PeerAddr Nothing hostAddr portNum | 251 | = PeerAddr Nothing hostAddr portNum |
252 | | otherwise = error $ "fromString: unable to parse (PeerAddr IPv4): " ++ str | 252 | | otherwise = error $ "fromString: unable to parse (PeerAddr IPv4): " ++ str |
253 | 253 | ||
254 | instance Read (PeerAddr IPv4) where | ||
255 | readsPrec i = RP.readP_to_S $ do | ||
256 | ipv4 <- RP.readS_to_P (readsPrec i) | ||
257 | _ <- RP.char ':' | ||
258 | port <- toEnum <$> RP.readS_to_P (readsPrec i) | ||
259 | return $ PeerAddr Nothing ipv4 port | ||
260 | |||
254 | readsIPv6_port :: String -> [((IPv6, PortNumber), String)] | 261 | readsIPv6_port :: String -> [((IPv6, PortNumber), String)] |
255 | readsIPv6_port = RP.readP_to_S $ do | 262 | readsIPv6_port = RP.readP_to_S $ do |
256 | ip <- RP.char '[' *> (RP.readS_to_P reads) <* RP.char ']' | 263 | ip <- RP.char '[' *> (RP.readS_to_P reads) <* RP.char ']' |