import Prelude hiding (lookup, splitAt)
+import System.IO (Handle)
import Control.Applicative (liftA2)
import Control.Concurrent (threadDelay)
import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, readMVar, putMVar)
-import Data.Binary (Binary(..))
+import Data.Binary (Binary(..), encode, decode)
import Data.Binary.Put (putWord16be, putWord32be, putWord64be, putByteString)
import Data.Binary.Get (Get, isEmpty, getByteString, getWord16be, getWord32be)
-import Data.ByteString (ByteString)
+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.Map as M (lookup)
import Network.HTTP.Base (urlEncode)
import qualified Data.ByteString.Base16 as B16 (encode)
-import FuncTorrent.Bencode (BVal(..), decode)
+import FuncTorrent.Bencode (BVal(..))
+import qualified FuncTorrent.Bencode as Benc
import FuncTorrent.Metainfo (Info(..), Metainfo(..))
import FuncTorrent.Network (sendGetRequest)
import FuncTorrent.Peer (Peer(..))
up <- readMVar $ uploaded st
down <- readMVar $ downloaded st
resp <- sendGetRequest (head . announceList $ m) $ mkArgs port peerId up down m
- case decode resp of
+ case Benc.decode resp of
Left e -> return $ pack (show e)
Right trackerInfo ->
case mkTrackerResponse trackerInfo of
_ <- putMVar (connectedPeers st) (peers tresp)
trackerLoop port peerId m st
+-- udp tracker
+getResponse :: Handle -> IO UDPResponse
+getResponse h = do
+ -- connect packet is 16 bytes long
+ -- announce packet is atleast 20 bytes long
+ bs <- hGet h (16*1024)
+ return $ decode $ fromStrict bs
+
+sendRequest :: Handle -> UDPRequest -> IO ()
+sendRequest h req = hPut h bsReq
+ where bsReq = toStrict $ encode req