summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-19 15:55:53 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-19 15:55:53 +0400
commit6253cd5c8e1b482d78881282f3d2a271b63d1a33 (patch)
treee0c0807208fb2c103a43c89e39f4fd4ba4ca47c8 /src/Network/BitTorrent.hs
parent22a3557ed14008a655d20ad216db22134442ed5b (diff)
+ Add torrent group operations.
Diffstat (limited to 'src/Network/BitTorrent.hs')
-rw-r--r--src/Network/BitTorrent.hs70
1 files changed, 63 insertions, 7 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index 7ae43cec..e68d1597 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -26,19 +26,35 @@ module Network.BitTorrent
26 , getTorrentInfo 26 , getTorrentInfo
27 , getTorrentInfoStr 27 , getTorrentInfoStr
28 28
29 -- * Torrent Groups
30 , ClientLoc (..), ppClientLoc
31 , concreteLoc, concretePath
32 , addTorrents
33 , removeTorrents
34
29 -- * Extensions 35 -- * Extensions
30 , Extension 36 , Extension
31 , defaultExtensions 37 , defaultExtensions
32 , ppExtension 38 , ppExtension
33 ) where 39 ) where
34 40
41import Control.Applicative
42import Control.Exception
43import Control.Monad
44import Data.List as L
45import Data.HashMap.Strict as HM
35import Network 46import Network
47import Text.Read
48import Text.PrettyPrint
49import System.Directory
50import System.FilePath
51
36import Data.Torrent 52import Data.Torrent
37import Network.BitTorrent.Sessions.Types 53import Network.BitTorrent.Sessions.Types
38import Network.BitTorrent.Sessions 54import Network.BitTorrent.Sessions
39import Network.BitTorrent.Extension 55import Network.BitTorrent.Extension
40import Network.BitTorrent.Tracker 56import Network.BitTorrent.Tracker
41import Text.Read 57
42 58
43-- TODO remove fork from Network.BitTorrent.Exchange 59-- TODO remove fork from Network.BitTorrent.Exchange
44-- TODO make all forks in Internal. 60-- TODO make all forks in Internal.
@@ -48,6 +64,11 @@ withDefaultClient :: PortNumber -> PortNumber -> (ClientSession -> IO ()) -> IO
48withDefaultClient listPort dhtPort action = do 64withDefaultClient listPort dhtPort action = do
49 withClientSession defaultThreadCount [] listPort dhtPort action 65 withClientSession defaultThreadCount [] listPort dhtPort action
50 66
67getTorrentInfoStr :: ClientSession -> String -> IO (Maybe Torrent)
68getTorrentInfoStr cs str
69 | Just infohash <- readMaybe str = getTorrentInfo cs infohash
70 | otherwise = return Nothing
71
51{----------------------------------------------------------------------- 72{-----------------------------------------------------------------------
52 Torrent management 73 Torrent management
53-----------------------------------------------------------------------} 74-----------------------------------------------------------------------}
@@ -61,12 +82,7 @@ addTorrent cs loc @ TorrentLoc {..} = do
61 82
62-- | Unregister torrent and stop all running sessions. 83-- | Unregister torrent and stop all running sessions.
63removeTorrent :: ClientSession -> InfoHash -> IO () 84removeTorrent :: ClientSession -> InfoHash -> IO ()
64removeTorrent ses loc = undefined -- atomically $ unregisterTorrent ses loc 85removeTorrent = unregisterTorrent
65
66getTorrentInfoStr :: ClientSession -> String -> IO (Maybe Torrent)
67getTorrentInfoStr cs str
68 | Just infohash <- readMaybe str = getTorrentInfo cs infohash
69 | otherwise = return Nothing
70 86
71{- 87{-
72-- | The same as 'removeTorrrent' torrent, but delete all torrent 88-- | The same as 'removeTorrrent' torrent, but delete all torrent
@@ -74,3 +90,43 @@ getTorrentInfoStr cs str
74deleteTorrent :: ClientSession -> TorrentLoc -> IO () 90deleteTorrent :: ClientSession -> TorrentLoc -> IO ()
75deleteTorrent ClientSession {..} TorrentLoc {..} = undefined 91deleteTorrent ClientSession {..} TorrentLoc {..} = undefined
76-} 92-}
93
94{-----------------------------------------------------------------------
95 Torrent group management
96-----------------------------------------------------------------------}
97-- TODO better name
98
99data ClientLoc = ClientLoc
100 { tdir :: FilePath -- ^ Path to directory with .torrent files.
101 , ddir :: FilePath -- ^ Path to directory to place content.
102 } deriving (Show, Eq)
103
104ppClientLoc :: ClientLoc -> Doc
105ppClientLoc ClientLoc {..} =
106 text "torrent directory" <+> text tdir $$
107 text "data directory" <+> text ddir
108
109concretePath :: ClientLoc -> FilePath -> FilePath
110concretePath ClientLoc {..} relPath = tdir </> relPath
111
112concreteLoc :: ClientLoc -> FilePath -> TorrentLoc
113concreteLoc loc @ ClientLoc {..} relPath
114 = TorrentLoc (concretePath loc relPath) ddir
115
116addTorrents :: ClientSession -> ClientLoc -> IO ()
117addTorrents ses loc @ ClientLoc {..} = do
118 paths <- L.filter isTorrentPath <$> getDirectoryContents tdir
119 forM_ paths $ handle handler . addTorrent ses . concreteLoc loc
120 where
121 handler :: SomeException -> IO ()
122 handler = print
123
124removeTorrents :: ClientSession -> IO ()
125removeTorrents cs = do
126 tm <- getRegistered cs
127 forM_ (keys tm) (removeTorrent cs)
128
129{-
130deleteTorrents :: ClientSession -> IO ()
131deleteTorrents = undefined
132-} \ No newline at end of file