]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
Handle error cases from the tracker
[functorrent.git] / src / Main.hs
index 32dc41a83099fe89bb8442e7a8f5b2554c67b793..77573313b61cc4d8c5a9da80a365321687aa64ca 100644 (file)
@@ -1,45 +1,73 @@
+{-# LANGUAGE OverloadedStrings #-}
 module Main where
 
+import Prelude hiding (length, readFile, writeFile)
+import Data.ByteString.Char8 (ByteString, readFile, writeFile, length, unpack)
 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 qualified Tracker as T
-import qualified Text.ParserCombinators.Parsec as Parsec
-import qualified Peer as P
-import Data.Functor
-
-printError :: Parsec.ParseError -> IO ()
-printError e = putStrLn $ "parse error: " ++ show e
+import System.Exit (exitSuccess)
+import System.Directory (doesFileExist)
+import Text.ParserCombinators.Parsec (ParseError)
+
+import FuncTorrent.Bencode (decode, BVal(..))
+import FuncTorrent.Logger (initLogger, logMessage, logStop)
+import FuncTorrent.Metainfo (announce, lengthInBytes, mkMetaInfo, info, name)
+import FuncTorrent.Peer (peers, mkPeerResp, handShakeMsg)
+import FuncTorrent.Tracker (connect, prepareRequest)
+
+logError :: ParseError -> (String -> IO ()) -> IO ()
+logError e logMsg = logMsg $ "parse error: \n" ++ show e
 
 peerId :: String
 peerId = "-HS0001-*-*-20150215"
 
-exit :: IO BC.ByteString
+exit :: IO ByteString
 exit = exitSuccess
 
 usage :: IO ()
 usage = putStrLn "usage: functorrent torrent-file"
 
-parse :: [String] -> IO BC.ByteString
+parse :: [String] -> IO ByteString
 parse [] = usage >> exit
-parse [a] = BC.readFile a
+parse [a] = do
+  fileExist <- doesFileExist a
+  if fileExist
+    then readFile a
+    else error "file does not exist"
 parse _ = exit
 
 main :: IO ()
 main = do
     args <- getArgs
+    logR <- initLogger
+    let logMsg = logMessage logR
+    logMsg $ "Parsing input file: " ++ concat args
     torrentStr <- parse args
-    case Benc.decode torrentStr of
+    case decode torrentStr of
       Right d ->
-          case MInfo.mkMetaInfo d of
-            Nothing -> putStrLn "parse error"
+          case mkMetaInfo d of
+            Nothing -> logMsg "parse error"
             Just m -> do
-              let len = MInfo.lengthInBytes (MInfo.info m)
-                  (Benc.Bdict d') = d
-              body <- BC.pack <$> T.connect (MInfo.announce m) (T.prepareRequest d' peerId len)
-              print (P.getPeers (P.getPeerResponse body))
-              print (BC.length (P.handShakeMsg d' peerId))
-      Left e -> printError e
-    putStrLn "done"
+              logMsg "Input File OK"
+
+              let len = lengthInBytes $ info m
+                  (Bdict d') = d
+
+              logMsg "Trying to fetch peers: "
+              response <- connect (announce m) (prepareRequest d' peerId len)
+
+              let hsMsgLen = show $ length $ handShakeMsg d' peerId
+              logMsg $ "Hand-shake message length : " ++ hsMsgLen
+
+              -- TODO: Write to ~/.functorrent/caches
+              writeFile (name (info m) ++ ".cache") response
+
+              case decode response of
+                Right trackerInfo ->
+                    case mkPeerResp trackerInfo of
+                      Right peerResp ->
+                          logMsg $ "Peers List : " ++ (show . peers $ peerResp)
+                      Left e -> logMsg $ "Error" ++ unpack e
+                Left e -> logError e logMsg
+
+      Left e -> logError e logMsg
+    logStop logR