X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fhtsn.git;a=blobdiff_plain;f=src%2FMain.hs;h=1b7458fe880c83463038abbc2de73be16f3d09ff;hp=374981248fd9bf2b17905175949914d89cea70e4;hb=a2c2a1a6865be7b4cd17fb72de635bb9385728b9;hpb=95e23e65db31cf51c9f207a6b447da19920ee1a1 diff --git a/src/Main.hs b/src/Main.hs index 3749812..1b7458f 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -44,6 +44,9 @@ import TSN.FeedHosts (FeedHosts(..)) import TSN.Xml (parse_xmlfid, xml_prologue) +-- | 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 @@ -51,6 +54,13 @@ 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 = case maybe_path of @@ -67,8 +77,11 @@ save_document cfg doc = 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 @@ -83,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 () @@ -152,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 @@ -187,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