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 | |
parent | 025a04ff3a3d9e936a27692b214e8c871260a3e7 (diff) |
Add canDownload & canUpload functions
-rw-r--r-- | src/Data/Torrent/Progress.hs | 16 | ||||
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 4 |
2 files changed, 18 insertions, 2 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 | ||
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 84b1e1f6..0eeaae9d 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs | |||
@@ -5,8 +5,8 @@ | |||
5 | -- Stability : experimental | 5 | -- Stability : experimental |
6 | -- Portability : portable | 6 | -- Portability : portable |
7 | -- | 7 | -- |
8 | -- * 'PeerAddr' is used to represent peer location. Currently it's | 8 | -- 'PeerAddr' is used to represent peer location. Currently it's |
9 | -- just peer IP and peer port but this might be changed later. | 9 | -- just peer IP and peer port but this might be changed later. |
10 | -- | 10 | -- |
11 | {-# LANGUAGE TemplateHaskell #-} | 11 | {-# LANGUAGE TemplateHaskell #-} |
12 | {-# LANGUAGE StandaloneDeriving #-} | 12 | {-# LANGUAGE StandaloneDeriving #-} |