From 67c47caaccf28d012e7bb34def41f83500d3c72a Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Fri, 4 Apr 2014 01:43:15 +0400 Subject: [Client] Emit TorrentAdded event --- src/Network/BitTorrent/Client/Handle.hs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/Network/BitTorrent/Client/Handle.hs') diff --git a/src/Network/BitTorrent/Client/Handle.hs b/src/Network/BitTorrent/Client/Handle.hs index a1bc163c..19ad1675 100644 --- a/src/Network/BitTorrent/Client/Handle.hs +++ b/src/Network/BitTorrent/Client/Handle.hs @@ -18,6 +18,7 @@ module Network.BitTorrent.Client.Handle , getStatus ) where +import Control.Concurrent.Chan.Split import Control.Concurrent.Lifted as L import Control.Monad import Control.Monad.Trans @@ -40,16 +41,23 @@ import Network.BitTorrent.Tracker as Tracker allocHandle :: InfoHash -> BitTorrent Handle -> BitTorrent Handle allocHandle ih m = do Client {..} <- getClient - modifyMVar clientTorrents $ \ handles -> do + + (h, added) <- modifyMVar clientTorrents $ \ handles -> do case HM.lookup ih handles of - Just h -> return (handles, h) + Just h -> return (handles, (h, False)) Nothing -> do h <- m - return (HM.insert ih h handles, h) + return (HM.insert ih h handles, (h, True)) + + when added $ do + liftIO $ send clientEvents (TorrentAdded ih) + + return h freeHandle :: InfoHash -> BitTorrent () -> BitTorrent () freeHandle ih finalizer = do Client {..} <- getClient + modifyMVar_ clientTorrents $ \ handles -> do case HM.lookup ih handles of Nothing -> return handles -- cgit v1.2.3