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 -> Metainfo -> IO ByteString
-tracker peer_id m = get (head . announceList $ m) $ mkArgs peer_id m
+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 -> Metainfo -> IO (Either ByteString TrackerResponse)
-getTrackerResponse peerId m = do
- resp <- tracker peerId m
+getTrackerResponse :: String -> ReaderT Metainfo IO (Either ByteString TrackerResponse)
+getTrackerResponse peerId = do
+ m <- ask
+ resp <- liftIO $ runReaderT (tracker peerId) m
case decode resp of
- Right trackerInfo -> return $ mkTrackerResponse trackerInfo
+ Right trackerInfo -> liftIO $ return $ mkTrackerResponse trackerInfo
Left e -> return $ Left (pack (show e))
--- | URL encode hash as per RFC1738
("left", pack . show . lengthInBytes $ info m),
("compact", "1"),
("event", "started")]
+
+
module Main where
import Prelude hiding (log, length, readFile, getContents)
+
+import Control.Monad.Reader(runReaderT)
import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack)
import System.Environment (getArgs)
import System.Exit (exitSuccess)
log "Trying to fetch peers"
log $ "Trackers: " ++ head (announceList m)
- trackerResp <- getTrackerResponse peerId m
+ trackerResp <- runReaderT (getTrackerResponse peerId) m
case trackerResp of
Left e -> log $ "Error" ++ unpack e
Right peerList -> do