]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
refactor PeerID and associated functions.
[functorrent.git] / src / Main.hs
index 16b2ca5b9f3a0e82b5c8def0e4a3e2f59b61ea45..bb50a8be38f4116ea46d67c976c6711dca086e6f 100644 (file)
@@ -1,21 +1,43 @@
 module Main where
 
 import System.Environment (getArgs)
+import System.Exit
 import qualified Data.ByteString.Char8 as BC
 import qualified Bencode as Benc
 import qualified Metainfo as MInfo
-import Text.ParserCombinators.Parsec
+import qualified Tracker as T
+import qualified Text.ParserCombinators.Parsec as Parsec
+import qualified Peer as P
+import Data.Functor
 
-printError :: ParseError -> IO ()
-printError e = putStrLn "parse error"
+printError :: Parsec.ParseError -> IO ()
+printError e = putStrLn $ "parse error: " ++ show e
+
+peerId :: String
+peerId = "-HS0001-*-*-20150215"
+
+exit :: IO BC.ByteString
+exit = exitSuccess
+
+usage :: IO ()
+usage = putStrLn "usage: functorrent torrent-file"
+
+parse :: [String] -> IO BC.ByteString
+parse [] = usage >> exit
+parse [a] = BC.readFile a
+parse _ = exit
 
 main :: IO ()
 main = do
-  args <- getArgs
-  torrentStr <- BC.readFile (head args)
-  case (Benc.decode torrentStr) of
-   Right d -> case (MInfo.mkMetaInfo d) of
-               Nothing -> putStrLn "parse error"
-               Just m -> putStrLn (show m)
-   Left e -> printError e
-  putStrLn "done"
+    args <- getArgs
+    torrentStr <- parse args
+    case Benc.decode torrentStr of
+      Right d ->
+          case MInfo.mkMetaInfo d of
+            Nothing -> putStrLn "parse error"
+            Just m -> do
+              let len = MInfo.lengthInBytes (MInfo.info m)
+              body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d peerId len)
+              print (P.getPeers (P.getPeerResponse body))
+      Left e -> printError e
+    putStrLn "done"