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)
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
-
-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
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
return ()
_ ->
error "Tracker Protocol unimplemented"
import qualified Data.ByteString.Char8 as BC
import Data.ByteString.Lazy (fromStrict, toStrict)
import Data.Word (Word16, Word32, Word64)
-import Network.Socket (Socket, Family( AF_INET ), SocketType( Datagram ), defaultProtocol, SockAddr(..), socket, inet_addr, close, getAddrInfo, addrAddress, SockAddr(..))
+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)
-import FuncTorrent.Tracker.Types (TrackerEventState(..))
-import FuncTorrent.Utils (IP, Port, toIP, toPort)
+import FuncTorrent.Tracker.Types (TrackerEventState(..), TState(..))
+import FuncTorrent.Utils (IP, Port, toIP, toPort, getHostname, getPort)
import qualified FuncTorrent.FileSystem as FS (MsgChannel, Stats(..), getStats)
-import FuncTorrent.Tracker.Types(TState(..))
-- UDP tracker: http://bittorrent.org/beps/bep_0015.html
data Action = Connect
connectResponse tid = do
h <- ask
resp <- liftIO $ recvResponse h
+ liftIO $ print resp
-- check if nbytes is at least 16 bytes long
case resp of
(ConnectResp tidr cid) ->
s <- socket AF_INET Datagram defaultProtocol
addrinfos <- getAddrInfo Nothing (Just host) (Just (show port))
let (SockAddrInet p ip) = addrAddress $ head addrinfos
- hostAddr <- inet_addr (show ip)
putStrLn "connected to tracker"
return $ UDPTrackerHandle { sock = s
- , addr = (SockAddrInet (fromIntegral port) hostAddr) }
+ , addr = (SockAddrInet (fromIntegral port) ip) }
closeSession :: UDPTrackerHandle -> IO ()
closeSession (UDPTrackerHandle s _ _) = close s
trackerLoop :: String -> Port -> String -> ByteString -> FS.MsgChannel -> TState -> IO ()
-trackerLoop host port peerId infohash fschan tstate = do
+trackerLoop url sport peerId infohash fschan tstate = do
st' <- FS.getStats fschan
st <- readMVar st'
let up = FS.bytesRead st
down = FS.bytesWritten st
- handle <- startSession host 2710
+ port = getPort url
+ host = getHostname url
+ putStrLn $ "host = " ++ (show host) ++ " port= " ++ (show port)
+ handle <- startSession host port
flip runReaderT handle $ do
t1 <- connectRequest
cid <- connectResponse t1
- t2 <- announceRequest cid infohash (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral port)
+ liftIO $ print cid
+ t2 <- announceRequest cid infohash (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral sport)
stats <- announceResponse t2
liftIO $ print stats