summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire/Status.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-06 23:32:49 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-06 23:32:49 +0400
commit50454e4cc0af670a3ad68efd828aa505811ed28a (patch)
tree96fcda85d93d4381f9e420cb159269148e7d2275 /src/Network/BitTorrent/PeerWire/Status.hs
parent99e771564a1433029ce8a8ce4db8282fc217a1c4 (diff)
- Remove Peer.* modules.
I do not expect that this modules will grow later, so they are merged with Network.BitTorrent.Peer now. We also avoid one "reexport only" module this way.
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Status.hs')
-rw-r--r--src/Network/BitTorrent/PeerWire/Status.hs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Status.hs b/src/Network/BitTorrent/PeerWire/Status.hs
new file mode 100644
index 00000000..806ba77d
--- /dev/null
+++ b/src/Network/BitTorrent/PeerWire/Status.hs
@@ -0,0 +1,65 @@
1-- |
2-- Copyright : (c) Sam T. 2013
3-- License : MIT
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8module Network.BitTorrent.Peer.Status
9 ( PeerStatus(..)
10 , setChoking, setInterested
11 , initPeerStatus
12
13 , SessionStatus(..)
14 , initSessionStatus
15 , setClientStatus, setPeerStatus
16 , canUpload, canDownload
17
18 -- * Defaults
19 , defaultUnchokeSlots
20 ) where
21
22data PeerStatus = PeerStatus {
23 psChoking :: Bool
24 , psInterested :: Bool
25 }
26
27-- | Any session between peers starts as choking and not interested.
28initPeerStatus :: PeerStatus
29initPeerStatus = PeerStatus True False
30
31setChoking :: Bool -> PeerStatus -> PeerStatus
32setChoking b ps = ps { psChoking = b }
33
34setInterested :: Bool -> PeerStatus -> PeerStatus
35setInterested b ps = ps { psInterested = b }
36
37
38
39data SessionStatus = SessionStatus {
40 seClientStatus :: PeerStatus
41 , sePeerStatus :: PeerStatus
42 }
43
44initSessionStatus :: SessionStatus
45initSessionStatus = SessionStatus initPeerStatus initPeerStatus
46
47setClientStatus :: (PeerStatus -> PeerStatus) -> SessionStatus -> SessionStatus
48setClientStatus f ss = ss { seClientStatus = f (seClientStatus ss) }
49
50setPeerStatus :: (PeerStatus -> PeerStatus) -> SessionStatus -> SessionStatus
51setPeerStatus f ss = ss { sePeerStatus = f (sePeerStatus ss) }
52
53-- | Can the /client/ to upload to the /peer/?
54canUpload :: SessionStatus -> Bool
55canUpload SessionStatus { seClientStatus = client, sePeerStatus = peer} =
56 psInterested peer && not (psChoking client)
57
58-- | Can the /client/ download from the /peer/?
59canDownload :: SessionStatus -> Bool
60canDownload SessionStatus { seClientStatus = client, sePeerStatus = peer } =
61 psInterested client && not (psChoking peer)
62
63-- | Indicates have many peers are allowed to download from the client.
64defaultUnchokeSlots :: Int
65defaultUnchokeSlots = 4 \ No newline at end of file