diff options
-rw-r--r-- | src/Network/BitTorrent.hs | 4 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client.hs | 10 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client/Handle.hs | 38 | ||||
-rw-r--r-- | src/Network/BitTorrent/Client/Types.hs | 8 |
4 files changed, 34 insertions, 26 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index ce02322f..a328bb5e 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -37,7 +37,9 @@ module Network.BitTorrent | |||
37 | 37 | ||
38 | -- ** Handle | 38 | -- ** Handle |
39 | , Handle | 39 | , Handle |
40 | , topic | 40 | , handleTopic |
41 | , handleTrackers | ||
42 | |||
41 | , TorrentSource(openHandle) | 43 | , TorrentSource(openHandle) |
42 | , closeHandle | 44 | , closeHandle |
43 | , getHandle | 45 | , getHandle |
diff --git a/src/Network/BitTorrent/Client.hs b/src/Network/BitTorrent/Client.hs index 2ab61ae0..0eeb6ecd 100644 --- a/src/Network/BitTorrent/Client.hs +++ b/src/Network/BitTorrent/Client.hs | |||
@@ -28,12 +28,18 @@ module Network.BitTorrent.Client | |||
28 | 28 | ||
29 | -- * Handle | 29 | -- * Handle |
30 | , Handle | 30 | , Handle |
31 | , topic | 31 | , handleTopic |
32 | , handleTrackers | ||
33 | |||
34 | -- ** Construction | ||
32 | , TorrentSource (..) | 35 | , TorrentSource (..) |
33 | , closeHandle | 36 | , closeHandle |
37 | |||
38 | -- ** Query | ||
34 | , getHandle | 39 | , getHandle |
35 | , getIndex | 40 | , getIndex |
36 | 41 | ||
42 | -- ** Management | ||
37 | , start | 43 | , start |
38 | , pause | 44 | , pause |
39 | , stop | 45 | , stop |
@@ -93,7 +99,7 @@ connHandler tmap ih = do | |||
93 | m <- readMVar tmap | 99 | m <- readMVar tmap |
94 | case HM.lookup ih m of | 100 | case HM.lookup ih m of |
95 | Nothing -> error "torrent not found" | 101 | Nothing -> error "torrent not found" |
96 | Just (Handle {..}) -> return exchange | 102 | Just (Handle {..}) -> return handleExchange |
97 | 103 | ||
98 | initClient :: Options -> LogFun -> ResIO Client | 104 | initClient :: Options -> LogFun -> ResIO Client |
99 | initClient opts @ Options {..} logFun = do | 105 | initClient opts @ Options {..} logFun = do |
diff --git a/src/Network/BitTorrent/Client/Handle.hs b/src/Network/BitTorrent/Client/Handle.hs index a1fe7b9e..258af3fb 100644 --- a/src/Network/BitTorrent/Client/Handle.hs +++ b/src/Network/BitTorrent/Client/Handle.hs | |||
@@ -82,10 +82,10 @@ openTorrent rootPath t @ Torrent {..} = do | |||
82 | eses <- liftIO $ Exchange.newSession clientLogger (externalAddr c) | 82 | eses <- liftIO $ Exchange.newSession clientLogger (externalAddr c) |
83 | rootPath tInfoDict | 83 | rootPath tInfoDict |
84 | return $ Handle | 84 | return $ Handle |
85 | { topic = ih | 85 | { handleTopic = ih |
86 | , private = idPrivate tInfoDict | 86 | , handlePrivate = idPrivate tInfoDict |
87 | , trackers = tses | 87 | , handleTrackers = tses |
88 | , exchange = eses | 88 | , handleExchange = eses |
89 | } | 89 | } |
90 | 90 | ||
91 | -- | Use 'nullMagnet' to open handle from 'InfoHash'. | 91 | -- | Use 'nullMagnet' to open handle from 'InfoHash'. |
@@ -97,10 +97,10 @@ openMagnet rootPath uri @ Magnet {..} = do | |||
97 | eses <- liftIO $ Exchange.newSession clientLogger (externalAddr c) | 97 | eses <- liftIO $ Exchange.newSession clientLogger (externalAddr c) |
98 | rootPath (error "openMagnet" exactTopic) | 98 | rootPath (error "openMagnet" exactTopic) |
99 | return $ Handle | 99 | return $ Handle |
100 | { topic = exactTopic | 100 | { handleTopic = exactTopic |
101 | , private = False | 101 | , handlePrivate = False |
102 | , trackers = tses | 102 | , handleTrackers = tses |
103 | , exchange = eses | 103 | , handleExchange = eses |
104 | } | 104 | } |
105 | 105 | ||
106 | -- | Stop torrent and destroy all sessions. You don't need to close | 106 | -- | Stop torrent and destroy all sessions. You don't need to close |
@@ -109,10 +109,10 @@ openMagnet rootPath uri @ Magnet {..} = do | |||
109 | -- may block. | 109 | -- may block. |
110 | closeHandle :: Handle -> BitTorrent () | 110 | closeHandle :: Handle -> BitTorrent () |
111 | closeHandle h @ Handle {..} = do | 111 | closeHandle h @ Handle {..} = do |
112 | freeHandle topic $ do | 112 | freeHandle handleTopic $ do |
113 | stop h | 113 | stop h |
114 | liftIO $ Exchange.closeSession exchange | 114 | liftIO $ Exchange.closeSession handleExchange |
115 | liftIO $ Tracker.closeSession trackers | 115 | liftIO $ Tracker.closeSession handleTrackers |
116 | 116 | ||
117 | {----------------------------------------------------------------------- | 117 | {----------------------------------------------------------------------- |
118 | -- Control | 118 | -- Control |
@@ -125,14 +125,14 @@ closeHandle h @ Handle {..} = do | |||
125 | start :: Handle -> BitTorrent () | 125 | start :: Handle -> BitTorrent () |
126 | start Handle {..} = do | 126 | start Handle {..} = do |
127 | Client {..} <- getClient | 127 | Client {..} <- getClient |
128 | liftIO $ Tracker.notify trackerManager trackers Tracker.Started | 128 | liftIO $ Tracker.notify trackerManager handleTrackers Tracker.Started |
129 | unless private $ do | 129 | unless handlePrivate $ do |
130 | liftDHT $ DHT.insert topic (error "start") | 130 | liftDHT $ DHT.insert handleTopic (error "start") |
131 | liftIO $ do | 131 | liftIO $ do |
132 | peers <- askPeers trackerManager trackers | 132 | peers <- askPeers trackerManager handleTrackers |
133 | print $ "got: " ++ show (L.length peers) ++ " peers" | 133 | print $ "got: " ++ show (L.length peers) ++ " peers" |
134 | forM_ peers $ \ peer -> do | 134 | forM_ peers $ \ peer -> do |
135 | Exchange.connect peer exchange | 135 | Exchange.connect peer handleExchange |
136 | 136 | ||
137 | -- | Stop downloading this torrent. | 137 | -- | Stop downloading this torrent. |
138 | pause :: Handle -> BitTorrent () | 138 | pause :: Handle -> BitTorrent () |
@@ -142,9 +142,9 @@ pause _ = return () | |||
142 | stop :: Handle -> BitTorrent () | 142 | stop :: Handle -> BitTorrent () |
143 | stop Handle {..} = do | 143 | stop Handle {..} = do |
144 | Client {..} <- getClient | 144 | Client {..} <- getClient |
145 | unless private $ do | 145 | unless handlePrivate $ do |
146 | liftDHT $ DHT.delete topic (error "stop") | 146 | liftDHT $ DHT.delete handleTopic (error "stop") |
147 | liftIO $ Tracker.notify trackerManager trackers Tracker.Stopped | 147 | liftIO $ Tracker.notify trackerManager handleTrackers Tracker.Stopped |
148 | 148 | ||
149 | {----------------------------------------------------------------------- | 149 | {----------------------------------------------------------------------- |
150 | -- Query | 150 | -- Query |
diff --git a/src/Network/BitTorrent/Client/Types.hs b/src/Network/BitTorrent/Client/Types.hs index 31ebca2d..3e62f0fa 100644 --- a/src/Network/BitTorrent/Client/Types.hs +++ b/src/Network/BitTorrent/Client/Types.hs | |||
@@ -32,10 +32,10 @@ import Network.BitTorrent.Exchange as Exchange | |||
32 | import Network.BitTorrent.Tracker as Tracker | 32 | import Network.BitTorrent.Tracker as Tracker |
33 | 33 | ||
34 | data Handle = Handle | 34 | data Handle = Handle |
35 | { topic :: !InfoHash | 35 | { handleTopic :: !InfoHash |
36 | , private :: !Bool | 36 | , handlePrivate :: !Bool |
37 | , trackers :: !Tracker.Session | 37 | , handleTrackers :: !Tracker.Session |
38 | , exchange :: !Exchange.Session | 38 | , handleExchange :: !Exchange.Session |
39 | } | 39 | } |
40 | 40 | ||
41 | data Client = Client | 41 | data Client = Client |