]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/Main.hs
bugfix: use the previous piece length times number of pieces to calculate offset
[functorrent.git] / src / Main.hs
index f86e32c6849399d479ffb8f782ab821dd670f54f..3aa65874efa581464d016899bcfcaf60356ae591 100644 (file)
@@ -1,15 +1,15 @@
 {-# LANGUAGE OverloadedStrings #-}
 module Main where
 
-import Prelude hiding (log, length, readFile)
-import Data.ByteString.Char8 (ByteString, readFile, unpack)
+import Prelude hiding (log, length, readFile, getContents)
+import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack)
 import System.Environment (getArgs)
 import System.Exit (exitSuccess)
 import System.Directory (doesFileExist)
 
 import FuncTorrent.Logger (initLogger, logMessage, logStop)
 import FuncTorrent.Metainfo (Info(..), Metainfo(..), torrentToMetainfo)
-import FuncTorrent.Peer (handShake, msgLoop)
+import FuncTorrent.Peer (handlePeerMsgs)
 import FuncTorrent.Tracker (peers, getTrackerResponse)
 
 logError :: String -> (String -> IO ()) -> IO ()
@@ -25,7 +25,7 @@ usage :: IO ()
 usage = putStrLn "usage: functorrent torrent-file"
 
 parse :: [String] -> IO ByteString
-parse [] = usage >> exit
+parse [] = getContents
 parse [a] = do
   fileExist <- doesFileExist a
   if fileExist
@@ -41,7 +41,8 @@ main = do
     log "Starting up functorrent"
     log $ "Parsing arguments " ++ concat args
     torrentStr <- parse args
-    case (torrentToMetainfo torrentStr) of
+    case torrentToMetainfo torrentStr of
+     Left e -> logError e log
      Right m -> do
        log "Input File OK"
        log $ "Downloading file : " ++ name (info m)
@@ -50,12 +51,9 @@ main = do
        log $ "Trackers: " ++ head (announceList m)
        trackerResp <- getTrackerResponse m peerId
        case  trackerResp of
+        Left e -> log $ "Error" ++ unpack e
         Right peerList -> do
           log $ "Peers List : " ++ (show . peers $ peerList)
           let p1 = head (peers peerList)
-          h <- handShake p1 (infoHash m) peerId
-          log $ "handshake"
-          msgLoop h (pieces (info m))
-        Left e -> log $ "Error" ++ unpack e
-     Left e -> logError e log
+          handlePeerMsgs p1 m peerId
     logStop logR