import Prelude hiding (log, length, readFile, getContents)
-import Control.Monad.Reader(runReaderT)
import Data.ByteString.Char8 (ByteString, getContents, readFile, unpack)
import System.Environment (getArgs)
import System.Exit (exitSuccess)
import System.Directory (doesFileExist)
+import System.Random (getStdGen, randomRs)
import FuncTorrent.Logger (initLogger, logMessage, logStop)
import FuncTorrent.Metainfo (Info(..), Metainfo(..), torrentToMetainfo)
logError :: String -> (String -> IO ()) -> IO ()
logError e logMsg = logMsg $ "parse error: \n" ++ e
-peerId :: String
-peerId = "-HS0001-*-*-20150215"
-
exit :: IO ByteString
exit = exitSuccess
else error "file does not exist"
parse _ = exit
+-- peer id is exactly 20 bytes long.
+-- peer id starts with '-', followed by 2 char client id'
+-- followed by 4 ascii digits for version number, followed by
+-- a '-'. Rest are random digits to fill the 20 bytes.
+mkPeerID :: IO String
+mkPeerID = do
+ stdgen <- getStdGen
+ let digits = randomRs (0, 9) stdgen :: [Integer]
+ return $ "-HS9001-" ++ (concatMap show $ take (20 - 8) digits)
+
main :: IO ()
main = do
args <- getArgs
logR <- initLogger
+ peerId <- mkPeerID
let log = logMessage logR
log "Starting up functorrent"
log $ "Parsing arguments " ++ concat args
log "Trying to fetch peers"
log $ "Trackers: " ++ head (announceList m)
- trackerResp <- runReaderT (getTrackerResponse peerId) m
+ trackerResp <- getTrackerResponse peerId m
case trackerResp of
Left e -> log $ "Error" ++ unpack e
Right peerList -> do