diff options
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Status.hs')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Status.hs | 67 |
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 #-} | ||
2 | module 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 | |||
22 | import Control.Lens | ||
23 | import Data.Aeson.TH | ||
24 | import Data.List as L | ||
25 | import Data.Default | ||
26 | |||
27 | -- | | ||
28 | data PeerStatus = PeerStatus { | ||
29 | _choking :: !Bool | ||
30 | , _interested :: !Bool | ||
31 | } deriving (Show, Eq) | ||
32 | |||
33 | $(makeLenses ''PeerStatus) | ||
34 | $(deriveJSON L.tail ''PeerStatus) | ||
35 | |||
36 | instance Default PeerStatus where | ||
37 | def = PeerStatus True False | ||
38 | |||
39 | -- | | ||
40 | data SessionStatus = SessionStatus { | ||
41 | _clientStatus :: !PeerStatus | ||
42 | , _peerStatus :: !PeerStatus | ||
43 | } deriving (Show, Eq) | ||
44 | |||
45 | $(makeLenses ''SessionStatus) | ||
46 | $(deriveJSON L.tail ''SessionStatus) | ||
47 | |||
48 | instance Default SessionStatus where | ||
49 | def = SessionStatus def def | ||
50 | |||
51 | -- | Can the /client/ transfer to the /peer/? | ||
52 | canUpload :: SessionStatus -> Bool | ||
53 | canUpload SessionStatus {..} | ||
54 | = _interested _peerStatus && not (_choking _clientStatus) | ||
55 | |||
56 | -- | Can the /client/ transfer from the /peer/? | ||
57 | canDownload :: SessionStatus -> Bool | ||
58 | canDownload SessionStatus {..} | ||
59 | = _interested _clientStatus && not (_choking _peerStatus) | ||
60 | |||
61 | inverseStatus :: SessionStatus -> SessionStatus | ||
62 | inverseStatus SessionStatus {..} = SessionStatus _peerStatus _clientStatus | ||
63 | |||
64 | -- | Indicates how many peers are allowed to download from the client | ||
65 | -- by default. | ||
66 | defaultUnchokeSlots :: Int | ||
67 | defaultUnchokeSlots = 4 \ No newline at end of file | ||