diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-11 04:57:56 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-11 04:57:56 +0400 |
commit | 64de83063c21a963b9c7cdec82cfe2b036166c15 (patch) | |
tree | 22906f9af68e7229490537e32fb71bfdb6f23924 /src/Network/BitTorrent/Internal.hs | |
parent | cdb75165ee0e4f2c36f5766fba4c7bc4bd31db2b (diff) |
+ Add progress to client session.
Diffstat (limited to 'src/Network/BitTorrent/Internal.hs')
-rw-r--r-- | src/Network/BitTorrent/Internal.hs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/Network/BitTorrent/Internal.hs b/src/Network/BitTorrent/Internal.hs index d34c6236..e231fb2c 100644 --- a/src/Network/BitTorrent/Internal.hs +++ b/src/Network/BitTorrent/Internal.hs | |||
@@ -12,7 +12,8 @@ | |||
12 | -- | 12 | -- |
13 | {-# LANGUAGE RecordWildCards #-} | 13 | {-# LANGUAGE RecordWildCards #-} |
14 | module Network.BitTorrent.Internal | 14 | module Network.BitTorrent.Internal |
15 | ( ClientSession(..), newClient | 15 | ( Progress(..), startProgress |
16 | , ClientSession(..), newClient | ||
16 | , SwarmSession(..), newLeacher, newSeeder | 17 | , SwarmSession(..), newLeacher, newSeeder |
17 | , PeerSession(..), withPeerSession | 18 | , PeerSession(..), withPeerSession |
18 | 19 | ||
@@ -49,6 +50,21 @@ import Network.BitTorrent.Exchange.Protocol as BT | |||
49 | 50 | ||
50 | 51 | ||
51 | 52 | ||
53 | -- | 'Progress' contains upload/download/left stats about | ||
54 | -- current client state. | ||
55 | -- | ||
56 | -- This data is considered as dynamic within one session. | ||
57 | -- | ||
58 | data Progress = Progress { | ||
59 | prUploaded :: Integer -- ^ Total amount of bytes uploaded. | ||
60 | , prDownloaded :: Integer -- ^ Total amount of bytes downloaded. | ||
61 | , prLeft :: Integer -- ^ Total amount of bytes left. | ||
62 | } deriving Show | ||
63 | |||
64 | startProgress :: Integer -> Progress | ||
65 | startProgress = Progress 0 0 | ||
66 | |||
67 | |||
52 | {----------------------------------------------------------------------- | 68 | {----------------------------------------------------------------------- |
53 | Client session | 69 | Client session |
54 | -----------------------------------------------------------------------} | 70 | -----------------------------------------------------------------------} |
@@ -59,6 +75,7 @@ data ClientSession = ClientSession { | |||
59 | , allowedExtensions :: [Extension] -- ^ | 75 | , allowedExtensions :: [Extension] -- ^ |
60 | , swarmSessions :: TVar (Set SwarmSession) | 76 | , swarmSessions :: TVar (Set SwarmSession) |
61 | , eventManager :: EventManager | 77 | , eventManager :: EventManager |
78 | , currentProgress :: IORef Progress | ||
62 | } | 79 | } |
63 | 80 | ||
64 | instance Eq ClientSession where | 81 | instance Eq ClientSession where |
@@ -72,6 +89,7 @@ newClient exts = ClientSession <$> newPeerID | |||
72 | <*> pure exts | 89 | <*> pure exts |
73 | <*> newTVarIO S.empty | 90 | <*> newTVarIO S.empty |
74 | <*> Ev.new | 91 | <*> Ev.new |
92 | <*> newIORef (startProgress 0) | ||
75 | 93 | ||
76 | {----------------------------------------------------------------------- | 94 | {----------------------------------------------------------------------- |
77 | Swarm session | 95 | Swarm session |
@@ -80,21 +98,21 @@ newClient exts = ClientSession <$> newPeerID | |||
80 | -- | Extensions are set globally by | 98 | -- | Extensions are set globally by |
81 | -- Swarm session are un | 99 | -- Swarm session are un |
82 | data SwarmSession = SwarmSession { | 100 | data SwarmSession = SwarmSession { |
83 | torrentInfoHash :: InfoHash | 101 | torrentMeta :: Torrent |
84 | , clientSession :: ClientSession | 102 | , clientSession :: ClientSession |
85 | , clientBitfield :: IORef Bitfield | 103 | , clientBitfield :: IORef Bitfield |
86 | , connectedPeers :: TVar (Set PeerSession) | 104 | , connectedPeers :: TVar (Set PeerSession) |
87 | } | 105 | } |
88 | 106 | ||
89 | instance Eq SwarmSession where | 107 | instance Eq SwarmSession where |
90 | (==) = (==) `on` torrentInfoHash | 108 | (==) = (==) `on` (tInfoHash . torrentMeta) |
91 | 109 | ||
92 | instance Ord SwarmSession where | 110 | instance Ord SwarmSession where |
93 | compare = comparing torrentInfoHash | 111 | compare = comparing (tInfoHash . torrentMeta) |
94 | 112 | ||
95 | newSwarmSession :: Bitfield -> ClientSession -> Torrent -> IO SwarmSession | 113 | newSwarmSession :: Bitfield -> ClientSession -> Torrent -> IO SwarmSession |
96 | newSwarmSession bf cs @ ClientSession {..} Torrent {..} | 114 | newSwarmSession bf cs @ ClientSession {..} t @ Torrent {..} |
97 | = SwarmSession <$> pure tInfoHash | 115 | = SwarmSession <$> pure t |
98 | <*> pure cs | 116 | <*> pure cs |
99 | <*> newIORef bf | 117 | <*> newIORef bf |
100 | <*> newTVarIO S.empty | 118 | <*> newTVarIO S.empty |
@@ -160,7 +178,7 @@ withPeerSession ss @ SwarmSession {..} addr | |||
160 | openSession = do | 178 | openSession = do |
161 | let caps = encodeExts $ allowedExtensions $ clientSession | 179 | let caps = encodeExts $ allowedExtensions $ clientSession |
162 | let pid = clientPeerID $ clientSession | 180 | let pid = clientPeerID $ clientSession |
163 | let chs = Handshake defaultBTProtocol caps torrentInfoHash pid | 181 | let chs = Handshake defaultBTProtocol caps (tInfoHash torrentMeta) pid |
164 | 182 | ||
165 | sock <- connectToPeer addr | 183 | sock <- connectToPeer addr |
166 | phs <- handshake sock chs `onException` close sock | 184 | phs <- handshake sock chs `onException` close sock |