diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-15 05:14:46 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-15 05:14:46 +0400 |
commit | ee4c3c7583d79beece46f65f360f5f6ae93c37ba (patch) | |
tree | 731110eba455612fb8c4aa8ea5a79ec22c29625d | |
parent | 3dc4e742c5c0cfca1efde1ef7391a626d1607dc2 (diff) |
Add TorrentSource class
-rw-r--r-- | src/Network/BitTorrent/Client.hs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/Network/BitTorrent/Client.hs b/src/Network/BitTorrent/Client.hs index c6b4901a..5218c299 100644 --- a/src/Network/BitTorrent/Client.hs +++ b/src/Network/BitTorrent/Client.hs | |||
@@ -1,3 +1,5 @@ | |||
1 | {-# LANGUAGE FlexibleInstances #-} | ||
2 | {-# LANGUAGE TypeSynonymInstances #-} | ||
1 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 3 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
2 | {-# LANGUAGE TemplateHaskell #-} | 4 | {-# LANGUAGE TemplateHaskell #-} |
3 | module Network.BitTorrent.Client | 5 | module Network.BitTorrent.Client |
@@ -25,8 +27,7 @@ module Network.BitTorrent.Client | |||
25 | , getClient | 27 | , getClient |
26 | 28 | ||
27 | -- * Handle | 29 | -- * Handle |
28 | , openTorrent | 30 | , TorrentSource (..) |
29 | , openMagnet | ||
30 | , closeHandle | 31 | , closeHandle |
31 | 32 | ||
32 | , start | 33 | , start |
@@ -37,6 +38,7 @@ module Network.BitTorrent.Client | |||
37 | import Control.Exception | 38 | import Control.Exception |
38 | import Control.Concurrent | 39 | import Control.Concurrent |
39 | import Control.Monad.Logger | 40 | import Control.Monad.Logger |
41 | import Control.Monad.Trans | ||
40 | import Control.Monad.Trans.Resource | 42 | import Control.Monad.Trans.Resource |
41 | 43 | ||
42 | import Data.Default | 44 | import Data.Default |
@@ -45,6 +47,9 @@ import Data.Maybe | |||
45 | import Data.Text | 47 | import Data.Text |
46 | import Network | 48 | import Network |
47 | 49 | ||
50 | import Data.Torrent | ||
51 | import Data.Torrent.InfoHash | ||
52 | import Data.Torrent.Magnet | ||
48 | import Network.BitTorrent.Client.Types | 53 | import Network.BitTorrent.Client.Types |
49 | import Network.BitTorrent.Client.Handle | 54 | import Network.BitTorrent.Client.Handle |
50 | import Network.BitTorrent.Core | 55 | import Network.BitTorrent.Core |
@@ -121,3 +126,32 @@ simpleClient :: BitTorrent () -> IO () | |||
121 | simpleClient m = do | 126 | simpleClient m = do |
122 | runStderrLoggingT $ LoggingT $ \ logger -> do | 127 | runStderrLoggingT $ LoggingT $ \ logger -> do |
123 | withClient def logger (`runBitTorrent` m) | 128 | withClient def logger (`runBitTorrent` m) |
129 | |||
130 | {----------------------------------------------------------------------- | ||
131 | -- Torrent identifiers | ||
132 | -----------------------------------------------------------------------} | ||
133 | |||
134 | class TorrentSource s where | ||
135 | openHandle :: FilePath -> s -> BitTorrent Handle | ||
136 | |||
137 | instance TorrentSource InfoHash where | ||
138 | openHandle path ih = openMagnet path (nullMagnet ih) | ||
139 | {-# INLINE openHandle #-} | ||
140 | |||
141 | instance TorrentSource Magnet where | ||
142 | openHandle = openMagnet | ||
143 | {-# INLINE openHandle #-} | ||
144 | |||
145 | instance TorrentSource InfoDict where | ||
146 | openHandle path dict = openTorrent path (nullTorrent dict) | ||
147 | {-# INLINE openHandle #-} | ||
148 | |||
149 | instance TorrentSource Torrent where | ||
150 | openHandle = openTorrent | ||
151 | {-# INLINE openHandle #-} | ||
152 | |||
153 | instance TorrentSource FilePath where | ||
154 | openHandle contentDir torrentPath = do | ||
155 | t <- liftIO $ fromFile torrentPath | ||
156 | openTorrent contentDir t | ||
157 | {-# INLINE openHandle #-} \ No newline at end of file | ||