diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-24 04:56:25 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-03-24 04:56:25 +0400 |
commit | c09681431dfff9522eec70dc20042183e6dde119 (patch) | |
tree | afdb021a38905750e6a454e2212cc309f2e99613 /src/Network/BitTorrent/Tracker/Session.hs | |
parent | b153806a0b9945e9ba3b82296ce0c39b627eb6b9 (diff) |
Move trackerURI field to TrackerList
Diffstat (limited to 'src/Network/BitTorrent/Tracker/Session.hs')
-rw-r--r-- | src/Network/BitTorrent/Tracker/Session.hs | 58 |
1 files changed, 27 insertions, 31 deletions
diff --git a/src/Network/BitTorrent/Tracker/Session.hs b/src/Network/BitTorrent/Tracker/Session.hs index f66e8bde..74d854c5 100644 --- a/src/Network/BitTorrent/Tracker/Session.hs +++ b/src/Network/BitTorrent/Tracker/Session.hs | |||
@@ -7,7 +7,9 @@ | |||
7 | -- | 7 | -- |
8 | -- Multitracker sessions. | 8 | -- Multitracker sessions. |
9 | -- | 9 | -- |
10 | {-# LANGUAGE TemplateHaskell #-} | 10 | {-# LANGUAGE FlexibleInstances #-} |
11 | {-# LANGUAGE TypeSynonymInstances #-} | ||
12 | {-# LANGUAGE TemplateHaskell #-} | ||
11 | module Network.BitTorrent.Tracker.Session | 13 | module Network.BitTorrent.Tracker.Session |
12 | ( -- * Session | 14 | ( -- * Session |
13 | Session | 15 | Session |
@@ -27,8 +29,7 @@ module Network.BitTorrent.Tracker.Session | |||
27 | 29 | ||
28 | -- ** Single tracker sessions | 30 | -- ** Single tracker sessions |
29 | , LastScrape (..) | 31 | , LastScrape (..) |
30 | , TrackerEntry | 32 | , TrackerSession |
31 | , trackerURI | ||
32 | , trackerPeers | 33 | , trackerPeers |
33 | , trackerScrape | 34 | , trackerScrape |
34 | , getSessionState | 35 | , getSessionState |
@@ -99,12 +100,9 @@ data LastScrape = LastScrape | |||
99 | $(deriveJSON omitRecordPrefix ''LastScrape) | 100 | $(deriveJSON omitRecordPrefix ''LastScrape) |
100 | 101 | ||
101 | -- | Single tracker session. | 102 | -- | Single tracker session. |
102 | data TrackerEntry = TrackerEntry | 103 | data TrackerSession = TrackerSession |
103 | { -- | Tracker announce URI. | 104 | { -- | Used to notify 'Stopped' and 'Completed' events. |
104 | trackerURI :: !URI | 105 | statusSent :: !(Maybe Status) |
105 | |||
106 | -- | Used to notify 'Stopped' and 'Completed' events. | ||
107 | , statusSent :: !(Maybe Status) | ||
108 | 106 | ||
109 | -- | Can be used to retrieve peer set. | 107 | -- | Can be used to retrieve peer set. |
110 | , trackerPeers :: Cached [PeerAddr IP] | 108 | , trackerPeers :: Cached [PeerAddr IP] |
@@ -113,16 +111,16 @@ data TrackerEntry = TrackerEntry | |||
113 | , trackerScrape :: Cached LastScrape | 111 | , trackerScrape :: Cached LastScrape |
114 | } | 112 | } |
115 | 113 | ||
116 | instance ToJSON TrackerEntry where | 114 | instance ToJSON (TierEntry TrackerSession) where |
117 | toJSON TrackerEntry {..} = object | 115 | toJSON (uri, TrackerSession {..}) = object |
118 | [ "uri" .= trackerURI | 116 | [ "uri" .= uri |
119 | , "peers" .= trackerPeers | 117 | , "peers" .= trackerPeers |
120 | , "scrape" .= trackerScrape | 118 | , "scrape" .= trackerScrape |
121 | ] | 119 | ] |
122 | 120 | ||
123 | -- | Single tracker session with empty state.l | 121 | -- | Not contacted. |
124 | nullEntry :: URI -> TrackerEntry | 122 | instance Default TrackerSession where |
125 | nullEntry uri = TrackerEntry uri Nothing def def | 123 | def = TrackerSession Nothing def def |
126 | 124 | ||
127 | -- | Do we need to notify this /specific/ tracker? | 125 | -- | Do we need to notify this /specific/ tracker? |
128 | needNotify :: Event -> Maybe Status -> Maybe Bool | 126 | needNotify :: Event -> Maybe Status -> Maybe Bool |
@@ -161,22 +159,21 @@ cacheScrape AnnounceInfo {..} = | |||
161 | } | 159 | } |
162 | 160 | ||
163 | -- | Make announce request to specific tracker returning new state. | 161 | -- | Make announce request to specific tracker returning new state. |
164 | notifyTo :: Manager -> Session -> Event -> TrackerEntry -> IO TrackerEntry | 162 | notifyTo :: Manager -> Session -> Event |
165 | notifyTo mgr s @ Session {..} event entry @ TrackerEntry {..} = do | 163 | -> TierEntry TrackerSession -> IO TrackerSession |
164 | notifyTo mgr s @ Session {..} event (uri, entry @ TrackerSession {..}) = do | ||
166 | let shouldNotify = needNotify event statusSent | 165 | let shouldNotify = needNotify event statusSent |
167 | mustNotify <- maybe (isExpired trackerPeers) return shouldNotify | 166 | mustNotify <- maybe (isExpired trackerPeers) return shouldNotify |
168 | if not mustNotify | 167 | if not mustNotify |
169 | then return entry | 168 | then return entry |
170 | else do | 169 | else do |
171 | let q = SAnnounceQuery sessionTopic def Nothing (Just event) | 170 | let q = SAnnounceQuery sessionTopic def Nothing (Just event) |
172 | res <- RPC.announce mgr trackerURI q | 171 | res <- RPC.announce mgr uri q |
173 | when (statusSent == Nothing) $ do | 172 | when (statusSent == Nothing) $ do |
174 | send sessionEvents (TrackerConfirmed trackerURI) | 173 | send sessionEvents (TrackerConfirmed uri) |
175 | send sessionEvents (AnnouncedTo trackerURI) | 174 | send sessionEvents (AnnouncedTo uri) |
176 | let status' = nextStatus event <|> statusSent | 175 | let status' = nextStatus event <|> statusSent |
177 | TrackerEntry trackerURI status' | 176 | TrackerSession status' <$> cachePeers res <*> cacheScrape res |
178 | <$> cachePeers res | ||
179 | <*> cacheScrape res | ||
180 | 177 | ||
181 | {----------------------------------------------------------------------- | 178 | {----------------------------------------------------------------------- |
182 | -- Multitracker Session | 179 | -- Multitracker Session |
@@ -194,7 +191,7 @@ data Session = Session | |||
194 | 191 | ||
195 | -- | A set of single-tracker sessions. Any request to a tracker | 192 | -- | A set of single-tracker sessions. Any request to a tracker |
196 | -- must take a lock. | 193 | -- must take a lock. |
197 | , sessionTrackers :: !(MVar (TrackerList TrackerEntry)) | 194 | , sessionTrackers :: !(MVar (TrackerList TrackerSession)) |
198 | 195 | ||
199 | , sessionEvents :: !(SendPort SessionEvent) | 196 | , sessionEvents :: !(SendPort SessionEvent) |
200 | } | 197 | } |
@@ -202,11 +199,11 @@ data Session = Session | |||
202 | -- | Create a new multitracker session in paused state. Tracker list | 199 | -- | Create a new multitracker session in paused state. Tracker list |
203 | -- must contant only /trusted/ tracker uris. To start announcing | 200 | -- must contant only /trusted/ tracker uris. To start announcing |
204 | -- client presence use 'notify'. | 201 | -- client presence use 'notify'. |
205 | newSession :: InfoHash -> TrackerList URI -> IO Session | 202 | newSession :: InfoHash -> TrackerList () -> IO Session |
206 | newSession ih origUris = do | 203 | newSession ih origUris = do |
207 | urisList <- shuffleTiers origUris | 204 | urisList <- shuffleTiers origUris |
208 | statusRef <- newIORef def | 205 | statusRef <- newIORef def |
209 | entriesVar <- newMVar (fmap nullEntry urisList) | 206 | entriesVar <- newMVar (fmap (const def) urisList) |
210 | eventStream <- newSendPort | 207 | eventStream <- newSendPort |
211 | return Session | 208 | return Session |
212 | { sessionTopic = ih | 209 | { sessionTopic = ih |
@@ -242,7 +239,7 @@ subscribe Session {..} = listen sessionEvents | |||
242 | -----------------------------------------------------------------------} | 239 | -----------------------------------------------------------------------} |
243 | 240 | ||
244 | -- | Normally you need to use 'Control.Monad.Trans.Resource.alloc'. | 241 | -- | Normally you need to use 'Control.Monad.Trans.Resource.alloc'. |
245 | withSession :: Manager -> InfoHash -> TrackerList URI | 242 | withSession :: Manager -> InfoHash -> TrackerList () |
246 | -> (Session -> IO ()) -> IO () | 243 | -> (Session -> IO ()) -> IO () |
247 | withSession m ih uris = bracket (newSession ih uris) (closeSession m) | 244 | withSession m ih uris = bracket (newSession ih uris) (closeSession m) |
248 | 245 | ||
@@ -251,7 +248,7 @@ withSession m ih uris = bracket (newSession ih uris) (closeSession m) | |||
251 | getStatus :: Session -> IO Status | 248 | getStatus :: Session -> IO Status |
252 | getStatus Session {..} = readIORef sessionStatus | 249 | getStatus Session {..} = readIORef sessionStatus |
253 | 250 | ||
254 | getSessionState :: Session -> IO (TrackerList TrackerEntry) | 251 | getSessionState :: Session -> IO (TrackerList TrackerSession) |
255 | getSessionState Session {..} = readMVar sessionTrackers | 252 | getSessionState Session {..} = readMVar sessionTrackers |
256 | 253 | ||
257 | -- | Do we need to sent this event to a first working tracker or to | 254 | -- | Do we need to sent this event to a first working tracker or to |
@@ -309,9 +306,8 @@ addTracker Session {..} uri = do | |||
309 | undefined | 306 | undefined |
310 | send sessionEvents (TrackerAdded uri) | 307 | send sessionEvents (TrackerAdded uri) |
311 | 308 | ||
312 | removeTracker :: Session -> URI -> IO () | 309 | removeTracker :: Manager -> Session -> URI -> IO () |
313 | removeTracker Session {..} uri = do | 310 | removeTracker m Session {..} uri = do |
314 | undefined | ||
315 | send sessionEvents (TrackerRemoved uri) | 311 | send sessionEvents (TrackerRemoved uri) |
316 | 312 | ||
317 | -- Also, as specified under the definitions section, a tracker that | 313 | -- Also, as specified under the definitions section, a tracker that |