]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Add a bunch of new options allowing htsn to daemonize.
[dead/htsn.git] / src / Main.hs
1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DoAndIfThenElse #-}
3
4 module Main
5 where
6
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 ( (<>) )
13 import Network (
14 connectTo,
15 PortID (PortNumber) )
16 import System.Console.CmdArgs ( def )
17 import System.Directory ( doesFileExist )
18 import System.Exit ( ExitCode(..), exitWith )
19 import System.FilePath ( (</>) )
20 import System.IO (
21 BufferMode (NoBuffering),
22 Handle,
23 hClose,
24 hGetChar,
25 hGetLine,
26 hPutStr,
27 hSetBuffering,
28 stderr,
29 stdout )
30 import System.IO.Error ( catchIOError )
31 import System.Timeout ( timeout )
32
33 import CommandLine ( get_args )
34 import Configuration ( Configuration(..), merge_optional )
35 import ExitCodes (
36 exit_no_feed_hosts,
37 exit_no_password,
38 exit_no_username,
39 exit_pidfile_exists )
40 import Logging (
41 init_logging,
42 log_debug,
43 log_error,
44 log_info,
45 log_warning )
46 import qualified OptionalConfiguration as OC (
47 OptionalConfiguration(..),
48 from_rc )
49 import Terminal (
50 display_debug,
51 display_error,
52 display_info,
53 display_sent,
54 display_warning )
55 import TSN.FeedHosts ( FeedHosts(..) )
56 import TSN.Xml ( parse_xmlfid )
57 import Unix ( full_daemonize )
58
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 ()
63 report_debug s = do
64 display_debug s
65 log_debug s
66
67
68 -- | Display and log an error condition. This will prefix the error
69 -- with "ERROR: " when displaying (but not logging) it so that it
70 -- stands out.
71 --
72 report_error :: String -> IO ()
73 report_error s = do
74 display_error $ "ERROR: " ++ s
75 log_error s
76
77
78 -- | Display and log an informational (status) message.
79 report_info :: String -> IO ()
80 report_info s = do
81 display_info s
82 log_info s
83
84
85 -- | Display and log a warning. This will prefix the warning with
86 -- "WARNING: " when displaying (but not logging) it so that it
87 -- stands out.
88 --
89 report_warning :: String -> IO ()
90 report_warning s = do
91 display_warning $ "WARNING: " ++ s
92 log_warning s
93
94
95 -- | Receive a single line of text from a Handle, and send it to the
96 -- debug log.
97 --
98 recv_line :: Handle -> IO String
99 recv_line h = do
100 line <- hGetLine h
101 report_debug (line ++ "\n")
102 return line
103
104
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
107 -- Configuration.
108 --
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.
111 --
112 save_document :: Configuration -> String -> IO ()
113 save_document cfg doc =
114 case either_path of
115 Left err -> report_error err
116 Right path -> do
117 already_exists <- doesFileExist path
118 when already_exists $ do
119 let msg = "File " ++ path ++ " already exists, overwriting."
120 report_warning msg
121 writeFile path doc
122 report_info $ "Wrote file: " ++ path ++ "."
123 where
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
128
129
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.
133 --
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
139 -- processing.
140 --
141 loop :: Configuration -> Handle -> [String] -> IO ()
142 loop !cfg !h !buffer = do
143 line <- recv_line h
144 let new_buffer = line : buffer
145
146 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
147 -- send invalid junk (on the same line) after closing the root
148 -- element.
149 if "</message>" `isPrefixOf` line
150 then do
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.
157 else
158 -- Append line to the head of the buffer and loop.
159 loop cfg h new_buffer
160
161
162 log_in :: Configuration -> Handle -> IO ()
163 log_in cfg h = do
164 prompt1 <- recv_prompt h
165
166 if prompt1 /= username_prompt then
167 report_error "Didn't receive username prompt."
168 else do
169 send_line h (username cfg)
170 prompt2 <- recv_prompt h
171
172 if prompt2 /= password_prompt then
173 report_error "Didn't receive password prompt."
174 else do
175 send_line h (password cfg)
176 _ <- recv_line h -- "The Sports Network"
177 return ()
178 where
179 username_prompt = "Username: "
180 password_prompt = "Password: "
181
182 send_line :: Handle -> String -> IO ()
183 send_line h' s = do
184 let line = s ++ "\r\n"
185 hPutStr h' line
186 -- Don't log the username/password!
187 display_sent line
188
189 recv_chars :: Int -> Handle -> IO String
190 recv_chars n h' = do
191 s <- sequence [ hGetChar h' | _ <- [1..n] ]
192 report_debug s
193 return s
194
195 recv_prompt :: Handle -> IO String
196 recv_prompt = recv_chars 10
197
198
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
203 return ()
204 where
205 five_seconds :: Int
206 five_seconds = 5000000
207
208 acquire_handle = connectTo host (PortNumber 4500)
209 release_handle = hClose
210 action h = do
211 -- No buffering anywhere.
212 hSetBuffering h NoBuffering
213
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.
217 --
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.
221 --
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.
228 --
229 login_worked <- timeout five_seconds $ log_in cfg h
230 case login_worked of
231 Nothing -> report_info "Login timed out (5s)."
232 Just _ -> loop cfg h []
233
234
235 -- | A wrapper around threadDelay which takes seconds instead of
236 -- microseconds as its argument.
237 --
238 thread_sleep :: Int -> IO ()
239 thread_sleep seconds = do
240 let microseconds = seconds * (10 ^ (6 :: Int))
241 threadDelay microseconds
242
243
244 -- | The entry point of the program.
245 main :: IO ()
246 main = do
247 rc_cfg <- OC.from_rc
248 cmd_cfg <- get_args
249
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
253
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
257 -- log the errors.
258 let cfg = (def :: Configuration) `merge_optional` opt_config
259 init_logging (log_file cfg) (log_level cfg) (syslog cfg)
260
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)
269
270 when (isNothing (OC.password opt_config)) $ do
271 report_error "No password supplied."
272 exitWith (ExitFailure exit_no_password)
273
274 when (isNothing (OC.username opt_config)) $ do
275 report_error "No username supplied."
276 exitWith (ExitFailure exit_no_username)
277
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)
284
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
289
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
294
295 -- If we were asked to daemonize, do that; otherwise just run the thing.
296 if (daemonize cfg)
297 then full_daemonize cfg run_program
298 else run_program
299
300 where
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.
304 --
305 -- Afterwards, we recurse (call ourself) again to loop more forevers.
306 --
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)