From: Ramakrishnan Muthukrishnan Date: Sun, 4 Jun 2017 07:08:13 +0000 (+0530) Subject: print and error and exit if called without arguments X-Git-Url: https://git.rkrishnan.org/pf/content/en/frontends/webapi.rst?a=commitdiff_plain;h=e92065b9e3a4f35f6a74aaa1d138f3032ead6b52;p=functorrent.git print and error and exit if called without arguments --- diff --git a/run b/run new file mode 100755 index 0000000..4e6ac2f --- /dev/null +++ b/run @@ -0,0 +1,4 @@ +#!/bin/sh + +./dist-newstyle/build/functorrent-0.2.0/build/FuncTorrent/functorrent $* + diff --git a/src/main/Main.hs b/src/main/Main.hs index 71c9841..8fccebe 100644 --- a/src/main/Main.hs +++ b/src/main/Main.hs @@ -45,8 +45,8 @@ logError e logMsg = logMsg $ "parse error: \n" ++ e exit :: IO ByteString exit = exitSuccess -usage :: IO () -usage = putStrLn "usage: functorrent torrent-file" +usage :: String +usage = "usage: functorrent torrent-file" parse :: [String] -> IO ByteString parse [] = getContents @@ -73,40 +73,44 @@ main = do logR <- initLogger peerId <- mkPeerID let log = logMessage logR - log "Starting up functorrent" - log $ "Parsing arguments " ++ concat args - torrentStr <- parse args - case torrentToMetainfo torrentStr of - Left e -> logError e log - Right m -> do - -- if we had downloaded the file before (partly or completely) - -- then we should check the current directory for the existence - -- of the file and then update the map of each piece' availability. - -- This can be done by reading each piece and verifying the checksum. - -- If the checksum does not match, we don't have that piece. - let filePath = name (info m) -- really this is just the file name, not file path - fileLen = lengthInBytes (info m) - pieceHash = pieces (info m) - pLen = pieceLength (info m) - infohash = infoHash m - defaultPieceMap = initPieceMap pieceHash fileLen pLen - log $ "create FS msg channel" - fsMsgChannel <- FS.createMsgChannel - log $ "Downloading file : " ++ filePath - pieceMap <- FS.pieceMapFromFile filePath fileLen defaultPieceMap - log $ "start filesystem manager thread" - fsTid <- forkIO $ withFile filePath ReadWriteMode (FS.run pieceMap fsMsgChannel) - log $ "starting server" - (serverSock, (PortNumber portnum)) <- Server.start - log $ "server started on " ++ show portnum - log "Trying to fetch peers" - _ <- forkIO $ Server.run serverSock peerId m pieceMap fsMsgChannel - log $ "Trackers: " ++ head (announceList m) - trackerMsgChan <- newTracker - _ <- forkIO $ runTracker trackerMsgChan fsMsgChannel infohash portnum peerId (announceList m) fileLen - ps <- getConnectedPeers trackerMsgChan - log $ "Peers List : " ++ (show ps) - let p1 = head ps - handlePeerMsgs p1 peerId m pieceMap True fsMsgChannel - logStop logR - killThread fsTid + case args of + [] -> do + log usage + _ -> do + log "Starting up functorrent" + log $ "Parsing arguments " ++ concat args + torrentStr <- parse args + case torrentToMetainfo torrentStr of + Left e -> logError e log + Right m -> do + -- if we had downloaded the file before (partly or completely) + -- then we should check the current directory for the existence + -- of the file and then update the map of each piece' availability. + -- This can be done by reading each piece and verifying the checksum. + -- If the checksum does not match, we don't have that piece. + let filePath = name (info m) -- really this is just the file name, not file path + fileLen = lengthInBytes (info m) + pieceHash = pieces (info m) + pLen = pieceLength (info m) + infohash = infoHash m + defaultPieceMap = initPieceMap pieceHash fileLen pLen + log $ "create FS msg channel" + fsMsgChannel <- FS.createMsgChannel + log $ "Downloading file : " ++ filePath + pieceMap <- FS.pieceMapFromFile filePath fileLen defaultPieceMap + log $ "start filesystem manager thread" + fsTid <- forkIO $ withFile filePath ReadWriteMode (FS.run pieceMap fsMsgChannel) + log $ "starting server" + (serverSock, (PortNumber portnum)) <- Server.start + log $ "server started on " ++ show portnum + log "Trying to fetch peers" + _ <- forkIO $ Server.run serverSock peerId m pieceMap fsMsgChannel + log $ "Trackers: " ++ head (announceList m) + trackerMsgChan <- newTracker + _ <- forkIO $ runTracker trackerMsgChan fsMsgChannel infohash portnum peerId (announceList m) fileLen + ps <- getConnectedPeers trackerMsgChan + log $ "Peers List : " ++ (show ps) + let p1 = head ps + handlePeerMsgs p1 peerId m pieceMap True fsMsgChannel + logStop logR + killThread fsTid