diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-12 11:16:21 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-12 11:16:21 +0400 |
commit | 4e30588737415d59fa36aa7308c037bb8bd8e3d5 (patch) | |
tree | 110178a60255633036ac4cdb0fdf4367d8aaf907 /src/Network/BitTorrent/Internal.hs | |
parent | eadb3a6826fb784c33b10b2eab7d4f7bf72b0043 (diff) |
+ Add session exception.
Diffstat (limited to 'src/Network/BitTorrent/Internal.hs')
-rw-r--r-- | src/Network/BitTorrent/Internal.hs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Internal.hs b/src/Network/BitTorrent/Internal.hs index da104240..a3e1a5cd 100644 --- a/src/Network/BitTorrent/Internal.hs +++ b/src/Network/BitTorrent/Internal.hs | |||
@@ -16,6 +16,7 @@ | |||
16 | {-# LANGUAGE OverloadedStrings #-} | 16 | {-# LANGUAGE OverloadedStrings #-} |
17 | {-# LANGUAGE RecordWildCards #-} | 17 | {-# LANGUAGE RecordWildCards #-} |
18 | {-# LANGUAGE TemplateHaskell #-} | 18 | {-# LANGUAGE TemplateHaskell #-} |
19 | {-# LANGUAGE DeriveDataTypeable #-} | ||
19 | {-# LANGUAGE FlexibleInstances #-} | 20 | {-# LANGUAGE FlexibleInstances #-} |
20 | {-# LANGUAGE FlexibleContexts #-} | 21 | {-# LANGUAGE FlexibleContexts #-} |
21 | {-# LANGUAGE MultiParamTypeClasses #-} | 22 | {-# LANGUAGE MultiParamTypeClasses #-} |
@@ -23,20 +24,31 @@ | |||
23 | module Network.BitTorrent.Internal | 24 | module Network.BitTorrent.Internal |
24 | ( Progress(..), startProgress | 25 | ( Progress(..), startProgress |
25 | 26 | ||
27 | -- * Client | ||
26 | , ClientSession (clientPeerID, allowedExtensions) | 28 | , ClientSession (clientPeerID, allowedExtensions) |
27 | , newClient, getCurrentProgress | 29 | , newClient, getCurrentProgress |
28 | 30 | ||
31 | -- * Swarm | ||
29 | , SwarmSession(SwarmSession, torrentMeta, clientSession) | 32 | , SwarmSession(SwarmSession, torrentMeta, clientSession) |
30 | , newLeacher, newSeeder | 33 | , newLeacher, newSeeder |
31 | 34 | ||
35 | -- * Peer | ||
32 | , PeerSession(PeerSession, connectedPeerAddr | 36 | , PeerSession(PeerSession, connectedPeerAddr |
33 | , swarmSession, enabledExtensions | 37 | , swarmSession, enabledExtensions |
34 | ) | 38 | ) |
35 | , SessionState | 39 | , SessionState |
40 | , withPeerSession | ||
41 | |||
42 | -- ** Exceptions | ||
43 | , SessionException(..) | ||
44 | , isSessionException | ||
45 | , putSessionException | ||
46 | , sessionError | ||
47 | |||
48 | -- ** Properties | ||
36 | , bitfield, status | 49 | , bitfield, status |
37 | , emptyBF, fullBF, singletonBF, adjustBF | 50 | , emptyBF, fullBF, singletonBF, adjustBF |
38 | , getPieceCount, getClientBF | 51 | , getPieceCount, getClientBF |
39 | , sessionError, withPeerSession | ||
40 | 52 | ||
41 | -- * Timeouts | 53 | -- * Timeouts |
42 | , updateIncoming, updateOutcoming | 54 | , updateIncoming, updateOutcoming |
@@ -55,6 +67,7 @@ import Data.Default | |||
55 | import Data.Function | 67 | import Data.Function |
56 | import Data.Ord | 68 | import Data.Ord |
57 | import Data.Set as S | 69 | import Data.Set as S |
70 | import Data.Typeable | ||
58 | 71 | ||
59 | import Data.Serialize hiding (get) | 72 | import Data.Serialize hiding (get) |
60 | import Text.PrettyPrint | 73 | import Text.PrettyPrint |
@@ -212,6 +225,7 @@ data PeerSession = PeerSession { | |||
212 | -- to avoid reduntant KA messages. | 225 | -- to avoid reduntant KA messages. |
213 | , outcomingTimeout :: TimeoutKey | 226 | , outcomingTimeout :: TimeoutKey |
214 | 227 | ||
228 | -- TODO use dupChan for broadcasting | ||
215 | , broadcastMessages :: Chan [Message] | 229 | , broadcastMessages :: Chan [Message] |
216 | , sessionState :: IORef SessionState | 230 | , sessionState :: IORef SessionState |
217 | } | 231 | } |
@@ -234,6 +248,17 @@ instance (MonadIO m, MonadReader PeerSession m) | |||
234 | get = asks sessionState >>= liftIO . readIORef | 248 | get = asks sessionState >>= liftIO . readIORef |
235 | put s = asks sessionState >>= \ref -> liftIO $ writeIORef ref s | 249 | put s = asks sessionState >>= \ref -> liftIO $ writeIORef ref s |
236 | 250 | ||
251 | data SessionException = SessionException | ||
252 | deriving (Show, Typeable) | ||
253 | |||
254 | instance Exception SessionException | ||
255 | |||
256 | isSessionException :: Monad m => SessionException -> m () | ||
257 | isSessionException _ = return () | ||
258 | |||
259 | putSessionException :: SessionException -> IO () | ||
260 | putSessionException = print | ||
261 | |||
237 | sessionError :: MonadIO m => Doc -> m () | 262 | sessionError :: MonadIO m => Doc -> m () |
238 | sessionError msg | 263 | sessionError msg |
239 | = liftIO $ throwIO $ userError $ render $ msg <+> "in session" | 264 | = liftIO $ throwIO $ userError $ render $ msg <+> "in session" |