summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-03-23 06:06:04 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-03-23 06:06:04 +0400
commit7f22b101097707db181f71a5025fe7f39b6e2b3b (patch)
tree21a65c0950c15c21c0c289d0b376f1732ffaa5a7 /src/Network/BitTorrent
parent09226ee22e5ba9ac87d193c1e6a1c733ac1ae620 (diff)
Add tracker eventstream
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Tracker/Session.hs34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/Network/BitTorrent/Tracker/Session.hs b/src/Network/BitTorrent/Tracker/Session.hs
index 4c61034e..a17973ad 100644
--- a/src/Network/BitTorrent/Tracker/Session.hs
+++ b/src/Network/BitTorrent/Tracker/Session.hs
@@ -38,11 +38,16 @@ module Network.BitTorrent.Tracker.Session
38 , addTracker 38 , addTracker
39 , removeTracker 39 , removeTracker
40 , getTrustedTrackers 40 , getTrustedTrackers
41
42 -- * Events
43 , SessionEvent (..)
44 , subscribe
41 ) where 45 ) where
42 46
43import Control.Applicative 47import Control.Applicative
44import Control.Exception 48import Control.Exception
45import Control.Concurrent 49import Control.Concurrent
50import Control.Concurrent.Chan.Split
46import Control.Monad 51import Control.Monad
47import Data.Aeson 52import Data.Aeson
48import Data.Aeson.TH 53import Data.Aeson.TH
@@ -189,6 +194,8 @@ data Session = Session
189 -- | A set of single-tracker sessions. Any request to a tracker 194 -- | A set of single-tracker sessions. Any request to a tracker
190 -- must take a lock. 195 -- must take a lock.
191 , sessionTrackers :: !(MVar (TrackerList TrackerEntry)) 196 , sessionTrackers :: !(MVar (TrackerList TrackerEntry))
197
198 , sessionEvents :: !(SendPort SessionEvent)
192 } 199 }
193 200
194-- | Create a new multitracker session in paused state. Tracker list 201-- | Create a new multitracker session in paused state. Tracker list
@@ -196,19 +203,40 @@ data Session = Session
196-- client presence use 'notify'. 203-- client presence use 'notify'.
197newSession :: InfoHash -> TrackerList URI -> IO Session 204newSession :: InfoHash -> TrackerList URI -> IO Session
198newSession ih origUris = do 205newSession ih origUris = do
199 urisList <- shuffleTiers origUris 206 urisList <- shuffleTiers origUris
200 statusRef <- newIORef def 207 statusRef <- newIORef def
201 entriesVar <- newMVar (fmap nullEntry urisList) 208 entriesVar <- newMVar (fmap nullEntry urisList)
209 eventStream <- newSendPort
202 return Session 210 return Session
203 { sessionTopic = ih 211 { sessionTopic = ih
204 , sessionStatus = statusRef 212 , sessionStatus = statusRef
205 , sessionTrackers = entriesVar 213 , sessionTrackers = entriesVar
214 , sessionEvents = eventStream
206 } 215 }
207 216
208-- | Release scarce resources associated with the given session. 217-- | Release scarce resources associated with the given session.
209closeSession :: Session -> IO () 218closeSession :: Session -> IO ()
210closeSession _ = return () 219closeSession _ = return ()
211 220
221{-----------------------------------------------------------------------
222-- Events
223-----------------------------------------------------------------------}
224
225data SessionEvent
226 = TrackerAdded URI
227 | TrackerConfirmed URI
228 | TrackerRemoved URI
229 | AnnouncedTo URI
230 | Reannounced URI
231 | SessionClosed
232
233subscribe :: Session -> IO (ReceivePort SessionEvent)
234subscribe Session {..} = listen sessionEvents
235
236{-----------------------------------------------------------------------
237-- Operations
238-----------------------------------------------------------------------}
239
212-- | Normally you need to use 'Control.Monad.Trans.Resource.alloc'. 240-- | Normally you need to use 'Control.Monad.Trans.Resource.alloc'.
213withSession :: InfoHash -> TrackerList URI -> (Session -> IO ()) -> IO () 241withSession :: InfoHash -> TrackerList URI -> (Session -> IO ()) -> IO ()
214withSession ih uris = bracket (newSession ih uris) closeSession 242withSession ih uris = bracket (newSession ih uris) closeSession