]> git.rkrishnan.org Git - functorrent.git/commitdiff
wip: Use reader to pass around metainfo
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 13 Sep 2015 07:15:35 +0000 (12:45 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 13 Sep 2015 07:15:35 +0000 (12:45 +0530)
functorrent.cabal
src/FuncTorrent/Tracker.hs
src/main/Main.hs

index b663ef9e7ea75f1fb31738ffe17a1f5ff515a2ae..31cbbdb62cc6d8c7836d2c80e6e39e5a6dd1195d 100644 (file)
@@ -45,7 +45,8 @@ library
                        QuickCheck,
                        safe,
                        tasty,
                        QuickCheck,
                        safe,
                        tasty,
-                       tasty-hunit
+                       tasty-hunit,
+                       transformers
 
 executable functorrent
   main-is:             Main.hs
 
 executable functorrent
   main-is:             Main.hs
index 206e1b8294c290e84605cbe611a82968fe616f6f..aac578c8aa7f77a05d020840b603dd05829ed540 100644 (file)
@@ -8,6 +8,8 @@ module FuncTorrent.Tracker
 
 import Prelude hiding (lookup, splitAt)
 
 
 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)
 import Data.ByteString (ByteString)
 import Data.ByteString.Char8 as BC (pack, unpack, splitAt)
 import Data.Char (chr)
@@ -65,14 +67,18 @@ mkTrackerResponse resp =
           where (ip', port') = splitAt 4 peer
 
 -- | Connect to a tracker and get peer info
           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
   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 e -> return $ Left (pack (show e))
 
 --- | URL encode hash as per RFC1738
@@ -100,3 +106,5 @@ mkArgs peer_id m = [("info_hash", pack . urlEncodeHash . B16.encode . infoHash $
                     ("left", pack . show . lengthInBytes $ info m),
                     ("compact", "1"),
                     ("event", "started")]
                     ("left", pack . show . lengthInBytes $ info m),
                     ("compact", "1"),
                     ("event", "started")]
+
+
index b86f2e4284b1990fb2fdb4532b288c08006fda21..b82d27a5f859acbc60c5fdb75d703cda80ab068a 100644 (file)
@@ -2,6 +2,8 @@
 module Main where
 
 import Prelude hiding (log, length, readFile, getContents)
 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)
 import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack)
 import System.Environment (getArgs)
 import System.Exit (exitSuccess)
@@ -49,7 +51,7 @@ main = do
        log "Trying to fetch peers"
 
        log $ "Trackers: " ++ head (announceList m)
        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
        case  trackerResp of
         Left e -> log $ "Error" ++ unpack e
         Right peerList -> do