]> git.rkrishnan.org Git - functorrent.git/commitdiff
Tracker: identify tracker protocol from the tracker server url
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Mon, 5 Oct 2015 01:36:00 +0000 (07:06 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Mon, 5 Oct 2015 01:36:00 +0000 (07:06 +0530)
src/FuncTorrent/Tracker.hs

index 6ae0281754b7fef50bb3af629e8d47f946434031..e0752e9e0a4f196a11cb289783e72ea8f7e2dc60 100644 (file)
@@ -18,7 +18,7 @@ import Data.ByteString (ByteString, hGet, hPut)
 import Data.ByteString.Char8 as BC (pack, unpack, splitAt)
 import Data.ByteString.Lazy (fromStrict, toStrict)
 import Data.Char (chr)
-import Data.List (intercalate)
+import Data.List (intercalate, isPrefixOf)
 import Data.Map as M (lookup)
 import Network (PortNumber)
 import Network.HTTP.Base (urlEncode)
@@ -31,6 +31,12 @@ import FuncTorrent.Network (sendGetRequest)
 import FuncTorrent.Peer (Peer(..))
 import FuncTorrent.Utils (splitN)
 
+
+data TrackerProtocol = Http
+                     | Udp
+                     | UnknownProtocol
+                     deriving (Show)
+
 -- | Tracker response
 data TrackerResponse = TrackerResponse {
   interval :: Integer
@@ -243,3 +249,8 @@ getResponse h = do
 sendRequest :: Handle -> UDPRequest -> IO ()
 sendRequest h req = hPut h bsReq
   where bsReq = toStrict $ encode req
+
+getTrackerType :: String -> TrackerProtocol
+getTrackerType url | isPrefixOf "http://" url = Http
+                   | isPrefixOf "udp://" url  = Udp
+                   | otherwise                = UnknownProtocol