diff options
Diffstat (limited to 'src/Network/BitTorrent/Client')
-rw-r--r-- | src/Network/BitTorrent/Client/Swarm.hs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Network/BitTorrent/Client/Swarm.hs b/src/Network/BitTorrent/Client/Swarm.hs new file mode 100644 index 00000000..a9dca048 --- /dev/null +++ b/src/Network/BitTorrent/Client/Swarm.hs | |||
@@ -0,0 +1,49 @@ | |||
1 | module Network.BitTorrent.Client.Swarm | ||
2 | ( Swarm | ||
3 | , newLeecher | ||
4 | , askPeers | ||
5 | ) where | ||
6 | |||
7 | import Data.Default | ||
8 | import Network | ||
9 | |||
10 | import Data.Torrent | ||
11 | import Data.Torrent.InfoHash | ||
12 | import Network.BitTorrent.Core | ||
13 | import Network.BitTorrent.Tracker.Message | ||
14 | import Network.BitTorrent.Tracker.RPC as RPC | ||
15 | |||
16 | |||
17 | data Swarm = Swarm | ||
18 | { swarmTopic :: InfoHash | ||
19 | , thisPeerId :: PeerId | ||
20 | , listenerPort :: PortNumber | ||
21 | , trackerConn :: Tracker | ||
22 | -- , infoDict :: | ||
23 | } | ||
24 | |||
25 | newLeecher :: PeerId -> PortNumber -> Torrent -> IO Swarm | ||
26 | newLeecher pid port Torrent {..} = do | ||
27 | tracker <- connect tAnnounce | ||
28 | return Swarm | ||
29 | { swarmTopic = idInfoHash tInfoDict | ||
30 | , thisPeerId = pid | ||
31 | , listenerPort = port | ||
32 | , trackerConn = tracker | ||
33 | } | ||
34 | |||
35 | getAnnounceQuery :: Swarm -> AnnounceQuery | ||
36 | getAnnounceQuery Swarm {..} = AnnounceQuery | ||
37 | { reqInfoHash = swarmTopic | ||
38 | , reqPeerId = thisPeerId | ||
39 | , reqPort = listenerPort | ||
40 | , reqProgress = def | ||
41 | , reqIP = Nothing | ||
42 | , reqNumWant = Nothing | ||
43 | , reqEvent = Nothing | ||
44 | } | ||
45 | |||
46 | askPeers :: Swarm -> IO [PeerAddr] | ||
47 | askPeers s @ Swarm {..} = do | ||
48 | AnnounceInfo {..} <- RPC.announce (getAnnounceQuery s) trackerConn | ||
49 | return (getPeerList respPeers) | ||