From: Ramakrishnan Muthukrishnan Date: Sat, 12 Sep 2015 17:54:28 +0000 (+0530) Subject: preparations to introduce Metainfo as an environment X-Git-Url: https://git.rkrishnan.org/...?a=commitdiff_plain;h=8ad1c7a539f570775338f354c0d82f69a40e5ac9;p=functorrent.git preparations to introduce Metainfo as an environment --- 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