]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Tracker.hs
refactor: remove peerid from Peer datatype
[functorrent.git] / src / FuncTorrent / Tracker.hs
index 5e059f75c0dda0fb4896ec3941a95869e3013d9f..8090feb99ea868eb328dc1811332b550984e277c 100644 (file)
@@ -36,42 +36,15 @@ 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)
+import FuncTorrent.Utils (Port, toPort, getHostname)
 import qualified FuncTorrent.FileSystem as FS (MsgChannel)
-import FuncTorrent.Peer (Peer)
+import FuncTorrent.PeerMsgs (Peer)
 
 type MsgChannel = Chan TrackerMsg
 
-data TrackerUrl = TrackerUrl { protocol :: TrackerProtocol
-                             , host :: String
-                             , port :: Port
-                             , path :: String
-                             }
-
 newTracker :: IO MsgChannel
 newTracker = newChan
 
-parseUrl :: String -> TrackerUrl
-parseUrl url = TrackerUrl proto host port path
-  where proto = getTrackerType url
-        host = getHostname url
-        port = getPort url
-        path = getPath url
-
-getTrackerType :: String -> TrackerProtocol
-getTrackerType url | isPrefixOf "http://" url = Http
-                   | isPrefixOf "udp://" url  = Udp
-                   | otherwise                = UnknownProtocol
-
-getHostname :: String -> String
-getHostname url = takeWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url
-
-getPort :: String -> Port
-getPort url = toPort . pack $ takeWhile (/= '/') $ drop 1 $ dropWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url
-
-getPath :: String -> String
-getPath url = dropWhile (/= '/') $ dropWhile (/= ':') $ drop 1 $ dropWhile (/= ':') url
-
 runTracker :: MsgChannel -> FS.MsgChannel -> ByteString -> PortNumber
            -> String -> [String] -> Integer -> IO ()
 runTracker msgChannel fsChan infohash port peerId announceList sz = do
@@ -83,15 +56,22 @@ runTracker msgChannel fsChan infohash port peerId announceList sz = do
       host = getHostname turl
   case getTrackerType turl of
     Http -> do
-      _ <- forkIO $ HT.trackerLoop host port peerId infohash fsChan initialTState
+      _ <- forkIO $ HT.trackerLoop turl port peerId infohash fsChan initialTState
       runStateT (msgHandler msgChannel) initialTState
       return ()
     Udp -> do
-      _ <- forkIO $ UT.trackerLoop host (fromIntegral port) peerId infohash fsChan initialTState
+      _ <- forkIO $ UT.trackerLoop turl (fromIntegral port) peerId infohash fsChan initialTState
+      runStateT (msgHandler msgChannel) initialTState
       return ()
     _ ->
       error "Tracker Protocol unimplemented"
 
+getTrackerType :: String -> TrackerProtocol
+getTrackerType url | "http://" `isPrefixOf` url = Http
+                   | "udp://" `isPrefixOf` url  = Udp
+                   | otherwise                  = UnknownProtocol
+
+
 msgHandler :: MsgChannel -> StateT TState IO ()
 msgHandler c = forever $ do
   st <- get
@@ -102,14 +82,13 @@ msgHandler c = forever $ do
       recvMsg = readChan c
       sendResponse msg peers =
         case msg of
-          GetConnectedPeersMsg var -> do
+          GetConnectedPeersMsg var ->
             putMVar var peers
-          _ -> do
+          _ ->
             putStrLn "Unhandled Tracker Msg"
 
 getConnectedPeers :: MsgChannel -> IO [Peer]
 getConnectedPeers c = do
   v <- newEmptyMVar
   writeChan c (GetConnectedPeersMsg v)
-  ps <- readMVar v
-  return ps
+  readMVar v