+{-# LANGUAGE OverloadedStrings #-}
module Main where
-import Prelude hiding (length, readFile)
+import Prelude hiding (length, readFile, writeFile)
+import Data.ByteString.Char8 (ByteString, length, readFile, writeFile, length)
+import System.Environment (getArgs)
+import System.Exit (exitSuccess)
+import Text.ParserCombinators.Parsec (ParseError)
import Bencode (decode, BVal(..))
-import Data.ByteString.Char8 as BC (ByteString, pack, length, readFile, length)
-import Data.Functor ((<$>))
-import Metainfo (announce, lengthInBytes, mkMetaInfo, info)
+import Logger (initLogger, logMessage, logStop)
+import Metainfo (announce, lengthInBytes, mkMetaInfo, info, name)
import Peer (getPeers, getPeerResponse, handShakeMsg)
-import System.Environment (getArgs)
-import System.Exit (exitSuccess)
import Tracker (connect, prepareRequest)
-import Text.ParserCombinators.Parsec (ParseError)
-import Logger
logError :: ParseError -> (String -> IO ()) -> IO ()
logError e logMsg = logMsg $ "parse error: \n" ++ show e
let len = lengthInBytes $ info m
(Bdict d') = d
-
+
logMsg "Trying to fetch peers: "
+ body <- connect (announce m) (prepareRequest d' peerId len)
+
+ -- TODO: Write to ~/.functorrent/caches
+ writeFile (name (info m) ++ ".cache") body
- body <- pack <$> connect (announce m) (prepareRequest d' peerId len)
-
let peerResponse = show $ getPeers $ getPeerResponse body
logMsg $ "Peers List : " ++ peerResponse
-
+
let hsMsgLen = show $ length $ handShakeMsg d' peerId
logMsg $ "Hand-shake message length : " ++ hsMsgLen
import Data.ByteString.Char8 (ByteString, pack, unpack)
import Data.Char (chr)
import Data.List (intercalate)
+import Data.Maybe (fromJust)
import Data.Map as M (Map, (!))
-import Network.HTTP (simpleHTTP, getRequest, getResponseBody)
+import Network.HTTP (simpleHTTP, defaultGETRequest_, getResponseBody)
import Network.HTTP.Base (urlEncode)
+import Network.URI (parseURI)
import Utils (splitN)
import qualified Data.ByteString.Base16 as B16 (encode)
("event", "started")]
in intercalate "&" [f ++ "=" ++ s | (f,s) <- p]
-connect :: Url -> String -> IO String
-connect baseurl qstr = let url = baseurl ++ "?" ++ qstr
- in simpleHTTP (getRequest url) >>=
- getResponseBody
+connect :: Url -> String -> IO ByteString
+connect baseurl qstr = simpleHTTP (defaultGETRequest_ url) >>= getResponseBody
+ where url = fromJust . parseURI $ (baseurl ++ "?" ++ qstr)