From 8ad1c7a539f570775338f354c0d82f69a40e5ac9 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 12 Sep 2015 23:24:28 +0530
Subject: [PATCH] preparations to introduce Metainfo as an environment

---
 src/FuncTorrent/Metainfo.hs |  1 -
 src/FuncTorrent/Peer.hs     |  4 ++--
 src/FuncTorrent/Tracker.hs  | 14 +++++++-------
 src/main/Main.hs            |  4 ++--
 4 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/FuncTorrent/Metainfo.hs b/src/FuncTorrent/Metainfo.hs
index 21bd532..2c5a725 100644
--- a/src/FuncTorrent/Metainfo.hs
+++ b/src/FuncTorrent/Metainfo.hs
@@ -65,7 +65,6 @@ mkMetaInfo (Bdict m)  =
              , encoding     = bstrToString  =<< encoding'
              , infoHash     = hash . encode $ (m ! "info")
              }
-
 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
 
 getAnnounceList :: Maybe BVal -> [String]
diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index 9f1aa2a..a7ce781 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -117,8 +117,8 @@ updatePieceAvailability pieceStatus p pieceList =
                        then (pd { peers = p : peers pd })
                        else pd) pieceStatus
 
-handlePeerMsgs :: Peer -> Metainfo -> String -> IO ()
-handlePeerMsgs p m peerId = do
+handlePeerMsgs :: Peer -> String -> Metainfo -> IO ()
+handlePeerMsgs p peerId m = do
   h <- connectToPeer p
   doHandshake h p (infoHash m) peerId
   let pstate = toPeerState h p False False True True
diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs
index 425b5d4..206e1b8 100644
--- a/src/FuncTorrent/Tracker.hs
+++ b/src/FuncTorrent/Tracker.hs
@@ -65,12 +65,12 @@ mkTrackerResponse resp =
           where (ip', port') = splitAt 4 peer
 
 -- | Connect to a tracker and get peer info
-tracker :: Metainfo -> String -> IO ByteString
-tracker m peer_id = get (head . announceList $ m) $ mkArgs m peer_id
+tracker :: String -> Metainfo -> IO ByteString
+tracker peer_id m = get (head . announceList $ m) $ mkArgs peer_id m
 
-getTrackerResponse :: Metainfo -> String -> IO (Either ByteString TrackerResponse)
-getTrackerResponse m peerId = do
-  resp <- tracker m peerId
+getTrackerResponse ::  String -> Metainfo -> IO (Either ByteString TrackerResponse)
+getTrackerResponse peerId m = do
+  resp <- tracker peerId m
   case decode resp of
    Right trackerInfo -> return $ mkTrackerResponse trackerInfo
    Left e -> return $ Left (pack (show e))
@@ -91,8 +91,8 @@ urlEncodeHash bs = concatMap (encode' . unpack) (splitN 2 bs)
 
 -- | Make arguments that should be posted to tracker.
 -- This is a separate pure function for testability.
-mkArgs :: Metainfo -> String -> [(String, ByteString)]
-mkArgs m peer_id = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $ m),
+mkArgs :: String -> Metainfo -> [(String, ByteString)]
+mkArgs peer_id m = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $ m),
                     ("peer_id", pack . urlEncode $ peer_id),
                     ("port", "6881"),
                     ("uploaded", "0"),
diff --git a/src/main/Main.hs b/src/main/Main.hs
index 3aa6587..b86f2e4 100644
--- a/src/main/Main.hs
+++ b/src/main/Main.hs
@@ -49,11 +49,11 @@ main = do
        log "Trying to fetch peers"
 
        log $ "Trackers: " ++ head (announceList m)
-       trackerResp <- getTrackerResponse m peerId
+       trackerResp <- getTrackerResponse peerId m
        case  trackerResp of
         Left e -> log $ "Error" ++ unpack e
         Right peerList -> do
           log $ "Peers List : " ++ (show . peers $ peerList)
           let p1 = head (peers peerList)
-          handlePeerMsgs p1 m peerId
+          handlePeerMsgs p1 peerId m
     logStop logR
-- 
2.45.2