{-# LANGUAGE OverloadedStrings #-}
module FuncTorrent.Peer
(Peer(..),
- handShake
+ handShake,
+ msgLoop
) where
import Prelude hiding (lookup, concat, replicate, splitAt)
import Data.Binary (Binary(..), decode)
import Data.Binary.Put (putWord32be, putWord16be, putWord8)
import Data.Binary.Get (getWord32be, getWord16be, getWord8)
-import Control.Monad (replicateM, liftM)
+import Control.Monad (replicateM, liftM, forever)
import Control.Applicative ((<$>), liftA3)
type ID = String
-- loop1 :: shake hands with all peers, find out the pieces they have, form PieceData.
-- recvMsg :: Peer -> Handle -> Msg
+
+msgLoop :: Handle -> IO ()
+msgLoop h = forever $ do
+ msg <- getMsg h
+ putStrLn $ "got a " ++ (show msg)
import FuncTorrent.Bencode (decode)
import FuncTorrent.Logger (initLogger, logMessage, logStop)
import FuncTorrent.Metainfo (Info(..), Metainfo(..), mkMetaInfo)
-import FuncTorrent.Peer (handShake)
+import FuncTorrent.Peer (handShake, msgLoop)
import FuncTorrent.Tracker (tracker, peers, mkTrackerResponse)
logError :: ParseError -> (String -> IO ()) -> IO ()
Right peerResp -> do
log $ "Peers List : " ++ (show . peers $ peerResp)
let p1 = head (peers peerResp)
- msg <- handShake p1 (infoHash m) peerId
- log $ "handshake: " ++ (show msg)
- return ()
+ h <- handShake p1 (infoHash m) peerId
+ log $ "handshake"
+ msgLoop h
Left e -> log $ "Error" ++ unpack e
Left e -> logError e log