-> String -> [String] -> Integer -> IO ()
runTracker msgChannel fsChan infohash port peerId announceList sz = do
ps <- newEmptyMVar
- let initialTState = TState { currentState = None
- , connectedPeers = ps
- , left = sz }
- turl = head announceList
- host = getHostname turl
- case getTrackerType turl of
- Http -> do
- _ <- forkIO $ HT.trackerLoop turl port peerId infohash fsChan initialTState
- runStateT (msgHandler msgChannel) initialTState
- return ()
- Udp -> do
- _ <- forkIO $ UT.trackerLoop turl (fromIntegral port) peerId infohash fsChan initialTState
- runStateT (msgHandler msgChannel) initialTState
- return ()
- _ ->
- error "Tracker Protocol unimplemented"
+ forkIO $ (getTrackerLoopFn turl) turl port peerId infohash fsChan (initialTState ps)
+ runStateT (msgHandler msgChannel) (initialTState ps)
+ return ()
+ where getTrackerLoopFn turl =
+ case getTrackerType turl of
+ Http -> HT.trackerLoop
+ Udp -> UT.trackerLoop
+ _ -> error "Tracker Protocol unimplemented"
+ initialTState ps' = TState { currentState = None
+ , connectedPeers = ps'
+ , left = sz }
+ turl = head announceList
getTrackerType :: String -> TrackerProtocol
getTrackerType url | "http://" `isPrefixOf` url = Http
import qualified Data.ByteString.Char8 as BC
import Data.ByteString.Lazy (fromStrict, toStrict)
import Data.Word (Word16, Word32, Word64)
+import Network (PortNumber)
import Network.Socket (Socket, Family( AF_INET ), SocketType( Datagram ), defaultProtocol, SockAddr(..), socket, close, getAddrInfo, addrAddress, SockAddr(..))
import Network.Socket.ByteString (sendTo, recvFrom)
import System.Random (randomIO)
closeSession :: UDPTrackerHandle -> IO ()
closeSession (UDPTrackerHandle s _ _) = close s
-trackerLoop :: String -> Port -> String -> ByteString -> FS.MsgChannel -> TState -> IO ()
+trackerLoop :: String -> PortNumber -> String -> ByteString -> FS.MsgChannel -> TState -> IO ()
trackerLoop url sport peerId infohash fschan tstate = forever $ do
st <- readMVar <$> FS.getStats fschan
up <- fmap FS.bytesRead st