summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-07 01:15:57 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-07 01:15:57 +0400
commitab9137ae8ac88a7dc0d15593a9754704e4058a52 (patch)
treeffb4af29722b1817e784c2632058d7682396a780
parente95d39a49d476e09b7d8da82bac514284f9d4e0f (diff)
~ Move exchange function to Exchange module.
-rw-r--r--src/Network/BitTorrent.hs40
-rw-r--r--src/Network/BitTorrent/Exchange.hs47
2 files changed, 43 insertions, 44 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index ef144f1f..c72498d0 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -115,43 +115,3 @@ discover swarm @ SwarmSession {..} action = {-# SCC discover #-} do
115 addr <- getPeerAddr tses 115 addr <- getPeerAddr tses
116 spawnP2P swarm addr $ do 116 spawnP2P swarm addr $ do
117 action 117 action
118
119-- Event translation table looks like:
120--
121-- Available -> Want
122-- Want -> Fragment
123-- Fragment -> Available
124--
125-- If we join the chain we get the event loop:
126--
127-- Available -> Want -> Fragment --\
128-- /|\ |
129-- \---------------------------/
130--
131
132
133-- | Default P2P action.
134exchange :: Storage -> P2P ()
135exchange storage = {-# SCC exchange #-} (awaitEvent >>= handler)
136 where
137 handler (Available bf) = do
138 liftIO (print (completeness bf))
139 ixs <- selBlk (findMin bf) storage
140 mapM_ (yieldEvent . Want) ixs -- TODO yield vectored
141
142 handler (Want bix) = do
143 blk <- liftIO $ getBlk bix storage
144 yieldEvent (Fragment blk)
145
146 handler (Fragment blk @ Block {..}) = do
147 liftIO $ print (ppBlock blk)
148 done <- liftIO $ putBlk blk storage
149 when done $ do
150 yieldEvent $ Available $ singleton blkPiece (succ blkPiece)
151
152 -- WARN this is not reliable: if peer do not return all piece
153 -- block we could slow don't until some other event occured
154 offer <- peerOffer
155 if BF.null offer
156 then return ()
157 else handler (Available offer)
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs
index b0a94853..3f3346d2 100644
--- a/src/Network/BitTorrent/Exchange.hs
+++ b/src/Network/BitTorrent/Exchange.hs
@@ -60,6 +60,7 @@ module Network.BitTorrent.Exchange
60 , awaitEvent 60 , awaitEvent
61 , yieldEvent 61 , yieldEvent
62 , handleEvent 62 , handleEvent
63 , exchange
63 64
64 -- * Exceptions 65 -- * Exceptions
65 , disconnect 66 , disconnect
@@ -85,16 +86,14 @@ import Data.Conduit.Network
85import Data.Serialize as S 86import Data.Serialize as S
86import Text.PrettyPrint as PP hiding (($$)) 87import Text.PrettyPrint as PP hiding (($$))
87 88
88
89import Network 89import Network
90 90
91 91import Data.Bitfield as BF
92import Network.BitTorrent.Internal 92import Network.BitTorrent.Internal
93import Network.BitTorrent.Extension 93import Network.BitTorrent.Extension
94import Network.BitTorrent.Peer 94import Network.BitTorrent.Peer
95import Network.BitTorrent.Exchange.Protocol 95import Network.BitTorrent.Exchange.Protocol
96import Data.Bitfield as BF 96import System.Torrent.Storage
97
98 97
99{----------------------------------------------------------------------- 98{-----------------------------------------------------------------------
100 Peer wire 99 Peer wire
@@ -464,3 +463,43 @@ yieldEvent e = {-# SCC yieldEvent #-} go e
464 463
465handleEvent :: (Event -> P2P Event) -> P2P () 464handleEvent :: (Event -> P2P Event) -> P2P ()
466handleEvent action = awaitEvent >>= action >>= yieldEvent 465handleEvent action = awaitEvent >>= action >>= yieldEvent
466
467-- Event translation table looks like:
468--
469-- Available -> Want
470-- Want -> Fragment
471-- Fragment -> Available
472--
473-- If we join the chain we get the event loop:
474--
475-- Available -> Want -> Fragment --\
476-- /|\ |
477-- \---------------------------/
478--
479
480
481-- | Default P2P action.
482exchange :: Storage -> P2P ()
483exchange storage = {-# SCC exchange #-} (awaitEvent >>= handler)
484 where
485 handler (Available bf) = do
486 liftIO (print (completeness bf))
487 ixs <- selBlk (findMin bf) storage
488 mapM_ (yieldEvent . Want) ixs -- TODO yield vectored
489
490 handler (Want bix) = do
491 blk <- liftIO $ getBlk bix storage
492 yieldEvent (Fragment blk)
493
494 handler (Fragment blk @ Block {..}) = do
495 liftIO $ print (ppBlock blk)
496 done <- liftIO $ putBlk blk storage
497 when done $ do
498 yieldEvent $ Available $ singleton blkPiece (succ blkPiece)
499
500 -- WARN this is not reliable: if peer do not return all piece
501 -- block we could slow don't until some other event occured
502 offer <- peerOffer
503 if BF.null offer
504 then return ()
505 else handler (Available offer)