From: Ramakrishnan Muthukrishnan Date: Thu, 16 Jul 2015 08:32:11 +0000 (+0530) Subject: Main.hs: move peer handling into Peer module X-Git-Url: https://git.rkrishnan.org/simplejson/components/%22file://%22%3C?a=commitdiff_plain;h=690290dc759dc20b7818a5c2f761afb94b2f80e5;p=functorrent.git Main.hs: move peer handling into Peer module --- diff --git a/src/FuncTorrent.hs b/src/FuncTorrent.hs index 576e6b5..2cded5b 100644 --- a/src/FuncTorrent.hs +++ b/src/FuncTorrent.hs @@ -7,8 +7,7 @@ module FuncTorrent getTrackerResponse, decode, encode, - handShake, - msgLoop, + handlePeerMsgs, initLogger, logMessage, logStop, diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs index d04d886..817a9cd 100644 --- a/src/FuncTorrent/Peer.hs +++ b/src/FuncTorrent/Peer.hs @@ -1,8 +1,7 @@ {-# LANGUAGE OverloadedStrings #-} module FuncTorrent.Peer (Peer(..), - handShake, - msgLoop + handlePeerMsgs ) where import Prelude hiding (lookup, concat, replicate, splitAt, empty, writeFile) @@ -20,6 +19,7 @@ import Control.Applicative ((<$>), liftA3) import Data.Bits import Data.Word (Word8) +import FuncTorrent.Metainfo (Info(..), Metainfo(..)) type ID = String type IP = String type Port = Integer @@ -178,3 +178,10 @@ msgLoop h pieceHash = 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)) + diff --git a/src/Main.hs b/src/Main.hs index f86e32c..8171d38 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -9,7 +9,7 @@ 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 () @@ -42,6 +42,7 @@ main = do 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) @@ -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 log logStop logR