diff options
-rw-r--r-- | src/Network/BitTorrent/Exchange/Session.hs | 9 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange/Session/Metadata.hs | 28 |
2 files changed, 20 insertions, 17 deletions
diff --git a/src/Network/BitTorrent/Exchange/Session.hs b/src/Network/BitTorrent/Exchange/Session.hs index 8cbce4e3..6e5d94d6 100644 --- a/src/Network/BitTorrent/Exchange/Session.hs +++ b/src/Network/BitTorrent/Exchange/Session.hs | |||
@@ -259,7 +259,8 @@ withStatusUpdates m = do | |||
259 | withMetadataUpdates :: Updates a -> Connected Session a | 259 | withMetadataUpdates :: Updates a -> Connected Session a |
260 | withMetadataUpdates m = do | 260 | withMetadataUpdates m = do |
261 | Session {..} <- asks connSession | 261 | Session {..} <- asks connSession |
262 | liftIO $ runUpdates metadata m | 262 | addr <- asks connRemoteAddr |
263 | liftIO $ runUpdates metadata addr m | ||
263 | 264 | ||
264 | getThisBitfield :: Wire Session Bitfield | 265 | getThisBitfield :: Wire Session Bitfield |
265 | getThisBitfield = do | 266 | getThisBitfield = do |
@@ -381,8 +382,7 @@ handleTransfer (Cancel bix) = filterQueue (not . (transferResponse bix)) | |||
381 | 382 | ||
382 | tryRequestMetadataBlock :: Wire Session () | 383 | tryRequestMetadataBlock :: Wire Session () |
383 | tryRequestMetadataBlock = do | 384 | tryRequestMetadataBlock = do |
384 | addr <- asks connRemoteAddr | 385 | mpix <- lift $ withMetadataUpdates Metadata.scheduleBlock |
385 | mpix <- lift $ withMetadataUpdates (Metadata.scheduleBlock addr) | ||
386 | case mpix of | 386 | case mpix of |
387 | Nothing -> undefined | 387 | Nothing -> undefined |
388 | Just pix -> sendMessage (MetadataRequest pix) | 388 | Just pix -> sendMessage (MetadataRequest pix) |
@@ -395,9 +395,8 @@ handleMetadata (MetadataRequest pix) = | |||
395 | mkResponse (Just (piece, total)) = MetadataData piece total | 395 | mkResponse (Just (piece, total)) = MetadataData piece total |
396 | 396 | ||
397 | handleMetadata (MetadataData {..}) = do | 397 | handleMetadata (MetadataData {..}) = do |
398 | addr <- asks connRemoteAddr | ||
399 | ih <- asks connTopic | 398 | ih <- asks connTopic |
400 | lift $ withMetadataUpdates (Metadata.pushBlock addr piece ih) | 399 | lift $ withMetadataUpdates (Metadata.pushBlock piece ih) |
401 | tryRequestMetadataBlock | 400 | tryRequestMetadataBlock |
402 | 401 | ||
403 | handleMetadata (MetadataReject pix) = do | 402 | handleMetadata (MetadataReject pix) = do |
diff --git a/src/Network/BitTorrent/Exchange/Session/Metadata.hs b/src/Network/BitTorrent/Exchange/Session/Metadata.hs index 5d72172a..02c2269f 100644 --- a/src/Network/BitTorrent/Exchange/Session/Metadata.hs +++ b/src/Network/BitTorrent/Exchange/Session/Metadata.hs | |||
@@ -17,6 +17,7 @@ module Network.BitTorrent.Exchange.Session.Metadata | |||
17 | 17 | ||
18 | import Control.Concurrent | 18 | import Control.Concurrent |
19 | import Control.Lens | 19 | import Control.Lens |
20 | import Control.Monad.Reader | ||
20 | import Control.Monad.State | 21 | import Control.Monad.State |
21 | import Data.ByteString as BS | 22 | import Data.ByteString as BS |
22 | import Data.ByteString.Lazy as BL | 23 | import Data.ByteString.Lazy as BL |
@@ -43,14 +44,15 @@ makeLenses ''Status | |||
43 | nullStatus :: Int -> Status | 44 | nullStatus :: Int -> Status |
44 | nullStatus ps = Status [] (Block.empty ps) | 45 | nullStatus ps = Status [] (Block.empty ps) |
45 | 46 | ||
46 | type Updates a = State Status a | 47 | type Updates = ReaderT (PeerAddr IP) (State Status) |
47 | 48 | ||
48 | runUpdates :: MVar Status -> Updates a -> IO a | 49 | runUpdates :: MVar Status -> PeerAddr IP -> Updates a -> IO a |
49 | runUpdates v m = modifyMVar v (return . swap . runState m) | 50 | runUpdates v a m = modifyMVar v (return . swap . runState (runReaderT m a)) |
50 | 51 | ||
51 | scheduleBlock :: PeerAddr IP -> Updates (Maybe PieceIx) | 52 | scheduleBlock :: Updates (Maybe PieceIx) |
52 | scheduleBlock addr = do | 53 | scheduleBlock = do |
53 | bkt <- use bucket | 54 | addr <- ask |
55 | bkt <- use bucket | ||
54 | case spans metadataPieceSize bkt of | 56 | case spans metadataPieceSize bkt of |
55 | [] -> return Nothing | 57 | [] -> return Nothing |
56 | ((off, _ ) : _) -> do | 58 | ((off, _ ) : _) -> do |
@@ -61,8 +63,10 @@ scheduleBlock addr = do | |||
61 | cancelPending :: PieceIx -> Updates () | 63 | cancelPending :: PieceIx -> Updates () |
62 | cancelPending pix = pending %= L.filter ((pix ==) . snd) | 64 | cancelPending pix = pending %= L.filter ((pix ==) . snd) |
63 | 65 | ||
64 | resetPending :: PeerAddr IP -> Updates () | 66 | resetPending :: Updates () |
65 | resetPending addr = pending %= L.filter ((addr ==) . fst) | 67 | resetPending = do |
68 | addr <- ask | ||
69 | pending %= L.filter ((addr ==) . fst) | ||
66 | 70 | ||
67 | parseInfoDict :: BS.ByteString -> InfoHash -> Result InfoDict | 71 | parseInfoDict :: BS.ByteString -> InfoHash -> Result InfoDict |
68 | parseInfoDict chunk topic = | 72 | parseInfoDict chunk topic = |
@@ -73,10 +77,10 @@ parseInfoDict chunk topic = | |||
73 | Left err -> Left $ "unable to parse infodict " ++ err | 77 | Left err -> Left $ "unable to parse infodict " ++ err |
74 | 78 | ||
75 | -- todo use incremental parsing to avoid BS.concat call | 79 | -- todo use incremental parsing to avoid BS.concat call |
76 | pushBlock :: PeerAddr IP -> Torrent.Piece BS.ByteString -> InfoHash | 80 | pushBlock :: Torrent.Piece BS.ByteString -> InfoHash -> Updates (Maybe InfoDict) |
77 | -> Updates (Maybe InfoDict) | 81 | pushBlock Torrent.Piece {..} topic = do |
78 | pushBlock addr Torrent.Piece {..} topic = do | 82 | addr <- ask |
79 | p <- use pending | 83 | p <- use pending |
80 | when ((addr, pieceIndex) `L.notElem` p) $ error "not requested" | 84 | when ((addr, pieceIndex) `L.notElem` p) $ error "not requested" |
81 | cancelPending pieceIndex | 85 | cancelPending pieceIndex |
82 | 86 | ||