diff options
Diffstat (limited to 'src/Data/Torrent/Progress.hs')
-rw-r--r-- | src/Data/Torrent/Progress.hs | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/Data/Torrent/Progress.hs b/src/Data/Torrent/Progress.hs index bbfe6e47..c42af248 100644 --- a/src/Data/Torrent/Progress.hs +++ b/src/Data/Torrent/Progress.hs | |||
@@ -27,6 +27,10 @@ module Data.Torrent.Progress | |||
27 | , enqueuedProgress | 27 | , enqueuedProgress |
28 | , uploadedProgress | 28 | , uploadedProgress |
29 | , dequeuedProgress | 29 | , dequeuedProgress |
30 | |||
31 | -- * Query | ||
32 | , canDownload | ||
33 | , canUpload | ||
30 | ) where | 34 | ) where |
31 | 35 | ||
32 | import Control.Applicative | 36 | import Control.Applicative |
@@ -36,6 +40,7 @@ import Data.Default | |||
36 | import Data.List as L | 40 | import Data.List as L |
37 | import Data.Monoid | 41 | import Data.Monoid |
38 | import Data.Serialize as S | 42 | import Data.Serialize as S |
43 | import Data.Ratio | ||
39 | import Data.Word | 44 | import Data.Word |
40 | 45 | ||
41 | 46 | ||
@@ -111,3 +116,14 @@ enqueuedProgress amount = left +~ fromIntegral amount | |||
111 | dequeuedProgress :: Integer -> Progress -> Progress | 116 | dequeuedProgress :: Integer -> Progress -> Progress |
112 | dequeuedProgress amount = left -~ fromIntegral amount | 117 | dequeuedProgress amount = left -~ fromIntegral amount |
113 | {-# INLINE dequeuedProgress #-} | 118 | {-# INLINE dequeuedProgress #-} |
119 | |||
120 | ri2rw64 :: Ratio Int -> Ratio Word64 | ||
121 | ri2rw64 x = fromIntegral (numerator x) % fromIntegral (denominator x) | ||
122 | |||
123 | -- | Check global /download/ limit by uploaded \/ downloaded ratio. | ||
124 | canDownload :: Ratio Int -> Progress -> Bool | ||
125 | canDownload limit Progress {..} = _uploaded % _downloaded > ri2rw64 limit | ||
126 | |||
127 | -- | Check global /upload/ limit by downloaded \/ uploaded ratio. | ||
128 | canUpload :: Ratio Int -> Progress -> Bool | ||
129 | canUpload limit Progress {..} = _downloaded % _uploaded > ri2rw64 limit | ||