-
Notifications
You must be signed in to change notification settings - Fork 0
/
Torrent.hs
54 lines (46 loc) · 1.2 KB
/
Torrent.hs
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
50
51
52
53
54
module Torrent where
import Control.Monad
import Control.Monad.IO.Class
import Control.Concurrent
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy.Char8 as BC8
import qualified Data.Map as M
import State
import TorrentFile
import Tracker
import Types
mkTorrent :: FilePath -> IO Torrent
mkTorrent fn = do
t <- readTorrent fn
kp <- newTVarIO []
--pid <- genPeerID
return Torrent {
torrentFile = t,
knownPeers = kp,
trackerPokerThread = undefined
}
addTorrent :: ByteString -> NPT ()
addTorrent fn = do
t_ <- liftIO $ mkTorrent $ BC8.unpack fn
tp <- forkE $ trackerPoker t_
s <- ask
let t = t_ {
trackerPokerThread = tp
}
liftIO $ atomically $ do
tm <- readTVar $ torrents s
writeTVar (torrents s) $ M.insert (infoHash $ torrentFile t) t tm
addPeer :: InfoHash -> Address -> NPT ()
addPeer ih addr = do
s <- ask
tm <- liftIO $ atomically $ readTVar $ torrents s
let Just t = M.lookup ih tm
liftIO $ atomically $ do
peers <- readTVar $ knownPeers t
writeTVar (knownPeers t) $ Peer addr : peers
delTorrent :: InfoHash -> NPT ()
delTorrent ih = undefined
trackerPoker :: Torrent -> NPT ()
trackerPoker t = forever $ do
pokeTrackers t
liftIO $ threadDelay 600000000