1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
module Network.BitTorrent.Client.Swarm
( Swarm
, newLeecher
, askPeers
) where
import Data.Default
import Network
import Data.Torrent
import Data.Torrent.InfoHash
import Network.BitTorrent.Core
import Network.BitTorrent.Tracker.Message
import Network.BitTorrent.Tracker.RPC as RPC
data Swarm = Swarm
{ swarmTopic :: InfoHash
, thisPeerId :: PeerId
, listenerPort :: PortNumber
, trackerConn :: Tracker
-- , infoDict ::
}
newLeecher :: PeerId -> PortNumber -> Torrent -> IO Swarm
newLeecher pid port Torrent {..} = do
tracker <- connect undefined
return Swarm
{ swarmTopic = idInfoHash tInfoDict
, thisPeerId = pid
, listenerPort = port
, trackerConn = tracker
}
getAnnounceQuery :: Swarm -> AnnounceQuery
getAnnounceQuery Swarm {..} = AnnounceQuery
{ reqInfoHash = swarmTopic
, reqPeerId = thisPeerId
, reqPort = listenerPort
, reqProgress = def
, reqIP = Nothing
, reqNumWant = Nothing
, reqEvent = Nothing
}
askPeers :: Swarm -> IO [PeerAddr IP]
askPeers s @ Swarm {..} = do
AnnounceInfo {..} <- RPC.announce (getAnnounceQuery s) trackerConn
return (getPeerList respPeers)
|