X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMain.hs;h=1b7458fe880c83463038abbc2de73be16f3d09ff;hb=a2c2a1a6865be7b4cd17fb72de635bb9385728b9;hp=415517f323612e40bd5ef45490f73cb674e9abab;hpb=64be4aa68a3ec03f318ff93af3ce9566795a993a;p=dead%2Fhtsn.git diff --git a/src/Main.hs b/src/Main.hs index 415517f..1b7458f 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -36,18 +36,17 @@ import ExitCodes ( exit_no_feed_hosts, exit_no_password, exit_no_username ) -import FeedHosts (FeedHosts(..)) import qualified OptionalConfiguration as OC ( OptionalConfiguration(..), from_rc ) -import Terminal (hPutRedLn, putGreenLn) +import Terminal (putGreenLn, report_error) +import TSN.FeedHosts (FeedHosts(..)) import TSN.Xml (parse_xmlfid, xml_prologue) -report_error :: String -> IO () -report_error = hPutRedLn stderr - - +-- | Receive a single line of text from a Handle, echoing it to stdout +-- in the process. +-- recv_line :: Handle -> IO String recv_line h = do line <- hGetLine h @@ -55,8 +54,15 @@ recv_line h = do return line +-- | Takes a Configuration, and an XML document (as a String). The XML +-- document is written to the output directory, as specified by the +-- Configuration. +-- +-- This can fail, but we don't purposefully throw any exceptions. If +-- something goes wrong, we would rather log it and keep going. +-- save_document :: Configuration -> String -> IO () -save_document cfg doc = do +save_document cfg doc = case maybe_path of Nothing -> report_error "ERROR: document missing XML_File_ID element." @@ -71,8 +77,11 @@ save_document cfg doc = do filename = fmap (++ ".xml") xmlfid maybe_path = fmap ((output_directory cfg) ) filename + -- | Loop forever, writing the buffer to file whenever a new XML --- prologue is seen. +-- prologue is seen. This is the low-level "loop forever" function +-- that we stay in as long as we are connected to one feed. +-- loop :: Configuration -> Handle -> [String] -> IO () loop !cfg !h !buffer = do line <- recv_line h @@ -87,7 +96,8 @@ loop !cfg !h !buffer = do save_document cfg document loop cfg h [line] -- empty the buffer before looping again else - loop cfg h (line : buffer) -- append line to the head of the buffer and loop + -- append line to the head of the buffer and loop + loop cfg h (line : buffer) log_in :: Configuration -> Handle -> IO () @@ -156,12 +166,14 @@ connect_and_loop cfg host = do -- | A wrapper around threadDelay which takes seconds instead of -- microseconds as its argument. +-- thread_sleep :: Int -> IO () thread_sleep seconds = do let microseconds = seconds * (10 ^ (6 :: Int)) threadDelay microseconds +-- | The entry point of the program. main :: IO () main = do rc_cfg <- OC.from_rc @@ -191,12 +203,21 @@ main = do -- set in either the config file or on the command-line. let cfg = (def :: Configuration) `merge_optional` opt_config + -- This may be superstition (and I believe stderr is unbuffered), + -- but it can't hurt. hSetBuffering stderr NoBuffering hSetBuffering stdout NoBuffering + -- Begin connecting to our feed hosts, starting with the first one. round_robin cfg 0 where + -- | This is the top-level "loop forever" function. If an + -- exception is thrown, it will propagate up to this point, where + -- it will be logged and ignored in style. + -- + -- Afterwards, we recurse (call ourself) again to loop more forevers. + -- round_robin :: Configuration -> Int -> IO () round_robin cfg feed_host_idx = do let hosts = get_feed_hosts $ feed_hosts cfg