]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Add a fifteen minute data timeout within the main loop.
[dead/htsn.git] / src / Main.hs
1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
3
4 module Main
5 where
6
7 -- System imports.
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 ( (<>) )
14 import Network (
15 connectTo,
16 PortID (PortNumber) )
17 import Network.Services.TSN.Logging ( init_logging )
18 import Network.Services.TSN.Report (
19 report_debug,
20 report_info,
21 report_warning,
22 report_error )
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 ( (</>) )
28 import System.IO (
29 BufferMode (NoBuffering),
30 Handle,
31 hClose,
32 hGetChar,
33 hGetLine,
34 hPutStr,
35 hSetBuffering,
36 stderr,
37 stdout )
38 import System.IO.Error ( catchIOError )
39 import System.Timeout ( timeout )
40
41 -- Local imports.
42 import CommandLine ( get_args )
43 import Configuration ( Configuration(..), merge_optional )
44 import ExitCodes (
45 exit_no_feed_hosts,
46 exit_no_password,
47 exit_no_username,
48 exit_pidfile_exists )
49 import FeedHosts ( FeedHosts(..) )
50 import qualified OptionalConfiguration as OC (
51 OptionalConfiguration(..),
52 from_rc )
53 import Xml ( parse_xmlfid )
54 import Unix ( full_daemonize )
55
56
57 -- | Receive a single line of text from a 'Handle', and record it for
58 -- debugging purposes.
59 --
60 recv_line :: Handle -> IO String
61 recv_line h = do
62 line <- hGetLine h
63 report_debug (line ++ "\n")
64 return line
65
66
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'.
70 --
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.
73 --
74 save_document :: Configuration
75 -> String -- ^ String representation of an XML document
76 -> IO ()
77 save_document cfg doc =
78 case either_path of
79 Left err -> report_error err
80 Right path -> do
81 already_exists <- doesFileExist path
82 when already_exists $ do
83 let msg = "File " ++ path ++ " already exists, overwriting."
84 report_warning msg
85 writeFile path doc
86 report_info $ "Wrote file: " ++ path ++ "."
87 where
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
92
93
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.
97 --
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
103 -- processing.
104 --
105 -- The bang pattern at least on @buffer@ is necessary for
106 -- performance reasons.
107 --
108 -- We specify a timeout of fifteen minutes on the 'recv_line'
109 -- function, after which we will return to our caller. This should
110 -- cause the connection to be dropped, and a new one initiated. The
111 -- timeout is in response to observed behavior where the feed
112 -- eventually stops transmitting data entirely without closing the
113 -- connection.
114 --
115 loop :: Configuration
116 -> Handle -- ^ Handle to the feed (network connection)
117 -> [String] -- ^ Current XML document buffer, line-by-line, in reverse
118 -> IO ()
119 loop !cfg !h !buffer = do
120 line' <- timeout fifteen_minutes $ recv_line h
121 case line' of
122 -- If we haven't received anything in fifteen minutes, return back
123 -- to the calling function. This should only happen in the case of
124 -- an error, and our caller should be prepared to handle it.
125 Nothing -> do
126 report_warning $ "No data received for 15 minutes."
127 Just line -> do
128 -- If the recv didn't timeout, proceed normally.
129 let new_buffer = line : buffer
130
131 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
132 -- send invalid junk (on the same line) after closing the root
133 -- element.
134 if "</message>" `isPrefixOf` line
135 then do
136 -- The buffer is in reverse (newest first) order, though, so we
137 -- have to reverse it first. We then concatenate all of its lines
138 -- into one big string.
139 let document = concat $ reverse new_buffer
140 save_document cfg document
141 loop cfg h [] -- Empty the buffer before looping again.
142 else
143 -- Append line to the head of the buffer and loop.
144 loop cfg h new_buffer
145 where
146 fifteen_minutes :: Int
147 fifteen_minutes = 15 * 60 * 1000000
148
149
150 -- | Once we're connected to a feed, we need to log in. There's no
151 -- protocol for this (the docs don't mention one), but we have
152 -- (apparently) successfully guessed it.
153 --
154 -- The first thing TSN sends once we've connected is the string
155 -- \"Username: \", containing 10 ASCII characters. We then send a
156 -- username, followed by a newline. If TSN likes the username, the
157 -- second they'll send is the string \"Password: \", also containing
158 -- 10 ASCII characters, to which we reply in kind.
159 --
160 -- Assuming the above will always hold, it is implemented as follows:
161 --
162 -- 1. Receive 10 chars
163 --
164 -- 2. Send username if we got the username prompt
165 --
166 -- 3. Receive 10 chars
167 --
168 -- 4. Send password if we got the password prompt
169 --
170 -- If TSN likes the password as well, they send the string \"The
171 -- Sports Network\" before finally beginning to stream the feed.
172 --
173 log_in :: Configuration -> Handle -> IO ()
174 log_in cfg h = do
175 prompt1 <- recv_prompt h
176
177 if prompt1 /= username_prompt then
178 report_error "Didn't receive username prompt."
179 else do
180 send_cred h (username cfg)
181 prompt2 <- recv_prompt h
182
183 if prompt2 /= password_prompt then
184 report_error "Didn't receive password prompt."
185 else do
186 send_cred h (password cfg)
187 _ <- recv_line h -- "The Sports Network"
188 report_info $ "Logged in as " ++ (username cfg) ++ "."
189 return ()
190 where
191 username_prompt = "Username: "
192 password_prompt = "Password: "
193
194 send_cred :: Handle -> String -> IO ()
195 send_cred h' s = do
196 -- The carriage return is super important!
197 let line = s ++ "\r\n"
198 hPutStr h' line
199 display_sent line -- Don't log the username/password!
200
201 recv_chars :: Int -> Handle -> IO String
202 recv_chars n h' = do
203 s <- sequence [ hGetChar h' | _ <- [1..n] ]
204 report_debug s
205 return s
206
207 recv_prompt :: Handle -> IO String
208 recv_prompt = recv_chars 10
209
210
211 -- | Connect to @host@ and attempt to parse the feed. As long as we
212 -- stay connected and nothing bad happens, the program will remain in
213 -- this function. If anything goes wrong, then the current invocation
214 -- of connect_and_parse will return, and get called again later
215 -- (probably with a different @host@).
216 --
217 -- Steps:
218 --
219 -- 1. Connect to @host@ on the XML feed port.
220 --
221 -- 2. Log in.
222 --
223 -- 3. Go into the eternal read/save loop.
224 --
225 connect_and_parse :: Configuration
226 -> String -- ^ Hostname to connect to
227 -> IO ()
228 connect_and_parse cfg host = do
229 bracket acquire_handle release_handle action
230 where
231 five_seconds :: Int
232 five_seconds = 5 * 1000000
233
234 acquire_handle = do
235 report_info $ "Connecting to " ++ host ++ "."
236 connectTo host (PortNumber 4500)
237
238 release_handle = \h -> do
239 report_info $ "Closing connection to " ++ host ++ "."
240 hClose h
241
242 action h = do
243 -- No buffering anywhere.
244 hSetBuffering h NoBuffering
245
246 -- The feed is often unresponsive after we send out username. It
247 -- happens in a telnet session, too (albeit less frequently?),
248 -- so there might be a bug on their end.
249 --
250 -- If we dump the packets with tcpdump, it looks like their
251 -- software is getting confused: they send us some XML in
252 -- the middle of the log-in procedure.
253 --
254 -- On the other hand, the documentation at
255 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
256 -- states that you can only make one connection per username to
257 -- a given host. So maybe they're simply rejecting the username
258 -- in an unfriendly fashion. In any case, the easiest fix is to
259 -- disconnect and try again.
260 --
261 login_worked <- timeout five_seconds $ log_in cfg h
262 case login_worked of
263 Nothing -> report_info $ "Login timed out (5 seconds). "
264 ++ "Waiting 5 seconds to reconnect."
265
266 -- If loop returns (due to its timeout), it will pop out right
267 -- here and the action will terminate causing 'release_handle'
268 -- to trigger.
269 Just _ -> loop cfg h []
270
271
272 -- | A wrapper around threadDelay which takes seconds instead of
273 -- microseconds as its argument.
274 --
275 thread_sleep :: Int -- ^ Number of seconds for which to sleep.
276 -> IO ()
277 thread_sleep seconds = do
278 let microseconds = seconds * (10 ^ (6 :: Int))
279 threadDelay microseconds
280
281
282 -- | The entry point of the program.
283 --
284 main :: IO ()
285 main = do
286 rc_cfg <- OC.from_rc
287 cmd_cfg <- get_args
288
289 -- Merge the config file options with the command-line ones,
290 -- prefering the command-line ones.
291 let opt_config = rc_cfg <> cmd_cfg
292
293 -- Update a default config with any options that have been set in
294 -- either the config file or on the command-line. We initialize
295 -- logging before the missing parameter checks below so that we can
296 -- log the errors.
297 let cfg = (def :: Configuration) `merge_optional` opt_config
298 init_logging (log_level cfg) (log_file cfg) (syslog cfg)
299
300 -- Check the optional config for missing required options. This is
301 -- necessary because if the user specifies an empty list of
302 -- hostnames in e.g. the config file, we want to bail rather than
303 -- fall back on the default list (which was merged from a default
304 -- Configuration above).
305 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
306 report_error "No feed hosts supplied."
307 exitWith (ExitFailure exit_no_feed_hosts)
308
309 when (isNothing (OC.password opt_config)) $ do
310 report_error "No password supplied."
311 exitWith (ExitFailure exit_no_password)
312
313 when (isNothing (OC.username opt_config)) $ do
314 report_error "No username supplied."
315 exitWith (ExitFailure exit_no_username)
316
317 when (daemonize cfg) $ do
318 -- Old PID files can be left around after an unclean shutdown. We
319 -- only care if we're running as a daemon.
320 pidfile_exists <- doesFileExist (pidfile cfg)
321 when pidfile_exists $ do
322 report_error $ "PID file " ++ (pidfile cfg) ++ " already exists. "
323 ++ "Refusing to start."
324 exitWith (ExitFailure exit_pidfile_exists)
325
326 -- This may be superstition (and I believe stderr is unbuffered),
327 -- but it can't hurt.
328 hSetBuffering stderr NoBuffering
329 hSetBuffering stdout NoBuffering
330
331 -- The rest of the program is kicked off by the following line which
332 -- begins connecting to our feed hosts, starting with the first one,
333 -- and proceeds in a round-robin fashion.
334 let run_program = round_robin cfg 0
335
336 -- If we were asked to daemonize, do that; otherwise just run the thing.
337 if (daemonize cfg)
338 then try_daemonize cfg run_program
339 else run_program
340
341 where
342 -- | This is the top-level \"loop forever\" function. If an
343 -- exception is thrown, it will propagate up to this point, where
344 -- it will be logged and ignored in style.
345 --
346 -- Afterwards, we recurse (call ourself) again to loop more forevers.
347 --
348 round_robin :: Configuration -> Int -> IO ()
349 round_robin cfg feed_host_idx = do
350 let hosts = get_feed_hosts $ feed_hosts cfg
351 let host = hosts !! feed_host_idx
352 catchIOError (connect_and_parse cfg host) (report_error . show)
353 thread_sleep 5 -- Wait 5s before attempting to reconnect.
354 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)
355
356
357 -- | A exception handler around full_daemonize. If full_daemonize
358 -- doesn't work, we report the error and crash. This is fine; we
359 -- only need the program to be resilient once it actually starts.
360 --
361 try_daemonize :: Configuration -> IO () -> IO ()
362 try_daemonize cfg program =
363 catchIOError
364 (full_daemonize cfg program)
365 (\e -> do
366 report_error (show e)
367 throw e)