summaryrefslogtreecommitdiff
path: root/src/System
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-01-05 20:06:13 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 20:06:13 +0400
commit3eaa3003d9ece714595dffbf07202ee90336d05f (patch)
tree4dfa6528139a37dc194da60cb01161ebff9fd5d9 /src/System
parentb925e91c5e53a4300e5b0511e8811c5f1af9e803 (diff)
Add storage streaming
Diffstat (limited to 'src/System')
-rw-r--r--src/System/Torrent/Storage.hs24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs
index 1c84bf69..3bb229a4 100644
--- a/src/System/Torrent/Storage.hs
+++ b/src/System/Torrent/Storage.hs
@@ -36,11 +36,17 @@ module System.Torrent.Storage
36 , readPiece 36 , readPiece
37 , hintRead 37 , hintRead
38 , unsafeReadPiece 38 , unsafeReadPiece
39
40 -- * Streaming
41 , sourceStorage
42 , sinkStorage
39 ) where 43 ) where
40 44
41import Control.Applicative 45import Control.Applicative
42import Control.Exception 46import Control.Exception
47import Control.Monad.Trans
43import Data.ByteString.Lazy as BL 48import Data.ByteString.Lazy as BL
49import Data.Conduit
44import Data.Typeable 50import Data.Typeable
45 51
46import Data.Torrent.Bitfield as BF 52import Data.Torrent.Bitfield as BF
@@ -120,6 +126,24 @@ unsafeReadPiece pix s @ Storage {..}
120 offset = fromIntegral pix * fromIntegral pieceLen 126 offset = fromIntegral pix * fromIntegral pieceLen
121 sz = fromIntegral pieceLen 127 sz = fromIntegral pieceLen
122 128
129-- | Stream storage pieces from first to the last.
130sourceStorage :: Storage -> Source IO (Piece BL.ByteString)
131sourceStorage s = go 0
132 where
133 go pix
134 | pix < totalPieces s = do
135 piece <- liftIO $ readPiece pix s
136 liftIO $ hintRead (succ pix) s
137 yield piece
138 go (succ pix)
139 | otherwise = return ()
140
141-- | Write stream of pieces to the storage. Fail if storage is 'ReadOnly'.
142sinkStorage :: Storage -> Sink (Piece BL.ByteString) IO ()
143sinkStorage s = do
144 awaitForever $ \ piece ->
145 liftIO $ writePiece piece s
146
123-- | TODO examples of use 147-- | TODO examples of use
124genPieceInfo :: Storage -> IO PieceInfo 148genPieceInfo :: Storage -> IO PieceInfo
125genPieceInfo = undefined 149genPieceInfo = undefined