sendGetRequest :: String -> [(String, ByteString)] -> IO ByteString
sendGetRequest url args = simpleHTTP (defaultGETRequest_ url') >>= getResponseBody
- where url' = case parseURI $ unpack $ concat [pack url, "?", qstr] of
+ where url' = case parseURI url'' of
Just x -> x
- _ -> error "Bad tracker URL"
+ _ -> error $ "Bad tracker URL: " ++ (show url'')
qstr = mkParams args
+ url'' = unpack $ concat [pack url, "?", qstr]
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
getTrackerType :: String -> TrackerProtocol
getTrackerType url | "http://" `isPrefixOf` url = Http
| "udp://" `isPrefixOf` url = Udp
- | otherwise = UnknownProtocol
+ | otherwise = UnknownProtocol
msgHandler :: MsgChannel -> StateT TState IO ()
import Control.Concurrent (threadDelay)
import Control.Concurrent.MVar (readMVar, putMVar, isEmptyMVar, swapMVar)
-import Control.Monad (forever)
+import Control.Monad (forever, void)
import qualified Data.ByteString.Base16 as B16 (encode)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 as BC (pack, unpack, splitAt)
return () -- $ pack (show e)
Right trackerInfo ->
case parseTrackerResponse trackerInfo of
- Left e -> return () -- e
+ Left e -> return ()
Right tresp -> do
- _ <- threadDelay $ fromIntegral (interval tresp)
ps <- isEmptyMVar $ connectedPeers tstate
if ps
- then do
- _ <- putMVar (connectedPeers tstate) (peers tresp)
- return ()
- else do
- _ <- swapMVar (connectedPeers tstate) (peers tresp)
- return ()
+ then
+ putMVar (connectedPeers tstate) (peers tresp)
+ else
+ void $ swapMVar (connectedPeers tstate) (peers tresp)
+ threadDelay $ fromIntegral (interval tresp)
parseTrackerResponse :: BVal -> Either ByteString TrackerResponse
parseTrackerResponse resp =
flip runReaderT handle $ do
t1 <- connectRequest
cid <- connectResponse t1
- liftIO $ print "connected: connect id"
t2 <- announceRequest cid infohash peerId (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral sport)
- liftIO $ print "waiting for announce response"
stats <- announceResponse t2
liftIO $ print stats
+-- _ <- threadDelay $