1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
7 import Control.Concurrent ( threadDelay )
8 import Control.Exception.Base ( bracket )
9 import Control.Monad ( when )
10 import Data.List ( isPrefixOf )
11 import Data.Maybe ( isNothing )
12 import Data.Monoid ( (<>) )
16 import System.Console.CmdArgs ( def )
17 import System.Directory ( doesFileExist )
18 import System.Exit ( ExitCode(..), exitWith )
19 import System.FilePath ( (</>) )
21 BufferMode (NoBuffering),
30 import System.IO.Error ( catchIOError )
31 import System.Timeout ( timeout )
33 import CommandLine ( get_args )
34 import Configuration ( Configuration(..), merge_optional )
46 import qualified OptionalConfiguration as OC (
47 OptionalConfiguration(..),
55 import TSN.FeedHosts ( FeedHosts(..) )
56 import TSN.Xml ( parse_xmlfid )
57 import Unix ( full_daemonize )
59 -- | Display and log debug information. WARNING! This does not
60 -- automatically append a newline. The output is displayed/logged
61 -- as-is, for, you know, debug purposes.
62 report_debug :: String -> IO ()
68 -- | Display and log an error condition. This will prefix the error
69 -- with "ERROR: " when displaying (but not logging) it so that it
72 report_error :: String -> IO ()
74 display_error $ "ERROR: " ++ s
78 -- | Display and log an informational (status) message.
79 report_info :: String -> IO ()
85 -- | Display and log a warning. This will prefix the warning with
86 -- "WARNING: " when displaying (but not logging) it so that it
89 report_warning :: String -> IO ()
91 display_warning $ "WARNING: " ++ s
95 -- | Receive a single line of text from a Handle, and send it to the
98 recv_line :: Handle -> IO String
101 report_debug (line ++ "\n")
105 -- | Takes a Configuration, and an XML document (as a String). The XML
106 -- document is written to the output directory, as specified by the
109 -- This can fail, but we don't purposefully throw any exceptions. If
110 -- something goes wrong, we would rather log it and keep going.
112 save_document :: Configuration -> String -> IO ()
113 save_document cfg doc =
115 Left err -> report_error err
117 already_exists <- doesFileExist path
118 when already_exists $ do
119 let msg = "File " ++ path ++ " already exists, overwriting."
122 report_info $ "Wrote file: " ++ path ++ "."
124 -- All the fmaps are because we're working inside a Maybe.
125 xmlfid = fmap show (parse_xmlfid doc)
126 filename = fmap (++ ".xml") xmlfid
127 either_path = fmap ((output_directory cfg) </>) filename
130 -- | Loop forever, writing the buffer to file whenever a </message>
131 -- tag is seen. This is the low-level "loop forever" function that
132 -- we stay in as long as we are connected to one feed.
134 -- The documentation at
135 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp> states
136 -- that \<message\> will always be the root element of the XML
137 -- documents, and \</message\> will be the final line transmitted
138 -- for a given document. We therefore rely on this to simplify
141 loop :: Configuration -> Handle -> [String] -> IO ()
142 loop !cfg !h !buffer = do
144 let new_buffer = line : buffer
146 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
147 -- send invalid junk (on the same line) after closing the root
149 if "</message>" `isPrefixOf` line
151 -- The buffer is in reverse (newest first) order, though, so we
152 -- have to reverse it first. We then concatenate all of its lines
153 -- into one big string.
154 let document = concat $ reverse new_buffer
155 save_document cfg document
156 loop cfg h [] -- Empty the buffer before looping again.
158 -- Append line to the head of the buffer and loop.
159 loop cfg h new_buffer
162 log_in :: Configuration -> Handle -> IO ()
164 prompt1 <- recv_prompt h
166 if prompt1 /= username_prompt then
167 report_error "Didn't receive username prompt."
169 send_line h (username cfg)
170 prompt2 <- recv_prompt h
172 if prompt2 /= password_prompt then
173 report_error "Didn't receive password prompt."
175 send_line h (password cfg)
176 _ <- recv_line h -- "The Sports Network"
179 username_prompt = "Username: "
180 password_prompt = "Password: "
182 send_line :: Handle -> String -> IO ()
184 let line = s ++ "\r\n"
186 -- Don't log the username/password!
189 recv_chars :: Int -> Handle -> IO String
191 s <- sequence [ hGetChar h' | _ <- [1..n] ]
195 recv_prompt :: Handle -> IO String
196 recv_prompt = recv_chars 10
199 connect_and_loop :: Configuration -> String -> IO ()
200 connect_and_loop cfg host = do
201 report_info $ "Connecting to " ++ host ++ "."
202 bracket acquire_handle release_handle action
206 five_seconds = 5000000
208 acquire_handle = connectTo host (PortNumber 4500)
209 release_handle = hClose
211 -- No buffering anywhere.
212 hSetBuffering h NoBuffering
214 -- The feed is often unresponsive after we send out username. It
215 -- happens in a telnet session, too (albeit less frequently?),
216 -- so there might be a bug on their end.
218 -- If we dump the packets with tcpdump, it looks like their
219 -- software is getting confused: they send us some XML in
220 -- the middle of the log-in procedure.
222 -- On the other hand, the documentation at
223 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
224 -- states that you can only make one connection per username to
225 -- a given host. So maybe they're simply rejecting the username
226 -- in an unfriendly fashion. In any case, the easiest fix is to
227 -- disconnect and try again.
229 login_worked <- timeout five_seconds $ log_in cfg h
231 Nothing -> report_info "Login timed out (5s)."
232 Just _ -> loop cfg h []
235 -- | A wrapper around threadDelay which takes seconds instead of
236 -- microseconds as its argument.
238 thread_sleep :: Int -> IO ()
239 thread_sleep seconds = do
240 let microseconds = seconds * (10 ^ (6 :: Int))
241 threadDelay microseconds
244 -- | The entry point of the program.
250 -- Merge the config file options with the command-line ones,
251 -- prefering the command-line ones.
252 let opt_config = rc_cfg <> cmd_cfg
254 -- Update a default config with any options that have been set in
255 -- either the config file or on the command-line. We initialize
256 -- logging before the missing parameter checks below so that we can
258 let cfg = (def :: Configuration) `merge_optional` opt_config
259 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
261 -- Check the optional config for missing required options. This is
262 -- necessary because if the user specifies an empty list of
263 -- hostnames in e.g. the config file, we want to bail rather than
264 -- fall back on the default list (which was merged from a default
265 -- Configuration above).
266 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
267 report_error "No feed hosts supplied."
268 exitWith (ExitFailure exit_no_feed_hosts)
270 when (isNothing (OC.password opt_config)) $ do
271 report_error "No password supplied."
272 exitWith (ExitFailure exit_no_password)
274 when (isNothing (OC.username opt_config)) $ do
275 report_error "No username supplied."
276 exitWith (ExitFailure exit_no_username)
278 when (daemonize cfg) $ do
279 pidfile_exists <- doesFileExist (pidfile cfg)
280 when pidfile_exists $ do
281 report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
282 ++ "Refusing to start."
283 exitWith (ExitFailure exit_pidfile_exists)
285 -- This may be superstition (and I believe stderr is unbuffered),
286 -- but it can't hurt.
287 hSetBuffering stderr NoBuffering
288 hSetBuffering stdout NoBuffering
290 -- The rest of the program is kicked off by the following line which
291 -- begins connecting to our feed hosts, starting with the first one,
292 -- and proceeds in a round-robin fashion.
293 let run_program = round_robin cfg 0
295 -- If we were asked to daemonize, do that; otherwise just run the thing.
297 then full_daemonize cfg run_program
301 -- | This is the top-level "loop forever" function. If an
302 -- exception is thrown, it will propagate up to this point, where
303 -- it will be logged and ignored in style.
305 -- Afterwards, we recurse (call ourself) again to loop more forevers.
307 round_robin :: Configuration -> Int -> IO ()
308 round_robin cfg feed_host_idx = do
309 let hosts = get_feed_hosts $ feed_hosts cfg
310 let host = hosts !! feed_host_idx
311 catchIOError (connect_and_loop cfg host) (report_error . show)
312 thread_sleep 5 -- Wait 5s before attempting to reconnect.
313 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)