summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent.hs')
-rw-r--r--src/Network/BitTorrent.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/Network/BitTorrent.hs b/src/Network/BitTorrent.hs
index ec0adb51..b97db4b0 100644
--- a/src/Network/BitTorrent.hs
+++ b/src/Network/BitTorrent.hs
@@ -73,6 +73,7 @@ import Control.Monad.Reader
73 73
74import Network 74import Network
75 75
76import Data.Bitfield
76import Data.Torrent 77import Data.Torrent
77import Network.BitTorrent.Internal 78import Network.BitTorrent.Internal
78import Network.BitTorrent.Exchange 79import Network.BitTorrent.Exchange
@@ -81,6 +82,8 @@ import Network.BitTorrent.Tracker
81import Network.BitTorrent.Extension 82import Network.BitTorrent.Extension
82import Network.BitTorrent.Peer 83import Network.BitTorrent.Peer
83 84
85import System.Torrent.Storage
86
84 87
85-- | Client session with default parameters. Use it for testing only. 88-- | Client session with default parameters. Use it for testing only.
86defaultClient :: IO ClientSession 89defaultClient :: IO ClientSession
@@ -105,3 +108,33 @@ discover swarm action = do
105 addr <- getPeerAddr tses 108 addr <- getPeerAddr tses
106 spawnP2P swarm addr $ do 109 spawnP2P swarm addr $ do
107 action 110 action
111
112-- Event translation table looks like:
113--
114-- Available -> Want
115-- Want -> Fragment
116-- Fragment -> Available
117--
118-- If we join the chain we get the event loop:
119--
120-- Available -> Want -> Fragment --\
121-- /|\ |
122-- \---------------------------/
123--
124
125-- | Default P2P action.
126exchange :: Storage -> P2P ()
127exchange storage = handleEvent handler
128 where
129 handler (Available bf)
130 | Just m <- findMin bf = return (Want (BlockIx m 0 10))
131 | otherwise = error "impossible"
132 -- TODO findMin :: Bitfield -> PieceIx
133
134 handler (Want bix) = do
135 blk <- liftIO $ getBlk bix storage
136 return (Fragment blk)
137
138 handler (Fragment blk) = do
139 liftIO $ putBlk blk storage
140 return (Available (singleton (blkPiece blk) (error "singleton") ))