diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 20:06:13 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 20:06:13 +0400 |
commit | 3eaa3003d9ece714595dffbf07202ee90336d05f (patch) | |
tree | 4dfa6528139a37dc194da60cb01161ebff9fd5d9 /src/System/Torrent/Storage.hs | |
parent | b925e91c5e53a4300e5b0511e8811c5f1af9e803 (diff) |
Add storage streaming
Diffstat (limited to 'src/System/Torrent/Storage.hs')
-rw-r--r-- | src/System/Torrent/Storage.hs | 24 |
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 | ||
41 | import Control.Applicative | 45 | import Control.Applicative |
42 | import Control.Exception | 46 | import Control.Exception |
47 | import Control.Monad.Trans | ||
43 | import Data.ByteString.Lazy as BL | 48 | import Data.ByteString.Lazy as BL |
49 | import Data.Conduit | ||
44 | import Data.Typeable | 50 | import Data.Typeable |
45 | 51 | ||
46 | import Data.Torrent.Bitfield as BF | 52 | import 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. | ||
130 | sourceStorage :: Storage -> Source IO (Piece BL.ByteString) | ||
131 | sourceStorage 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'. | ||
142 | sinkStorage :: Storage -> Sink (Piece BL.ByteString) IO () | ||
143 | sinkStorage s = do | ||
144 | awaitForever $ \ piece -> | ||
145 | liftIO $ writePiece piece s | ||
146 | |||
123 | -- | TODO examples of use | 147 | -- | TODO examples of use |
124 | genPieceInfo :: Storage -> IO PieceInfo | 148 | genPieceInfo :: Storage -> IO PieceInfo |
125 | genPieceInfo = undefined | 149 | genPieceInfo = undefined |