diff options
Diffstat (limited to 'src/Network/BitTorrent.hs')
-rw-r--r-- | src/Network/BitTorrent.hs | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs index 86c7802b..30735023 100644 --- a/src/Network/BitTorrent.hs +++ b/src/Network/BitTorrent.hs | |||
@@ -35,6 +35,8 @@ module Network.BitTorrent | |||
35 | 35 | ||
36 | -- * Storage | 36 | -- * Storage |
37 | , Storage | 37 | , Storage |
38 | , ppStorage | ||
39 | |||
38 | , bindTo | 40 | , bindTo |
39 | , unbind | 41 | , unbind |
40 | 42 | ||
@@ -80,7 +82,7 @@ import Control.Monad.Reader | |||
80 | 82 | ||
81 | import Network | 83 | import Network |
82 | 84 | ||
83 | import Data.Bitfield | 85 | import Data.Bitfield as BF |
84 | import Data.Torrent | 86 | import Data.Torrent |
85 | import Network.BitTorrent.Internal | 87 | import Network.BitTorrent.Internal |
86 | import Network.BitTorrent.Exchange | 88 | import Network.BitTorrent.Exchange |
@@ -132,17 +134,24 @@ discover swarm action = do | |||
132 | 134 | ||
133 | -- | Default P2P action. | 135 | -- | Default P2P action. |
134 | exchange :: Storage -> P2P () | 136 | exchange :: Storage -> P2P () |
135 | exchange storage = handleEvent (\msg -> liftIO (print msg) >> handler msg) | 137 | exchange storage = awaitEvent >>= handler |
136 | where | 138 | where |
137 | handler (Available bf) | 139 | handler (Available bf) = do |
138 | | Just m <- findMin bf = return (Want (BlockIx m 0 262144)) | 140 | liftIO (print (completeness bf)) |
139 | | otherwise = error "impossible" | 141 | ixs <- selBlk (findMin bf) storage |
140 | -- TODO findMin :: Bitfield -> PieceIx | 142 | mapM_ (yieldEvent . Want) ixs -- TODO yield vectored |
141 | 143 | ||
142 | handler (Want bix) = do | 144 | handler (Want bix) = do |
143 | blk <- liftIO $ getBlk bix storage | 145 | blk <- liftIO $ getBlk bix storage |
144 | return (Fragment blk) | 146 | yieldEvent (Fragment blk) |
145 | 147 | ||
146 | handler (Fragment blk) = do | 148 | handler (Fragment blk @ Block {..}) = do |
147 | liftIO $ putBlk blk storage | 149 | liftIO $ print (ppBlock blk) |
148 | return (Available (singleton (blkPiece blk) (error "singleton") )) | 150 | done <- liftIO $ putBlk blk storage |
151 | when done $ do | ||
152 | yieldEvent $ Available $ singleton blkPiece (succ blkPiece) | ||
153 | |||
154 | offer <- peerOffer | ||
155 | if BF.null offer | ||
156 | then return () | ||
157 | else handler (Available offer) \ No newline at end of file | ||