summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Client/Swarm.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Client/Swarm.hs')
-rw-r--r--src/Network/BitTorrent/Client/Swarm.hs49
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 @@
1module Network.BitTorrent.Client.Swarm
2 ( Swarm
3 , newLeecher
4 , askPeers
5 ) where
6
7import Data.Default
8import Network
9
10import Data.Torrent
11import Data.Torrent.InfoHash
12import Network.BitTorrent.Core
13import Network.BitTorrent.Tracker.Message
14import Network.BitTorrent.Tracker.RPC as RPC
15
16
17data Swarm = Swarm
18 { swarmTopic :: InfoHash
19 , thisPeerId :: PeerId
20 , listenerPort :: PortNumber
21 , trackerConn :: Tracker
22-- , infoDict ::
23 }
24
25newLeecher :: PeerId -> PortNumber -> Torrent -> IO Swarm
26newLeecher 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
35getAnnounceQuery :: Swarm -> AnnounceQuery
36getAnnounceQuery 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
46askPeers :: Swarm -> IO [PeerAddr]
47askPeers s @ Swarm {..} = do
48 AnnounceInfo {..} <- RPC.announce (getAnnounceQuery s) trackerConn
49 return (getPeerList respPeers)