summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-27 13:59:33 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-27 13:59:33 +0400
commita5b5c13610d2097d6541e9d0d5a118735607dfab (patch)
tree8b6bda1bf7a402a47bf02753e0a165ab4a8dc905 /src/Network/BitTorrent
parent27cf6fbeeb19572a58a71ca7cf080aeea82d0cb8 (diff)
Move exchange status to separate module
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Exchange/Protocol.hs65
-rw-r--r--src/Network/BitTorrent/Exchange/Status.hs67
2 files changed, 68 insertions, 64 deletions
diff --git a/src/Network/BitTorrent/Exchange/Protocol.hs b/src/Network/BitTorrent/Exchange/Protocol.hs
index 7af99335..037ef31f 100644
--- a/src/Network/BitTorrent/Exchange/Protocol.hs
+++ b/src/Network/BitTorrent/Exchange/Protocol.hs
@@ -43,25 +43,11 @@ module Network.BitTorrent.Exchange.Protocol
43 43
44 -- * Regular messages 44 -- * Regular messages
45 , Message(..) 45 , Message(..)
46
47 -- * control
48 , PeerStatus(..)
49 , choking, interested
50
51 , SessionStatus(..)
52 , inverseStatus
53 , clientStatus, peerStatus
54 , canUpload, canDownload
55
56 -- ** Defaults
57 , defaultUnchokeSlots
58 ) where 46 ) where
59 47
60import Control.Applicative 48import Control.Applicative
61import Control.Exception 49import Control.Exception
62import Control.Monad 50import Control.Monad
63import Control.Lens
64import Data.Aeson.TH
65import Data.Binary as B 51import Data.Binary as B
66import Data.Binary.Get as B 52import Data.Binary.Get as B
67import Data.Binary.Put as B 53import Data.Binary.Put as B
@@ -69,9 +55,7 @@ import Data.ByteString as BS
69import Data.ByteString.Char8 as BC 55import Data.ByteString.Char8 as BC
70import Data.ByteString.Lazy as BL 56import Data.ByteString.Lazy as BL
71import Data.Default 57import Data.Default
72import Data.List as L
73import Data.Serialize as S 58import Data.Serialize as S
74import Data.Word
75import Network 59import Network
76import Network.Socket.ByteString 60import Network.Socket.ByteString
77import Text.PrettyPrint 61import Text.PrettyPrint
@@ -80,10 +64,9 @@ import Text.PrettyPrint.Class
80import Data.Torrent.Bitfield 64import Data.Torrent.Bitfield
81import Data.Torrent.Block 65import Data.Torrent.Block
82import Data.Torrent.InfoHash 66import Data.Torrent.InfoHash
83import Data.Torrent
84import Network.BitTorrent.Extension 67import Network.BitTorrent.Extension
85import Network.BitTorrent.Core.PeerId 68import Network.BitTorrent.Core.PeerId
86import Network.BitTorrent.Core.PeerAddr 69import Network.BitTorrent.Core.PeerAddr ()
87 70
88 71
89getInt :: S.Get Int 72getInt :: S.Get Int
@@ -398,49 +381,3 @@ instance Binary Message where
398 put (SuggestPiece pix) = putIntB 5 >> B.putWord8 0x0D >> putIntB pix 381 put (SuggestPiece pix) = putIntB 5 >> B.putWord8 0x0D >> putIntB pix
399 put (RejectRequest i ) = putIntB 13 >> B.putWord8 0x10 >> B.put i 382 put (RejectRequest i ) = putIntB 13 >> B.putWord8 0x10 >> B.put i
400 put (AllowedFast i ) = putIntB 5 >> B.putWord8 0x11 >> putIntB i 383 put (AllowedFast i ) = putIntB 5 >> B.putWord8 0x11 >> putIntB i
401
402{-----------------------------------------------------------------------
403 Peer Status
404-----------------------------------------------------------------------}
405
406-- |
407data PeerStatus = PeerStatus {
408 _choking :: !Bool
409 , _interested :: !Bool
410 } deriving (Show, Eq)
411
412$(makeLenses ''PeerStatus)
413$(deriveJSON (L.dropWhile (== '_')) ''PeerStatus)
414
415instance Default PeerStatus where
416 def = PeerStatus True False
417
418-- |
419data SessionStatus = SessionStatus {
420 _clientStatus :: !PeerStatus
421 , _peerStatus :: !PeerStatus
422 } deriving (Show, Eq)
423
424$(makeLenses ''SessionStatus)
425$(deriveJSON (L.dropWhile (== '_')) ''SessionStatus)
426
427instance Default SessionStatus where
428 def = SessionStatus def def
429
430-- | Can the /client/ transfer to the /peer/?
431canUpload :: SessionStatus -> Bool
432canUpload SessionStatus {..}
433 = _interested _peerStatus && not (_choking _clientStatus)
434
435-- | Can the /client/ transfer from the /peer/?
436canDownload :: SessionStatus -> Bool
437canDownload SessionStatus {..}
438 = _interested _clientStatus && not (_choking _peerStatus)
439
440inverseStatus :: SessionStatus -> SessionStatus
441inverseStatus SessionStatus {..} = SessionStatus _peerStatus _clientStatus
442
443-- | Indicates how many peers are allowed to download from the client
444-- by default.
445defaultUnchokeSlots :: Int
446defaultUnchokeSlots = 4 \ No newline at end of file
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