diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-04-04 01:22:27 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-04-04 01:22:27 +0400 |
commit | 2a64b6b3e5766242d012baefde7bca54adc675f1 (patch) | |
tree | d92df65ddb60b6f9e923ed4fba69083aec021d21 /src | |
parent | c3e85d6baeba53168ed9dd2157f9b8f70bb1532d (diff) |
[Client] Use lifted concurrent operations
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/BitTorrent/Client/Handle.hs | 14 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client/Types.hs | 15 |
2 files changed, 21 insertions, 8 deletions
diff --git a/src/Network/BitTorrent/Client/Handle.hs b/src/Network/BitTorrent/Client/Handle.hs index 6e2dd0dc..2da3b357 100644 --- a/src/Network/BitTorrent/Client/Handle.hs +++ b/src/Network/BitTorrent/Client/Handle.hs | |||
@@ -19,7 +19,7 @@ module Network.BitTorrent.Client.Handle | |||
19 | ) where | 19 | ) where |
20 | 20 | ||
21 | import Control.Applicative | 21 | import Control.Applicative |
22 | import Control.Concurrent | 22 | import Control.Concurrent.Lifted as L |
23 | import Control.Monad | 23 | import Control.Monad |
24 | import Control.Monad.Trans | 24 | import Control.Monad.Trans |
25 | import Data.Default | 25 | import Data.Default |
@@ -40,28 +40,28 @@ import Network.BitTorrent.Tracker as Tracker | |||
40 | 40 | ||
41 | allocHandle :: InfoHash -> BitTorrent Handle -> BitTorrent Handle | 41 | allocHandle :: InfoHash -> BitTorrent Handle -> BitTorrent Handle |
42 | allocHandle ih m = do | 42 | allocHandle ih m = do |
43 | c @ Client {..} <- getClient | 43 | Client {..} <- getClient |
44 | liftIO $ modifyMVar clientTorrents $ \ handles -> do | 44 | modifyMVar clientTorrents $ \ handles -> do |
45 | case HM.lookup ih handles of | 45 | case HM.lookup ih handles of |
46 | Just h -> return (handles, h) | 46 | Just h -> return (handles, h) |
47 | Nothing -> do | 47 | Nothing -> do |
48 | h <- runBitTorrent c m | 48 | h <- m |
49 | return (HM.insert ih h handles, h) | 49 | return (HM.insert ih h handles, h) |
50 | 50 | ||
51 | freeHandle :: InfoHash -> BitTorrent () -> BitTorrent () | 51 | freeHandle :: InfoHash -> BitTorrent () -> BitTorrent () |
52 | freeHandle ih finalizer = do | 52 | freeHandle ih finalizer = do |
53 | c @ Client {..} <- getClient | 53 | c @ Client {..} <- getClient |
54 | liftIO $ modifyMVar_ clientTorrents $ \ handles -> do | 54 | modifyMVar_ clientTorrents $ \ handles -> do |
55 | case HM.lookup ih handles of | 55 | case HM.lookup ih handles of |
56 | Nothing -> return handles | 56 | Nothing -> return handles |
57 | Just _ -> do | 57 | Just _ -> do |
58 | runBitTorrent c finalizer | 58 | finalizer |
59 | return (HM.delete ih handles) | 59 | return (HM.delete ih handles) |
60 | 60 | ||
61 | lookupHandle :: InfoHash -> BitTorrent (Maybe Handle) | 61 | lookupHandle :: InfoHash -> BitTorrent (Maybe Handle) |
62 | lookupHandle ih = do | 62 | lookupHandle ih = do |
63 | Client {..} <- getClient | 63 | Client {..} <- getClient |
64 | handles <- liftIO $ readMVar clientTorrents | 64 | handles <- readMVar clientTorrents |
65 | return (HM.lookup ih handles) | 65 | return (HM.lookup ih handles) |
66 | 66 | ||
67 | {----------------------------------------------------------------------- | 67 | {----------------------------------------------------------------------- |
diff --git a/src/Network/BitTorrent/Client/Types.hs b/src/Network/BitTorrent/Client/Types.hs index d6b08efe..c0d50175 100644 --- a/src/Network/BitTorrent/Client/Types.hs +++ b/src/Network/BitTorrent/Client/Types.hs | |||
@@ -1,4 +1,6 @@ | |||
1 | {-# LANGUAGE FlexibleInstances #-} | 1 | {-# LANGUAGE FlexibleInstances #-} |
2 | {-# LANGUAGE TypeFamilies #-} | ||
3 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
2 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 4 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
3 | module Network.BitTorrent.Client.Types | 5 | module Network.BitTorrent.Client.Types |
4 | ( -- * Core types | 6 | ( -- * Core types |
@@ -20,8 +22,10 @@ module Network.BitTorrent.Client.Types | |||
20 | import Control.Applicative | 22 | import Control.Applicative |
21 | import Control.Concurrent | 23 | import Control.Concurrent |
22 | import Control.Concurrent.Chan.Split | 24 | import Control.Concurrent.Chan.Split |
25 | import Control.Monad.Base | ||
23 | import Control.Monad.Logger | 26 | import Control.Monad.Logger |
24 | import Control.Monad.Reader | 27 | import Control.Monad.Reader |
28 | import Control.Monad.Trans.Control | ||
25 | import Control.Monad.Trans.Resource | 29 | import Control.Monad.Trans.Resource |
26 | import Data.Function | 30 | import Data.Function |
27 | import Data.HashMap.Strict as HM | 31 | import Data.HashMap.Strict as HM |
@@ -82,12 +86,21 @@ data ClientEvent | |||
82 | newtype BitTorrent a = BitTorrent | 86 | newtype BitTorrent a = BitTorrent |
83 | { unBitTorrent :: ReaderT Client IO a | 87 | { unBitTorrent :: ReaderT Client IO a |
84 | } deriving ( Functor, Applicative, Monad | 88 | } deriving ( Functor, Applicative, Monad |
85 | , MonadIO, MonadThrow, MonadUnsafeIO | 89 | , MonadIO, MonadThrow, MonadUnsafeIO, MonadBase IO |
86 | ) | 90 | ) |
87 | 91 | ||
88 | class MonadBitTorrent m where | 92 | class MonadBitTorrent m where |
89 | liftBT :: BitTorrent a -> m a | 93 | liftBT :: BitTorrent a -> m a |
90 | 94 | ||
95 | instance MonadBaseControl IO BitTorrent where | ||
96 | newtype StM BitTorrent a = StM { unSt :: StM (ReaderT Client IO) a } | ||
97 | liftBaseWith cc = BitTorrent $ liftBaseWith $ \ cc' -> | ||
98 | cc $ \ (BitTorrent m) -> StM <$> cc' m | ||
99 | {-# INLINE liftBaseWith #-} | ||
100 | |||
101 | restoreM = BitTorrent . restoreM . unSt | ||
102 | {-# INLINE restoreM #-} | ||
103 | |||
91 | -- | NOP. | 104 | -- | NOP. |
92 | instance MonadBitTorrent BitTorrent where | 105 | instance MonadBitTorrent BitTorrent where |
93 | liftBT = id | 106 | liftBT = id |