diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-21 00:07:49 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-21 00:07:49 +0400 |
commit | 2b7044a4c1034e36e1762905fa6ca1a515afc0bf (patch) | |
tree | 2ae2ce68924be81ed98c39bc88d2653856b440c3 /src/Data/Torrent | |
parent | 025a04ff3a3d9e936a27692b214e8c871260a3e7 (diff) |
Add canDownload & canUpload functions
Diffstat (limited to 'src/Data/Torrent')
-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 | ||