{-# LANGUAGE OverloadedStrings #-}
module FuncTorrent.Tracker
(TrackerResponse(..),
- mkArgs,
- getTrackerResponse,
- urlEncodeHash
+ getTrackerResponse
) where
import Prelude hiding (lookup, splitAt)
-import Control.Monad.IO.Class (liftIO)
-import Control.Monad.Reader (ReaderT, ask, runReaderT)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 as BC (pack, unpack, splitAt)
import Data.Char (chr)
where (ip', port') = splitAt 4 peer
-- | Connect to a tracker and get peer info
-tracker :: String -> ReaderT Metainfo IO ByteString
-tracker peer_id = do
- m <- ask
- let args = mkArgs peer_id m
- liftIO $ get (head . announceList $ m) args
-
-getTrackerResponse :: String -> ReaderT Metainfo IO (Either ByteString TrackerResponse)
-getTrackerResponse peerId = do
- m <- ask
- resp <- liftIO $ runReaderT (tracker peerId) m
+tracker :: String -> Metainfo -> IO ByteString
+tracker peer_id m = do
+ get (head . announceList $ m) $ mkArgs peer_id m
+
+getTrackerResponse :: String -> Metainfo -> IO (Either ByteString TrackerResponse)
+getTrackerResponse peerId m = do
+ resp <- tracker peerId m
case decode resp of
- Right trackerInfo -> liftIO $ return $ mkTrackerResponse trackerInfo
+ Right trackerInfo -> return $ mkTrackerResponse trackerInfo
Left e -> return $ Left (pack (show e))
--- | URL encode hash as per RFC1738