diff options
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r-- | src/Network/BitTorrent/Client.hs | 13 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client/Types.hs | 9 |
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 | |||
50 | import Control.Applicative | 54 | import Control.Applicative |
51 | import Control.Exception | 55 | import Control.Exception |
52 | import Control.Concurrent | 56 | import Control.Concurrent |
57 | import Control.Concurrent.Chan.Split as CS | ||
53 | import Control.Monad.Logger | 58 | import Control.Monad.Logger |
54 | import Control.Monad.Trans | 59 | import Control.Monad.Trans |
55 | import Control.Monad.Trans.Resource | 60 | import 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 | ||
134 | newClient :: Options -> LogFun -> IO Client | 142 | newClient :: 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 | ||
169 | subscription :: BitTorrent (ReceivePort ClientEvent) | ||
170 | subscription = 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 | ||
17 | import Control.Applicative | 20 | import Control.Applicative |
18 | import Control.Concurrent | 21 | import Control.Concurrent |
22 | import Control.Concurrent.Chan.Split | ||
19 | import Control.Monad.Logger | 23 | import Control.Monad.Logger |
20 | import Control.Monad.Reader | 24 | import Control.Monad.Reader |
21 | import Control.Monad.Trans.Resource | 25 | import 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 | ||
53 | instance Eq Client where | 58 | instance Eq Client where |
@@ -66,6 +71,10 @@ externalAddr Client {..} = PeerAddr | |||
66 | , peerPort = clientListenerPort | 71 | , peerPort = clientListenerPort |
67 | } | 72 | } |
68 | 73 | ||
74 | data ClientEvent | ||
75 | = TorrentAdded InfoHash | ||
76 | deriving (Show, Eq) | ||
77 | |||
69 | {----------------------------------------------------------------------- | 78 | {----------------------------------------------------------------------- |
70 | -- BitTorrent monad | 79 | -- BitTorrent monad |
71 | -----------------------------------------------------------------------} | 80 | -----------------------------------------------------------------------} |