]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Minor documentation fixes; create all (even internal) docs with `make doc`.
[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 loop :: Configuration
109 -> Handle -- ^ Handle to the feed (network connection)
110 -> [String] -- ^ Current XML document buffer, line-by-line, in reverse
111 -> IO ()
112 loop !cfg !h !buffer = do
113 line <- recv_line h
114 let new_buffer = line : buffer
115
116 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
117 -- send invalid junk (on the same line) after closing the root
118 -- element.
119 if "</message>" `isPrefixOf` line
120 then do
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.
127 else
128 -- Append line to the head of the buffer and loop.
129 loop cfg h new_buffer
130
131
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.
135 --
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.
141 --
142 -- Assuming the above will always hold, it is implemented as follows:
143 --
144 -- 1. Receive 10 chars
145 --
146 -- 2. Send username if we got the username prompt
147 --
148 -- 3. Receive 10 chars
149 --
150 -- 4. Send password if we got the password prompt
151 --
152 -- If TSN likes the password as well, they send the string \"The
153 -- Sports Network\" before finally beginning to stream the feed.
154 --
155 log_in :: Configuration -> Handle -> IO ()
156 log_in cfg h = do
157 prompt1 <- recv_prompt h
158
159 if prompt1 /= username_prompt then
160 report_error "Didn't receive username prompt."
161 else do
162 send_cred h (username cfg)
163 prompt2 <- recv_prompt h
164
165 if prompt2 /= password_prompt then
166 report_error "Didn't receive password prompt."
167 else do
168 send_cred h (password cfg)
169 _ <- recv_line h -- "The Sports Network"
170 report_info $ "Logged in as " ++ (username cfg) ++ "."
171 return ()
172 where
173 username_prompt = "Username: "
174 password_prompt = "Password: "
175
176 send_cred :: Handle -> String -> IO ()
177 send_cred h' s = do
178 -- The carriage return is super important!
179 let line = s ++ "\r\n"
180 hPutStr h' line
181 display_sent line -- Don't log the username/password!
182
183 recv_chars :: Int -> Handle -> IO String
184 recv_chars n h' = do
185 s <- sequence [ hGetChar h' | _ <- [1..n] ]
186 report_debug s
187 return s
188
189 recv_prompt :: Handle -> IO String
190 recv_prompt = recv_chars 10
191
192
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@).
198 --
199 -- Steps:
200 --
201 -- 1. Connect to @host@ on the XML feed port.
202 --
203 -- 2. Log in.
204 --
205 -- 3. Go into the eternal read/save loop.
206 --
207 connect_and_parse :: Configuration
208 -> String -- ^ Hostname to connect to
209 -> IO ()
210 connect_and_parse cfg host = do
211 report_info $ "Connecting to " ++ host ++ "."
212 bracket acquire_handle release_handle action
213 return ()
214 where
215 five_seconds :: Int
216 five_seconds = 5000000
217
218 acquire_handle = connectTo host (PortNumber 4500)
219 release_handle = hClose
220 action h = do
221 -- No buffering anywhere.
222 hSetBuffering h NoBuffering
223
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.
227 --
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.
231 --
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.
238 --
239 login_worked <- timeout five_seconds $ log_in cfg h
240 case login_worked of
241 Nothing -> report_info $ "Login timed out (5 seconds). "
242 ++ "Waiting 5 seconds to reconnect."
243 Just _ -> loop cfg h []
244
245
246 -- | A wrapper around threadDelay which takes seconds instead of
247 -- microseconds as its argument.
248 --
249 thread_sleep :: Int -- ^ Number of seconds for which to sleep.
250 -> IO ()
251 thread_sleep seconds = do
252 let microseconds = seconds * (10 ^ (6 :: Int))
253 threadDelay microseconds
254
255
256 -- | The entry point of the program.
257 --
258 main :: IO ()
259 main = do
260 rc_cfg <- OC.from_rc
261 cmd_cfg <- get_args
262
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
266
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
270 -- log the errors.
271 let cfg = (def :: Configuration) `merge_optional` opt_config
272 init_logging (log_level cfg) (log_file cfg) (syslog cfg)
273
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)
282
283 when (isNothing (OC.password opt_config)) $ do
284 report_error "No password supplied."
285 exitWith (ExitFailure exit_no_password)
286
287 when (isNothing (OC.username opt_config)) $ do
288 report_error "No username supplied."
289 exitWith (ExitFailure exit_no_username)
290
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)
299
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
304
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
309
310 -- If we were asked to daemonize, do that; otherwise just run the thing.
311 if (daemonize cfg)
312 then try_daemonize cfg run_program
313 else run_program
314
315 where
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.
319 --
320 -- Afterwards, we recurse (call ourself) again to loop more forevers.
321 --
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)
329
330
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.
334 --
335 try_daemonize :: Configuration -> IO () -> IO ()
336 try_daemonize cfg program =
337 catchIOError
338 (full_daemonize cfg program)
339 (\e -> do
340 report_error (show e)
341 throw e)