diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-25 17:40:24 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-02-25 17:40:24 +0400 |
commit | 4688e69dbb16899d898d06aa643f35c4d2fa2df8 (patch) | |
tree | cf1df5a201d3fc128fb396a8c4c1b95892690b7f /src/Network/BitTorrent/Exchange/Session | |
parent | f6f4e61099065251ef77e43ff10bb98c8b2507be (diff) |
Pass peer address in metadata scheduler implicitly
Diffstat (limited to 'src/Network/BitTorrent/Exchange/Session')
-rw-r--r-- | src/Network/BitTorrent/Exchange/Session/Metadata.hs | 28 |
1 files changed, 16 insertions, 12 deletions
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 | ||