From 0b25993954633638e1e2dc3935049739d91c163e Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Wed, 6 May 2015 15:17:19 +0530
Subject: [PATCH] add a simple message loop to print out received msgs

This is just to study what messages come after the handshake.
---
 src/FuncTorrent.hs      |  1 +
 src/FuncTorrent/Peer.hs | 10 ++++++++--
 src/Main.hs             |  8 ++++----
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/FuncTorrent.hs b/src/FuncTorrent.hs
index 8289da5..cd6473c 100644
--- a/src/FuncTorrent.hs
+++ b/src/FuncTorrent.hs
@@ -8,6 +8,7 @@ module FuncTorrent
      decode,
      encode,
      handShake,
+     msgLoop,
      initLogger,
      logMessage,
      logStop,
diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 29d5714..aeba52f 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -1,7 +1,8 @@
 {-# LANGUAGE OverloadedStrings #-}
 module FuncTorrent.Peer
     (Peer(..),
-     handShake
+     handShake,
+     msgLoop
     ) where
 
 import Prelude hiding (lookup, concat, replicate, splitAt)
@@ -14,7 +15,7 @@ import Network (connectTo, PortID(..))
 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
@@ -143,3 +144,8 @@ getMsg h = do
 
 -- 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)
diff --git a/src/Main.hs b/src/Main.hs
index 65e2dd7..afa0bd3 100644
--- a/src/Main.hs
+++ b/src/Main.hs
@@ -11,7 +11,7 @@ import Text.ParserCombinators.Parsec (ParseError)
 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 ()
@@ -64,9 +64,9 @@ main = do
                       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
 
-- 
2.45.2