diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-28 23:44:45 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-28 23:44:45 +0400 |
commit | 1d4b5f97c4323895d77aa197c3faf832c538aed9 (patch) | |
tree | ea48bdd9c43c1e177183d0469bfeaaa6d3f88d02 | |
parent | 6d0741ea0388dea6f123df19fa014d39123bf885 (diff) |
+ Default P2P.
-rw-r--r-- | bittorrent.cabal | 3 | ||||
-rw-r--r-- | examples/Main.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent.hs | 33 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange.hs | 5 |
4 files changed, 43 insertions, 1 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal index fbd045f4..7f1a9be5 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal | |||
@@ -101,7 +101,8 @@ library | |||
101 | 101 | ||
102 | -- System | 102 | -- System |
103 | , filepath >= 1 | 103 | , filepath >= 1 |
104 | , mmap >= 0.5.2 | 104 | , directory >= 1 |
105 | , mmap >= 0.5.2 | ||
105 | 106 | ||
106 | -- Misc | 107 | -- Misc |
107 | , data-default | 108 | , data-default |
diff --git a/examples/Main.hs b/examples/Main.hs index d0404405..fdf982d8 100644 --- a/examples/Main.hs +++ b/examples/Main.hs | |||
@@ -20,6 +20,9 @@ main = do | |||
20 | client <- newClient 100 [] | 20 | client <- newClient 100 [] |
21 | swarm <- newLeecher client torrent | 21 | swarm <- newLeecher client torrent |
22 | 22 | ||
23 | -- storage <- bindStorage swarm "/tmp/" | ||
24 | -- discover swarm $ exchange storage | ||
25 | |||
23 | ref <- liftIO $ newIORef 0 | 26 | ref <- liftIO $ newIORef 0 |
24 | discover swarm $ do | 27 | discover swarm $ do |
25 | forever $ do | 28 | forever $ do |
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") )) | ||
diff --git a/src/Network/BitTorrent/Exchange.hs b/src/Network/BitTorrent/Exchange.hs index 2eedc6bd..3d05f7fc 100644 --- a/src/Network/BitTorrent/Exchange.hs +++ b/src/Network/BitTorrent/Exchange.hs | |||
@@ -58,6 +58,7 @@ module Network.BitTorrent.Exchange | |||
58 | , Event(..) | 58 | , Event(..) |
59 | , awaitEvent | 59 | , awaitEvent |
60 | , yieldEvent | 60 | , yieldEvent |
61 | , handleEvent | ||
61 | 62 | ||
62 | -- * Exceptions | 63 | -- * Exceptions |
63 | , disconnect | 64 | , disconnect |
@@ -444,6 +445,10 @@ yieldEvent (Fragment blk) = do | |||
444 | then yieldMessage (Piece blk) | 445 | then yieldMessage (Piece blk) |
445 | else return () | 446 | else return () |
446 | 447 | ||
448 | |||
449 | handleEvent :: (Event -> P2P Event) -> P2P () | ||
450 | handleEvent action = awaitEvent >>= action >>= yieldEvent | ||
451 | |||
447 | --flushBroadcast :: P2P () | 452 | --flushBroadcast :: P2P () |
448 | --flushBroadcast = nextBroadcast >>= maybe (return ()) go | 453 | --flushBroadcast = nextBroadcast >>= maybe (return ()) go |
449 | -- where | 454 | -- where |