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
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."
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
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 ()
-- | 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
-- 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