, encoding = bstrToString =<< encoding'
, infoHash = hash . encode $ (m ! "info")
}
-
mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"
getAnnounceList :: Maybe BVal -> [String]
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
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))
-- | 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"),
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