{-# LANGUAGE OverloadedStrings #-}
module FuncTorrent.Peer
(Peer(..),
- handShake,
- msgLoop
+ handlePeerMsgs
) where
import Prelude hiding (lookup, concat, replicate, splitAt, empty, writeFile)
import Data.Bits
import Data.Word (Word8)
+import FuncTorrent.Metainfo (Info(..), Metainfo(..))
type ID = String
type IP = String
type Port = Integer
putStrLn (show pieceList)
-- download each of the piece in order
_ -> putStrLn (show msg)
+
+handlePeerMsgs :: Peer -> Metainfo -> String -> (String -> IO ()) -> IO ()
+handlePeerMsgs p m peerId logFn = do
+ h <- handShake p (infoHash m) peerId
+ logFn $ "handShake"
+ msgLoop h (pieces (info m))
+
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 ()
log $ "Parsing arguments " ++ concat args
torrentStr <- parse args
case (torrentToMetainfo torrentStr) of
+ Left e -> logError e log
Right m -> do
log "Input File OK"
log $ "Downloading file : " ++ name (info m)
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 log
logStop logR