1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
8 import Control.Concurrent ( threadDelay )
9 import Control.Exception ( bracket, throw )
10 import Control.Monad ( when )
11 import Data.List ( isPrefixOf )
12 import Data.Maybe ( isNothing )
13 import Data.Monoid ( (<>) )
17 import Network.Services.TSN.Logging ( init_logging )
18 import Network.Services.TSN.Report (
23 import Network.Services.TSN.Terminal ( display_sent )
24 import System.Console.CmdArgs ( def )
25 import System.Directory ( doesFileExist )
26 import System.Exit ( ExitCode(..), exitWith )
27 import System.FilePath ( (</>) )
29 BufferMode (NoBuffering),
38 import System.IO.Error ( catchIOError )
39 import System.Timeout ( timeout )
42 import CommandLine ( get_args )
43 import Configuration ( Configuration(..), merge_optional )
49 import FeedHosts ( FeedHosts(..) )
50 import qualified OptionalConfiguration as OC (
51 OptionalConfiguration(..),
53 import Xml ( parse_xmlfid )
54 import Unix ( full_daemonize )
57 -- | Receive a single line of text from a 'Handle', and record it for
58 -- debugging purposes.
60 recv_line :: Handle -> IO String
63 report_debug (line ++ "\n")
67 -- | Takes a 'Configuration', and an XML document (as a 'String'). The
68 -- XML document is written to the output directory, as specified by
69 -- the 'Configuration'.
71 -- This can fail, but we don't purposefully throw any exceptions. If
72 -- something goes wrong, we would rather log it and keep going.
74 save_document :: Configuration
75 -> String -- ^ String representation of an XML document
77 save_document cfg doc =
79 Left err -> report_error err
81 already_exists <- doesFileExist path
82 when already_exists $ do
83 let msg = "File " ++ path ++ " already exists, overwriting."
86 report_info $ "Wrote file: " ++ path ++ "."
88 -- All the fmaps are because we're working inside a Maybe.
89 xmlfid = fmap show (parse_xmlfid doc)
90 filename = fmap (++ ".xml") xmlfid
91 either_path = fmap ((output_directory cfg) </>) filename
94 -- | Loop forever, writing the @buffer@ to file whenever a
95 -- \</message\> tag is seen. This is the low-level \"loop forever\"
96 -- function that we stay in as long as we are connected to one feed.
98 -- The documentation at
99 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp> states
100 -- that \<message\> will always be the root element of the XML
101 -- documents, and \</message\> will be the final line transmitted
102 -- for a given document. We therefore rely on this to simplify
105 -- The bang pattern at least on @buffer@ is necessary for
106 -- performance reasons.
108 loop :: Configuration
109 -> Handle -- ^ Handle to the feed (network connection)
110 -> [String] -- ^ Current XML document buffer, line-by-line, in reverse
112 loop !cfg !h !buffer = do
114 let new_buffer = line : buffer
116 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
117 -- send invalid junk (on the same line) after closing the root
119 if "</message>" `isPrefixOf` line
121 -- The buffer is in reverse (newest first) order, though, so we
122 -- have to reverse it first. We then concatenate all of its lines
123 -- into one big string.
124 let document = concat $ reverse new_buffer
125 save_document cfg document
126 loop cfg h [] -- Empty the buffer before looping again.
128 -- Append line to the head of the buffer and loop.
129 loop cfg h new_buffer
132 -- | Once we're connected to a feed, we need to log in. There's no
133 -- protocol for this (the docs don't mention one), but we have
134 -- (apparently) successfully guessed it.
136 -- The first thing TSN sends once we've connected is the string
137 -- \"Username: \", containing 10 ASCII characters. We then send a
138 -- username, followed by a newline. If TSN likes the username, the
139 -- second they'll send is the string \"Password: \", also containing
140 -- 10 ASCII characters, to which we reply in kind.
142 -- Assuming the above will always hold, it is implemented as follows:
144 -- 1. Receive 10 chars
146 -- 2. Send username if we got the username prompt
148 -- 3. Receive 10 chars
150 -- 4. Send password if we got the password prompt
152 -- If TSN likes the password as well, they send the string \"The
153 -- Sports Network\" before finally beginning to stream the feed.
155 log_in :: Configuration -> Handle -> IO ()
157 prompt1 <- recv_prompt h
159 if prompt1 /= username_prompt then
160 report_error "Didn't receive username prompt."
162 send_cred h (username cfg)
163 prompt2 <- recv_prompt h
165 if prompt2 /= password_prompt then
166 report_error "Didn't receive password prompt."
168 send_cred h (password cfg)
169 _ <- recv_line h -- "The Sports Network"
170 report_info $ "Logged in as " ++ (username cfg) ++ "."
173 username_prompt = "Username: "
174 password_prompt = "Password: "
176 send_cred :: Handle -> String -> IO ()
178 -- The carriage return is super important!
179 let line = s ++ "\r\n"
181 display_sent line -- Don't log the username/password!
183 recv_chars :: Int -> Handle -> IO String
185 s <- sequence [ hGetChar h' | _ <- [1..n] ]
189 recv_prompt :: Handle -> IO String
190 recv_prompt = recv_chars 10
193 -- | Connect to @host@ and attempt to parse the feed. As long as we
194 -- stay connected and nothing bad happens, the program will remain in
195 -- this function. If anything goes wrong, then the current invocation
196 -- of connect_and_parse will return, and get called again later
197 -- (probably with a different @host@).
201 -- 1. Connect to @host@ on the XML feed port.
205 -- 3. Go into the eternal read/save loop.
207 connect_and_parse :: Configuration
208 -> String -- ^ Hostname to connect to
210 connect_and_parse cfg host = do
211 report_info $ "Connecting to " ++ host ++ "."
212 bracket acquire_handle release_handle action
216 five_seconds = 5000000
218 acquire_handle = connectTo host (PortNumber 4500)
219 release_handle = hClose
221 -- No buffering anywhere.
222 hSetBuffering h NoBuffering
224 -- The feed is often unresponsive after we send out username. It
225 -- happens in a telnet session, too (albeit less frequently?),
226 -- so there might be a bug on their end.
228 -- If we dump the packets with tcpdump, it looks like their
229 -- software is getting confused: they send us some XML in
230 -- the middle of the log-in procedure.
232 -- On the other hand, the documentation at
233 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
234 -- states that you can only make one connection per username to
235 -- a given host. So maybe they're simply rejecting the username
236 -- in an unfriendly fashion. In any case, the easiest fix is to
237 -- disconnect and try again.
239 login_worked <- timeout five_seconds $ log_in cfg h
241 Nothing -> report_info $ "Login timed out (5 seconds). "
242 ++ "Waiting 5 seconds to reconnect."
243 Just _ -> loop cfg h []
246 -- | A wrapper around threadDelay which takes seconds instead of
247 -- microseconds as its argument.
249 thread_sleep :: Int -- ^ Number of seconds for which to sleep.
251 thread_sleep seconds = do
252 let microseconds = seconds * (10 ^ (6 :: Int))
253 threadDelay microseconds
256 -- | The entry point of the program.
263 -- Merge the config file options with the command-line ones,
264 -- prefering the command-line ones.
265 let opt_config = rc_cfg <> cmd_cfg
267 -- Update a default config with any options that have been set in
268 -- either the config file or on the command-line. We initialize
269 -- logging before the missing parameter checks below so that we can
271 let cfg = (def :: Configuration) `merge_optional` opt_config
272 init_logging (log_level cfg) (log_file cfg) (syslog cfg)
274 -- Check the optional config for missing required options. This is
275 -- necessary because if the user specifies an empty list of
276 -- hostnames in e.g. the config file, we want to bail rather than
277 -- fall back on the default list (which was merged from a default
278 -- Configuration above).
279 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
280 report_error "No feed hosts supplied."
281 exitWith (ExitFailure exit_no_feed_hosts)
283 when (isNothing (OC.password opt_config)) $ do
284 report_error "No password supplied."
285 exitWith (ExitFailure exit_no_password)
287 when (isNothing (OC.username opt_config)) $ do
288 report_error "No username supplied."
289 exitWith (ExitFailure exit_no_username)
291 when (daemonize cfg) $ do
292 -- Old PID files can be left around after an unclean shutdown. We
293 -- only care if we're running as a daemon.
294 pidfile_exists <- doesFileExist (pidfile cfg)
295 when pidfile_exists $ do
296 report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
297 ++ "Refusing to start."
298 exitWith (ExitFailure exit_pidfile_exists)
300 -- This may be superstition (and I believe stderr is unbuffered),
301 -- but it can't hurt.
302 hSetBuffering stderr NoBuffering
303 hSetBuffering stdout NoBuffering
305 -- The rest of the program is kicked off by the following line which
306 -- begins connecting to our feed hosts, starting with the first one,
307 -- and proceeds in a round-robin fashion.
308 let run_program = round_robin cfg 0
310 -- If we were asked to daemonize, do that; otherwise just run the thing.
312 then try_daemonize cfg run_program
316 -- | This is the top-level \"loop forever\" function. If an
317 -- exception is thrown, it will propagate up to this point, where
318 -- it will be logged and ignored in style.
320 -- Afterwards, we recurse (call ourself) again to loop more forevers.
322 round_robin :: Configuration -> Int -> IO ()
323 round_robin cfg feed_host_idx = do
324 let hosts = get_feed_hosts $ feed_hosts cfg
325 let host = hosts !! feed_host_idx
326 catchIOError (connect_and_parse cfg host) (report_error . show)
327 thread_sleep 5 -- Wait 5s before attempting to reconnect.
328 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)
331 -- | A exception handler around full_daemonize. If full_daemonize
332 -- doesn't work, we report the error and crash. This is fine; we
333 -- only need the program to be resilient once it actually starts.
335 try_daemonize :: Configuration -> IO () -> IO ()
336 try_daemonize cfg program =
338 (full_daemonize cfg program)
340 report_error (show e)