summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-14 03:25:46 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-14 03:25:46 +0400
commit5a6e5634452ff463a7442dbd8761678651517d20 (patch)
tree5ca9f455dee0aa6fcbf67e34f23b753f00bf3d55 /src/Network
parente160fcee699c7012d8f50a6e4bcd3b8a9c02aedf (diff)
~ Minor changes.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent.hs3
-rw-r--r--src/Network/BitTorrent/Sessions.hs43
-rw-r--r--src/Network/BitTorrent/Sessions/Types.lhs8
3 files changed, 33 insertions, 21 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index 8c3189d3..c166b1b1 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -46,8 +46,7 @@ import System.Torrent.Storage
46-- | Client session with default parameters. Use it for testing only. 46-- | Client session with default parameters. Use it for testing only.
47withDefaultClient :: PortNumber -> PortNumber -> (ClientSession -> IO ()) -> IO () 47withDefaultClient :: PortNumber -> PortNumber -> (ClientSession -> IO ()) -> IO ()
48withDefaultClient dhtPort listPort action = do 48withDefaultClient dhtPort listPort action = do
49 withClientSession defaultThreadCount defaultExtensions $ \client -> do 49 withClientSession defaultThreadCount defaultExtensions listPort dhtPort action
50 action client
51 50
52{----------------------------------------------------------------------- 51{-----------------------------------------------------------------------
53 Torrent management 52 Torrent management
diff --git a/src/Network/BitTorrent/Sessions.hs b/src/Network/BitTorrent/Sessions.hs
index 2e51fde6..9a1d0c6a 100644
--- a/src/Network/BitTorrent/Sessions.hs
+++ b/src/Network/BitTorrent/Sessions.hs
@@ -48,7 +48,7 @@ module Network.BitTorrent.Sessions
48 , discover 48 , discover
49 ) where 49 ) where
50 50
51import Prelude hiding (mapM_) 51import Prelude hiding (mapM_, elem)
52 52
53import Control.Applicative 53import Control.Applicative
54import Control.Concurrent 54import Control.Concurrent
@@ -61,6 +61,7 @@ import Control.Monad.Trans
61import Data.IORef 61import Data.IORef
62import Data.Map as M 62import Data.Map as M
63import Data.HashMap.Strict as HM 63import Data.HashMap.Strict as HM
64import Data.Foldable as F
64import Data.Set as S 65import Data.Set as S
65 66
66import Data.Serialize hiding (get) 67import Data.Serialize hiding (get)
@@ -141,14 +142,13 @@ startDHT ClientSession {..} nodePort = withRunning peerListener failure start
141 142
142-- | Create a new client session. The data passed to this function are 143-- | Create a new client session. The data passed to this function are
143-- usually loaded from configuration file. 144-- usually loaded from configuration file.
144openClientSession :: SessionCount -- ^ Maximum count of active P2P Sessions. 145openClientSession :: SessionCount -> [Extension] -> PortNumber -> PortNumber -> IO ClientSession
145 -> [Extension] -- ^ Extensions allowed to use. 146openClientSession n exts listenerPort _ = do
146 -> IO ClientSession -- ^ Client with unique peer ID.
147openClientSession n exts = do
148 mgr <- Ev.new 147 mgr <- Ev.new
149 -- TODO kill this thread when leave client 148 -- TODO kill this thread when leave client
150 _ <- forkIO $ loop mgr 149 _ <- forkIO $ loop mgr
151 ClientSession 150
151 cs <- ClientSession
152 <$> genPeerId 152 <$> genPeerId
153 <*> pure exts 153 <*> pure exts
154 <*> newEmptyMVar 154 <*> newEmptyMVar
@@ -160,13 +160,21 @@ openClientSession n exts = do
160 <*> newTVarIO (startProgress 0) 160 <*> newTVarIO (startProgress 0)
161 <*> newTVarIO HM.empty 161 <*> newTVarIO HM.empty
162 162
163 startListener cs listenerPort
164 return cs
165
163closeClientSession :: ClientSession -> IO () 166closeClientSession :: ClientSession -> IO ()
164closeClientSession ClientSession {..} = 167closeClientSession ClientSession {..} = do
165 stopService nodeListener `finally` stopService peerListener 168 stopService nodeListener
166-- TODO stop all swarm sessions 169 stopService peerListener
170
171 sws <- readTVarIO swarmSessions
172 forM_ sws closeSwarmSession
167 173
168withClientSession :: SessionCount -> [Extension] -> (ClientSession -> IO ()) -> IO () 174withClientSession :: SessionCount -> [Extension]
169withClientSession c es = bracket (openClientSession c es) closeClientSession 175 -> PortNumber -> PortNumber
176 -> (ClientSession -> IO ()) -> IO ()
177withClientSession c es l d = bracket (openClientSession c es l d) closeClientSession
170 178
171-- | Get current global progress of the client. This value is usually 179-- | Get current global progress of the client. This value is usually
172-- shown to a user. 180-- shown to a user.
@@ -222,6 +230,14 @@ discover swarm @ SwarmSession {..} action = {-# SCC discover #-} do
222 initiatePeerSession swarm addr $ \conn -> 230 initiatePeerSession swarm addr $ \conn ->
223 runP2P conn action 231 runP2P conn action
224 232
233registerSwarmSession :: SwarmSession -> IO ()
234registerSwarmSession = undefined
235
236unregisterSwarmSession :: SwarmSession -> IO ()
237unregisterSwarmSession SwarmSession {..} =
238 atomically $ modifyTVar (swarmSessions clientSession) $
239 M.delete $ tInfoHash torrentMeta
240
225newSwarmSession :: Int -> Bitfield -> ClientSession -> Torrent 241newSwarmSession :: Int -> Bitfield -> ClientSession -> Torrent
226 -> IO SwarmSession 242 -> IO SwarmSession
227newSwarmSession n bf cs @ ClientSession {..} t @ Torrent {..} 243newSwarmSession n bf cs @ ClientSession {..} t @ Torrent {..}
@@ -246,11 +262,6 @@ closeSwarmSession se @ SwarmSession {..} = do
246 -- TODO the order is important! 262 -- TODO the order is important!
247 closeStorage storage 263 closeStorage storage
248 264
249unregisterSwarmSession :: SwarmSession -> IO ()
250unregisterSwarmSession SwarmSession {..} =
251 atomically $ modifyTVar (swarmSessions clientSession) $
252 M.delete $ tInfoHash torrentMeta
253
254getSwarm :: ClientSession -> InfoHash -> IO SwarmSession 265getSwarm :: ClientSession -> InfoHash -> IO SwarmSession
255getSwarm cs @ ClientSession {..} ih = do 266getSwarm cs @ ClientSession {..} ih = do
256 ss <- readTVarIO swarmSessions 267 ss <- readTVarIO swarmSessions
diff --git a/src/Network/BitTorrent/Sessions/Types.lhs b/src/Network/BitTorrent/Sessions/Types.lhs
index f94dbfa6..3f9c6db1 100644
--- a/src/Network/BitTorrent/Sessions/Types.lhs
+++ b/src/Network/BitTorrent/Sessions/Types.lhs
@@ -73,10 +73,12 @@ Thread layout
73 73
74When client session created 2 new threads appear: 74When client session created 2 new threads appear:
75 75
76 * DHT listener - replies to DHT requests;
77
78 * Peer listener - accept new P2P connection initiated by other 76 * Peer listener - accept new P2P connection initiated by other
79peers. 77peers;
78
79 * Tracker announcer - announce that the peer have this torrent.
80
81 * OPTIONAL: DHT listener - replies to DHT requests;
80 82
81When swarn session created 3 new threads appear: 83When swarn session created 3 new threads appear:
82 84