diff options
Diffstat (limited to 'src/Network/BitTorrent.hs')
-rw-r--r-- | src/Network/BitTorrent.hs | 33 |
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 | ||
74 | import Network | 74 | import Network |
75 | 75 | ||
76 | import Data.Bitfield | ||
76 | import Data.Torrent | 77 | import Data.Torrent |
77 | import Network.BitTorrent.Internal | 78 | import Network.BitTorrent.Internal |
78 | import Network.BitTorrent.Exchange | 79 | import Network.BitTorrent.Exchange |
@@ -81,6 +82,8 @@ import Network.BitTorrent.Tracker | |||
81 | import Network.BitTorrent.Extension | 82 | import Network.BitTorrent.Extension |
82 | import Network.BitTorrent.Peer | 83 | import Network.BitTorrent.Peer |
83 | 84 | ||
85 | import 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. |
86 | defaultClient :: IO ClientSession | 89 | defaultClient :: 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. | ||
126 | exchange :: Storage -> P2P () | ||
127 | exchange 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") )) | ||