summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-21 00:07:49 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-21 00:07:49 +0400
commit2b7044a4c1034e36e1762905fa6ca1a515afc0bf (patch)
tree2ae2ce68924be81ed98c39bc88d2653856b440c3
parent025a04ff3a3d9e936a27692b214e8c871260a3e7 (diff)
Add canDownload & canUpload functions
-rw-r--r--src/Data/Torrent/Progress.hs16
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs4
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
32import Control.Applicative 36import Control.Applicative
@@ -36,6 +40,7 @@ import Data.Default
36import Data.List as L 40import Data.List as L
37import Data.Monoid 41import Data.Monoid
38import Data.Serialize as S 42import Data.Serialize as S
43import Data.Ratio
39import Data.Word 44import Data.Word
40 45
41 46
@@ -111,3 +116,14 @@ enqueuedProgress amount = left +~ fromIntegral amount
111dequeuedProgress :: Integer -> Progress -> Progress 116dequeuedProgress :: Integer -> Progress -> Progress
112dequeuedProgress amount = left -~ fromIntegral amount 117dequeuedProgress amount = left -~ fromIntegral amount
113{-# INLINE dequeuedProgress #-} 118{-# INLINE dequeuedProgress #-}
119
120ri2rw64 :: Ratio Int -> Ratio Word64
121ri2rw64 x = fromIntegral (numerator x) % fromIntegral (denominator x)
122
123-- | Check global /download/ limit by uploaded \/ downloaded ratio.
124canDownload :: Ratio Int -> Progress -> Bool
125canDownload limit Progress {..} = _uploaded % _downloaded > ri2rw64 limit
126
127-- | Check global /upload/ limit by downloaded \/ uploaded ratio.
128canUpload :: Ratio Int -> Progress -> Bool
129canUpload 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 #-}