summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Client.hs13
-rw-r--r--src/Network/BitTorrent/Client/Types.hs9
2 files changed, 22 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Client.hs b/src/Network/BitTorrent/Client.hs
index b9099736..03b7301d 100644
--- a/src/Network/BitTorrent/Client.hs
+++ b/src/Network/BitTorrent/Client.hs
@@ -21,6 +21,10 @@ module Network.BitTorrent.Client
21 , withClient 21 , withClient
22 , simpleClient 22 , simpleClient
23 23
24 -- ** Events
25 , ClientEvent (..)
26 , subscription
27
24 -- * BitTorrent monad 28 -- * BitTorrent monad
25 , MonadBitTorrent (..) 29 , MonadBitTorrent (..)
26 , BitTorrent 30 , BitTorrent
@@ -50,6 +54,7 @@ module Network.BitTorrent.Client
50import Control.Applicative 54import Control.Applicative
51import Control.Exception 55import Control.Exception
52import Control.Concurrent 56import Control.Concurrent
57import Control.Concurrent.Chan.Split as CS
53import Control.Monad.Logger 58import Control.Monad.Logger
54import Control.Monad.Trans 59import Control.Monad.Trans
55import Control.Monad.Trans.Resource 60import Control.Monad.Trans.Resource
@@ -119,6 +124,8 @@ initClient opts @ Options {..} logFun = do
119 (_, node) <- allocate mkNode DHT.closeNode 124 (_, node) <- allocate mkNode DHT.closeNode
120 125
121 resourceMap <- getInternalState 126 resourceMap <- getInternalState
127 eventStream <- liftIO newSendPort
128
122 return Client 129 return Client
123 { clientPeerId = pid 130 { clientPeerId = pid
124 , clientListenerPort = optPort 131 , clientListenerPort = optPort
@@ -129,6 +136,7 @@ initClient opts @ Options {..} logFun = do
129 , clientNode = node 136 , clientNode = node
130 , clientTorrents = tmap 137 , clientTorrents = tmap
131 , clientLogger = logFun 138 , clientLogger = logFun
139 , clientEvents = eventStream
132 } 140 }
133 141
134newClient :: Options -> LogFun -> IO Client 142newClient :: Options -> LogFun -> IO Client
@@ -158,6 +166,11 @@ simpleClient m = do
158 runStderrLoggingT $ LoggingT $ \ logger -> do 166 runStderrLoggingT $ LoggingT $ \ logger -> do
159 withClient def logger (`runBitTorrent` m) 167 withClient def logger (`runBitTorrent` m)
160 168
169subscription :: BitTorrent (ReceivePort ClientEvent)
170subscription = do
171 Client {..} <- getClient
172 liftIO $ listen clientEvents
173
161{----------------------------------------------------------------------- 174{-----------------------------------------------------------------------
162-- Torrent identifiers 175-- Torrent identifiers
163-----------------------------------------------------------------------} 176-----------------------------------------------------------------------}
diff --git a/src/Network/BitTorrent/Client/Types.hs b/src/Network/BitTorrent/Client/Types.hs
index 3e62f0fa..d6b08efe 100644
--- a/src/Network/BitTorrent/Client/Types.hs
+++ b/src/Network/BitTorrent/Client/Types.hs
@@ -12,10 +12,14 @@ module Network.BitTorrent.Client.Types
12 , getClient 12 , getClient
13 13
14 , MonadBitTorrent (..) 14 , MonadBitTorrent (..)
15
16 -- * Events
17 , ClientEvent (..)
15 ) where 18 ) where
16 19
17import Control.Applicative 20import Control.Applicative
18import Control.Concurrent 21import Control.Concurrent
22import Control.Concurrent.Chan.Split
19import Control.Monad.Logger 23import Control.Monad.Logger
20import Control.Monad.Reader 24import Control.Monad.Reader
21import Control.Monad.Trans.Resource 25import Control.Monad.Trans.Resource
@@ -48,6 +52,7 @@ data Client = Client
48 , clientNode :: !(Node IPv4) 52 , clientNode :: !(Node IPv4)
49 , clientTorrents :: !(MVar (HashMap InfoHash Handle)) 53 , clientTorrents :: !(MVar (HashMap InfoHash Handle))
50 , clientLogger :: !LogFun 54 , clientLogger :: !LogFun
55 , clientEvents :: !(SendPort ClientEvent)
51 } 56 }
52 57
53instance Eq Client where 58instance Eq Client where
@@ -66,6 +71,10 @@ externalAddr Client {..} = PeerAddr
66 , peerPort = clientListenerPort 71 , peerPort = clientListenerPort
67 } 72 }
68 73
74data ClientEvent
75 = TorrentAdded InfoHash
76 deriving (Show, Eq)
77
69{----------------------------------------------------------------------- 78{-----------------------------------------------------------------------
70-- BitTorrent monad 79-- BitTorrent monad
71-----------------------------------------------------------------------} 80-----------------------------------------------------------------------}