summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-30 05:18:24 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-30 05:18:24 +0400
commitc15da2e2b376d81671f35e821e94db19e59d5ddd (patch)
tree7bcc2c929df2dd49f27ef3083eb830344b3d7685 /src/Network/BitTorrent.hs
parentf556bf196bf07308f024cc43c1a51dfd4c21188c (diff)
+ Add very basic storage operations.
Now we can download and make some progress, but very unstable.
Diffstat (limited to 'src/Network/BitTorrent.hs')
-rw-r--r--src/Network/BitTorrent.hs31
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
81import Network 83import Network
82 84
83import Data.Bitfield 85import Data.Bitfield as BF
84import Data.Torrent 86import Data.Torrent
85import Network.BitTorrent.Internal 87import Network.BitTorrent.Internal
86import Network.BitTorrent.Exchange 88import Network.BitTorrent.Exchange
@@ -132,17 +134,24 @@ discover swarm action = do
132 134
133-- | Default P2P action. 135-- | Default P2P action.
134exchange :: Storage -> P2P () 136exchange :: Storage -> P2P ()
135exchange storage = handleEvent (\msg -> liftIO (print msg) >> handler msg) 137exchange 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