]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Tracker.hs
more hlint cleanups
[functorrent.git] / src / FuncTorrent / Tracker.hs
index 8f6a5cc379dc5fc46a45c395899bab6f985e692a..b9e977acff1bea112cfc1090ac5f93c85b0cc6a9 100644 (file)
@@ -29,16 +29,15 @@ import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan)
 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.Peer (Peer)
+import FuncTorrent.PeerMsgs (Peer)
 
 type MsgChannel = Chan TrackerMsg
 
@@ -48,23 +47,20 @@ newTracker = newChan
 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
-  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 $ fn 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