summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker/Session.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-03-24 04:56:25 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-03-24 04:56:25 +0400
commitc09681431dfff9522eec70dc20042183e6dde119 (patch)
treeafdb021a38905750e6a454e2212cc309f2e99613 /src/Network/BitTorrent/Tracker/Session.hs
parentb153806a0b9945e9ba3b82296ce0c39b627eb6b9 (diff)
Move trackerURI field to TrackerList
Diffstat (limited to 'src/Network/BitTorrent/Tracker/Session.hs')
-rw-r--r--src/Network/BitTorrent/Tracker/Session.hs58
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 #-}
11module Network.BitTorrent.Tracker.Session 13module 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.
102data TrackerEntry = TrackerEntry 103data 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
116instance ToJSON TrackerEntry where 114instance 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.
124nullEntry :: URI -> TrackerEntry 122instance Default TrackerSession where
125nullEntry 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?
128needNotify :: Event -> Maybe Status -> Maybe Bool 126needNotify :: 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.
164notifyTo :: Manager -> Session -> Event -> TrackerEntry -> IO TrackerEntry 162notifyTo :: Manager -> Session -> Event
165notifyTo mgr s @ Session {..} event entry @ TrackerEntry {..} = do 163 -> TierEntry TrackerSession -> IO TrackerSession
164notifyTo 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'.
205newSession :: InfoHash -> TrackerList URI -> IO Session 202newSession :: InfoHash -> TrackerList () -> IO Session
206newSession ih origUris = do 203newSession 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'.
245withSession :: Manager -> InfoHash -> TrackerList URI 242withSession :: Manager -> InfoHash -> TrackerList ()
246 -> (Session -> IO ()) -> IO () 243 -> (Session -> IO ()) -> IO ()
247withSession m ih uris = bracket (newSession ih uris) (closeSession m) 244withSession 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)
251getStatus :: Session -> IO Status 248getStatus :: Session -> IO Status
252getStatus Session {..} = readIORef sessionStatus 249getStatus Session {..} = readIORef sessionStatus
253 250
254getSessionState :: Session -> IO (TrackerList TrackerEntry) 251getSessionState :: Session -> IO (TrackerList TrackerSession)
255getSessionState Session {..} = readMVar sessionTrackers 252getSessionState 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
312removeTracker :: Session -> URI -> IO () 309removeTracker :: Manager -> Session -> URI -> IO ()
313removeTracker Session {..} uri = do 310removeTracker 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