summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal9
-rw-r--r--examples/GetTorrent.hs37
-rw-r--r--src/Network/BitTorrent/Core/Node.hs3
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs7
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
240executable 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
240executable client-example 249executable 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 @@
1module Main (main) where
2import Data.Default
3
4import Options.Applicative
5import Data.Torrent.InfoHash
6import Network.BitTorrent.Core
7
8
9data Params = Params
10 { infohash :: InfoHash
11 , thisNode :: NodeAddr IPv4
12 , bootNode :: NodeAddr IPv4
13 } deriving Show
14
15paramsParser :: Parser Params
16paramsParser = 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
26programInfo :: ParserInfo Params
27programInfo = info (helper <*> paramsParser)
28 ( fullDesc
29 <> progDesc ""
30 <> header "gettorrent - get torrent file by infohash"
31 )
32
33getTorrent :: Params -> IO ()
34getTorrent = print
35
36main :: IO ()
37main = 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
125instance 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@
126instance Default (NodeAddr IPv4) where 129instance 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
254instance 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
254readsIPv6_port :: String -> [((IPv6, PortNumber), String)] 261readsIPv6_port :: String -> [((IPv6, PortNumber), String)]
255readsIPv6_port = RP.readP_to_S $ do 262readsIPv6_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 ']'