X-Git-Url: https://git.rkrishnan.org/?a=blobdiff_plain;f=src%2Fmain%2FMain.hs;h=dff915f4225eb6d9b61ce8a5a8f0fe3dd88d8747;hb=32dbac49469e50dae928b399bcddef5030339771;hp=b82d27a5f859acbc60c5fdb75d703cda80ab068a;hpb=721aeee745cc308014b80594b4c6b7b403d4ef1f;p=functorrent.git diff --git a/src/main/Main.hs b/src/main/Main.hs index b82d27a..dff915f 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -3,11 +3,11 @@ module Main where 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) @@ -17,9 +17,6 @@ import FuncTorrent.Tracker (peers, getTrackerResponse) logError :: String -> (String -> IO ()) -> IO () logError e logMsg = logMsg $ "parse error: \n" ++ e -peerId :: String -peerId = "-HS0001-*-*-20150215" - exit :: IO ByteString exit = exitSuccess @@ -35,10 +32,21 @@ parse [a] = do 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 @@ -51,7 +59,7 @@ main = do 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