summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange/Status.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Status.hs')
-rw-r--r--src/Network/BitTorrent/Exchange/Status.hs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Exchange/Status.hs b/src/Network/BitTorrent/Exchange/Status.hs
new file mode 100644
index 00000000..683ac594
--- /dev/null
+++ b/src/Network/BitTorrent/Exchange/Status.hs
@@ -0,0 +1,67 @@
1{-# LANGUAGE TemplateHaskell #-}
2module Network.BitTorrent.Exchange.Status
3 ( -- * Peer status
4 PeerStatus(..)
5 , choking
6 , interested
7
8 -- * Session status
9 , SessionStatus(..)
10 , clientStatus
11 , peerStatus
12
13 -- ** Query
14 , canUpload
15 , canDownload
16
17 -- * Extra
18 , inverseStatus
19 , defaultUnchokeSlots
20 ) where
21
22import Control.Lens
23import Data.Aeson.TH
24import Data.List as L
25import Data.Default
26
27-- |
28data PeerStatus = PeerStatus {
29 _choking :: !Bool
30 , _interested :: !Bool
31 } deriving (Show, Eq)
32
33$(makeLenses ''PeerStatus)
34$(deriveJSON L.tail ''PeerStatus)
35
36instance Default PeerStatus where
37 def = PeerStatus True False
38
39-- |
40data SessionStatus = SessionStatus {
41 _clientStatus :: !PeerStatus
42 , _peerStatus :: !PeerStatus
43 } deriving (Show, Eq)
44
45$(makeLenses ''SessionStatus)
46$(deriveJSON L.tail ''SessionStatus)
47
48instance Default SessionStatus where
49 def = SessionStatus def def
50
51-- | Can the /client/ transfer to the /peer/?
52canUpload :: SessionStatus -> Bool
53canUpload SessionStatus {..}
54 = _interested _peerStatus && not (_choking _clientStatus)
55
56-- | Can the /client/ transfer from the /peer/?
57canDownload :: SessionStatus -> Bool
58canDownload SessionStatus {..}
59 = _interested _clientStatus && not (_choking _peerStatus)
60
61inverseStatus :: SessionStatus -> SessionStatus
62inverseStatus SessionStatus {..} = SessionStatus _peerStatus _clientStatus
63
64-- | Indicates how many peers are allowed to download from the client
65-- by default.
66defaultUnchokeSlots :: Int
67defaultUnchokeSlots = 4 \ No newline at end of file