diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-05-06 04:53:59 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-05-06 04:53:59 +0400 |
commit | fe546e6c3926019efd614787f6c2e8cf12469aed (patch) | |
tree | a051e6ca14a91aa0c0f5986f5462a9a4cb95bf1e /src/Network | |
parent | ddc164bf13535354bfb1bd5483b96c25343d6620 (diff) |
+ Add Peer.Status module.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Peer.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/Peer/Status.hs | 65 |
2 files changed, 67 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Peer.hs b/src/Network/BitTorrent/Peer.hs index ff32feb5..660f146f 100644 --- a/src/Network/BitTorrent/Peer.hs +++ b/src/Network/BitTorrent/Peer.hs | |||
@@ -13,4 +13,5 @@ module Network.BitTorrent.Peer | |||
13 | 13 | ||
14 | import Network.BitTorrent.Peer.Addr as P | 14 | import Network.BitTorrent.Peer.Addr as P |
15 | import Network.BitTorrent.Peer.ClientInfo as P | 15 | import Network.BitTorrent.Peer.ClientInfo as P |
16 | import Network.BitTorrent.Peer.ID as P \ No newline at end of file | 16 | import Network.BitTorrent.Peer.ID as P |
17 | import Network.BitTorrent.Peer.Status as P \ No newline at end of file | ||
diff --git a/src/Network/BitTorrent/Peer/Status.hs b/src/Network/BitTorrent/Peer/Status.hs new file mode 100644 index 00000000..806ba77d --- /dev/null +++ b/src/Network/BitTorrent/Peer/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 | -- | ||
8 | module 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 | |||
22 | data PeerStatus = PeerStatus { | ||
23 | psChoking :: Bool | ||
24 | , psInterested :: Bool | ||
25 | } | ||
26 | |||
27 | -- | Any session between peers starts as choking and not interested. | ||
28 | initPeerStatus :: PeerStatus | ||
29 | initPeerStatus = PeerStatus True False | ||
30 | |||
31 | setChoking :: Bool -> PeerStatus -> PeerStatus | ||
32 | setChoking b ps = ps { psChoking = b } | ||
33 | |||
34 | setInterested :: Bool -> PeerStatus -> PeerStatus | ||
35 | setInterested b ps = ps { psInterested = b } | ||
36 | |||
37 | |||
38 | |||
39 | data SessionStatus = SessionStatus { | ||
40 | seClientStatus :: PeerStatus | ||
41 | , sePeerStatus :: PeerStatus | ||
42 | } | ||
43 | |||
44 | initSessionStatus :: SessionStatus | ||
45 | initSessionStatus = SessionStatus initPeerStatus initPeerStatus | ||
46 | |||
47 | setClientStatus :: (PeerStatus -> PeerStatus) -> SessionStatus -> SessionStatus | ||
48 | setClientStatus f ss = ss { seClientStatus = f (seClientStatus ss) } | ||
49 | |||
50 | setPeerStatus :: (PeerStatus -> PeerStatus) -> SessionStatus -> SessionStatus | ||
51 | setPeerStatus f ss = ss { sePeerStatus = f (sePeerStatus ss) } | ||
52 | |||
53 | -- | Can the /client/ to upload to the /peer/? | ||
54 | canUpload :: SessionStatus -> Bool | ||
55 | canUpload SessionStatus { seClientStatus = client, sePeerStatus = peer} = | ||
56 | psInterested peer && not (psChoking client) | ||
57 | |||
58 | -- | Can the /client/ download from the /peer/? | ||
59 | canDownload :: SessionStatus -> Bool | ||
60 | canDownload SessionStatus { seClientStatus = client, sePeerStatus = peer } = | ||
61 | psInterested client && not (psChoking peer) | ||
62 | |||
63 | -- | Indicates have many peers are allowed to download from the client. | ||
64 | defaultUnchokeSlots :: Int | ||
65 | defaultUnchokeSlots = 4 \ No newline at end of file | ||