import Control.Concurrent.MVar (newEmptyMVar, putMVar, readMVar)
import Control.Monad.State (StateT, liftIO, get, runStateT)
import Control.Monad (forever)
-import Data.ByteString.Char8 (ByteString, pack, unpack)
+import Data.ByteString.Char8 (ByteString)
import Data.List (isPrefixOf)
import Network (PortNumber)
import qualified FuncTorrent.Tracker.Http as HT (trackerLoop)
import qualified FuncTorrent.Tracker.Udp as UT (trackerLoop)
import FuncTorrent.Tracker.Types (TState(..), TrackerEventState(..), TrackerProtocol(..), TrackerMsg(..))
-import FuncTorrent.Utils (Port, toPort, getHostname)
import qualified FuncTorrent.FileSystem as FS (MsgChannel)
import FuncTorrent.PeerMsgs (Peer)
runTracker :: MsgChannel -> FS.MsgChannel -> ByteString -> PortNumber
-> String -> [String] -> Integer -> IO ()
runTracker msgChannel fsChan infohash port peerId announceList sz = do
+ let fn = getTrackerLoopFn turl
ps <- newEmptyMVar
- forkIO $ (getTrackerLoopFn turl) turl port peerId infohash fsChan (initialTState ps)
- runStateT (msgHandler msgChannel) (initialTState ps)
+ _ <- forkIO $ fn turl port peerId infohash fsChan (initialTState ps)
+ _ <- runStateT (msgHandler msgChannel) (initialTState ps)
return ()
- where getTrackerLoopFn turl =
- case getTrackerType turl of
+ where getTrackerLoopFn turl' =
+ case getTrackerType turl' of
Http -> HT.trackerLoop
Udp -> UT.trackerLoop
_ -> error "Tracker Protocol unimplemented"
putWord32be 0
putWord32be 10
putWord16be $ fromIntegral port
- put (ScrapeReq _ _ _) = undefined
+ put ScrapeReq {} = undefined
get = undefined
instance Binary UDPResponse where
ip <- toIP <$> getByteString 4
port <- toPort <$> getByteString 2
ipportpairs <- getIPPortPairs
- return $ (Peer ip port) : ipportpairs
+ return $ Peer ip port : ipportpairs
startSession :: String -> Port -> IO UDPTrackerHandle
startSession host port = do
let (SockAddrInet p ip) = addrAddress $ head addrinfos
putStrLn "connected to tracker"
return UDPTrackerHandle { sock = s
- , addr = (SockAddrInet (fromIntegral port) ip) }
+ , addr = SockAddrInet (fromIntegral port) ip }
closeSession :: UDPTrackerHandle -> IO ()
closeSession (UDPTrackerHandle s _) = close s
t1 <- connectRequest
cid <- connectResponse t1
t2 <- announceRequest cid infohash peerId (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral sport)
- stats <- announceResponse t2
- return stats
+ announceResponse t2