From: Ramakrishnan Muthukrishnan Date: Sun, 6 Dec 2015 09:09:42 +0000 (+0530) Subject: rename tracker response function, Utils, catch exceptions. X-Git-Url: https://git.rkrishnan.org/?a=commitdiff_plain;h=1e8250682c95dc677d54a73f8765bd94985d4a38;p=functorrent.git rename tracker response function, Utils, catch exceptions. --- diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index e0752e9..125a926 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -159,9 +159,9 @@ initialTrackerState sz = do , downloaded = down , left = sz } --- | Deserialize tracker response -mkTrackerResponse :: BVal -> Either ByteString TrackerResponse -mkTrackerResponse resp = +-- | Deserialize HTTP tracker response +parseTrackerResponse :: BVal -> Either ByteString TrackerResponse +parseTrackerResponse resp = case lookup "failure reason" body of Just (Bstr err) -> Left err Just _ -> Left "Unknown failure" @@ -231,7 +231,7 @@ trackerLoop port peerId m st = do case Benc.decode resp of Left e -> return $ pack (show e) Right trackerInfo -> - case mkTrackerResponse trackerInfo of + case parseTrackerResponse trackerInfo of Left e -> return e Right tresp -> do _ <- threadDelay $ fromIntegral (interval tresp) diff --git a/src/FuncTorrent/Utils.hs b/src/FuncTorrent/Utils.hs index b1db8d0..2a27976 100644 --- a/src/FuncTorrent/Utils.hs +++ b/src/FuncTorrent/Utils.hs @@ -11,7 +11,7 @@ module FuncTorrent.Utils import Prelude hiding (writeFile, take) import qualified Crypto.Hash.SHA1 as SHA1 (hash) -import Control.Monad (unless) +import Control.Exception.Base (IOException, try) import Data.ByteString (ByteString, writeFile, hPut, hGet, take) import qualified Data.ByteString.Char8 as BC import System.IO (withFile, hSeek, IOMode(..), SeekMode(..)) @@ -26,11 +26,14 @@ splitNum n d | n == 0 = [] | n < d = [n] | otherwise = d : splitNum (n - d) d -createDummyFile :: FilePath -> Int -> IO () +createDummyFile :: FilePath -> Int -> IO (Either IOException ()) createDummyFile path size = do dfe <- doesFileExist path - unless dfe $ - writeFile path (BC.replicate size '\0') + if not dfe + then do + try $ writeFile path (BC.replicate size '\0') + else + return $ Right () -- write into a file at a specific offet writeFileAtOffset :: FilePath -> Integer -> ByteString -> IO ()