]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Based on TSN documentation, split XML documents on the </message> tag instead of...
[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 import Logging (
40 init_logging,
41 log_debug,
42 log_error,
43 log_info,
44 log_warning )
45 import qualified OptionalConfiguration as OC (
46 OptionalConfiguration(..),
47 from_rc )
48 import Terminal (
49 display_debug,
50 display_error,
51 display_info,
52 display_sent,
53 display_warning )
54 import TSN.FeedHosts ( FeedHosts(..) )
55 import TSN.Xml ( parse_xmlfid )
56
57
58 -- | Warning! This does not automatically append a newline. The output
59 -- is displayed/logged as-is, for, you know, debug purposes.
60 report_debug :: String -> IO ()
61 report_debug s = do
62 display_debug s
63 log_debug s
64
65 report_error :: String -> IO ()
66 report_error s = do
67 display_error $ "ERROR: " ++ s
68 log_error s
69
70 report_info :: String -> IO ()
71 report_info s = do
72 display_info s
73 log_info s
74
75 -- | Warning! This does not automatically append a newline.
76 report_sent :: String -> IO ()
77 report_sent s = do
78 display_sent s
79 log_debug s
80
81 report_warning :: String -> IO ()
82 report_warning s = do
83 display_warning $ "WARNING: " ++ s
84 log_warning s
85
86
87 -- | Receive a single line of text from a Handle, and send it to the
88 -- debug log.
89 --
90 recv_line :: Handle -> IO String
91 recv_line h = do
92 line <- hGetLine h
93 report_debug (line ++ "\n")
94 return line
95
96
97 -- | Takes a Configuration, and an XML document (as a String). The XML
98 -- document is written to the output directory, as specified by the
99 -- Configuration.
100 --
101 -- This can fail, but we don't purposefully throw any exceptions. If
102 -- something goes wrong, we would rather log it and keep going.
103 --
104 save_document :: Configuration -> String -> IO ()
105 save_document cfg doc =
106 case maybe_path of
107 Nothing ->
108 report_error "Document missing XML_File_ID element."
109 Just path -> do
110 already_exists <- doesFileExist path
111 when already_exists $ do
112 let msg = "File " ++ path ++ " already exists, overwriting."
113 report_warning msg
114 writeFile path doc
115 report_info $ "Wrote file: " ++ path ++ "."
116 where
117 xmlfid = fmap show (parse_xmlfid doc)
118 filename = fmap (++ ".xml") xmlfid
119 maybe_path = fmap ((output_directory cfg) </>) filename
120
121
122 -- | Loop forever, writing the buffer to file whenever a </message>
123 -- tag is seen. This is the low-level "loop forever" function that
124 -- we stay in as long as we are connected to one feed.
125 --
126 -- The documentation at
127 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp> states
128 -- that \<message\> will always be the root element of the XML
129 -- documents, and \</message\> will be the final line transmitted
130 -- for a given document. We therefore rely on this to simplify
131 -- processing.
132 --
133 loop :: Configuration -> Handle -> [String] -> IO ()
134 loop !cfg !h !buffer = do
135 line <- recv_line h
136 let new_buffer = line : buffer
137
138 -- Use isPrefixOf to avoid line-ending issues. Hopefully they won't
139 -- send invalid junk (on the same line) after closing the root
140 -- element.
141 if "</message>" `isPrefixOf` line
142 then do
143 -- The buffer is in reverse (newest first) order, though, so we
144 -- have to reverse it first. We then concatenate all of its lines
145 -- into one big string.
146 let document = concat $ reverse new_buffer
147 save_document cfg document
148 loop cfg h [] -- Empty the buffer before looping again.
149 else
150 -- Append line to the head of the buffer and loop.
151 loop cfg h new_buffer
152
153
154 log_in :: Configuration -> Handle -> IO ()
155 log_in cfg h = do
156 prompt1 <- recv_prompt h
157
158 if prompt1 /= username_prompt then
159 report_error "Didn't receive username prompt."
160 else do
161 send_line h (username cfg)
162 prompt2 <- recv_prompt h
163
164 if prompt2 /= password_prompt then
165 report_error "Didn't receive password prompt."
166 else do
167 send_line h (password cfg)
168 _ <- recv_line h -- "The Sports Network"
169 return ()
170 where
171 username_prompt = "Username: "
172 password_prompt = "Password: "
173
174 send_line :: Handle -> String -> IO ()
175 send_line h' s = do
176 let line = s ++ "\r\n"
177 hPutStr h' line
178 display_sent line
179
180 recv_chars :: Int -> Handle -> IO String
181 recv_chars n h' = do
182 s <- sequence [ hGetChar h' | _ <- [1..n] ]
183 report_debug s
184 return s
185
186 recv_prompt :: Handle -> IO String
187 recv_prompt = recv_chars 10
188
189
190 connect_and_loop :: Configuration -> String -> IO ()
191 connect_and_loop cfg host = do
192 report_info $ "Connecting to " ++ host ++ "..."
193 bracket acquire_handle release_handle action
194 return ()
195 where
196 five_seconds :: Int
197 five_seconds = 5000000
198
199 acquire_handle = connectTo host (PortNumber 4500)
200 release_handle = hClose
201 action h = do
202 -- No buffering anywhere.
203 hSetBuffering h NoBuffering
204
205 -- The feed is often unresponsive after we send out username. It
206 -- happens in a telnet session, too (albeit less frequently?),
207 -- so there might be a bug on their end.
208 --
209 -- If we dump the packets with tcpdump, it looks like their
210 -- software is getting confused: they send us some XML in
211 -- the middle of the log-in procedure.
212 --
213 -- On the other hand, the documentation at
214 -- <http://www.sportsnetworkdata.com/feeds/xml-levels.asp>
215 -- states that you can only make one connection per username to
216 -- a given host. So maybe they're simply rejecting the username
217 -- in an unfriendly fashion. In any case, the easiest fix is to
218 -- disconnect and try again.
219 --
220 login_worked <- timeout five_seconds $ log_in cfg h
221 case login_worked of
222 Nothing -> report_info "Login timed out (5s)."
223 Just _ -> loop cfg h []
224
225
226 -- | A wrapper around threadDelay which takes seconds instead of
227 -- microseconds as its argument.
228 --
229 thread_sleep :: Int -> IO ()
230 thread_sleep seconds = do
231 let microseconds = seconds * (10 ^ (6 :: Int))
232 threadDelay microseconds
233
234
235 -- | The entry point of the program.
236 main :: IO ()
237 main = do
238 rc_cfg <- OC.from_rc
239 cmd_cfg <- get_args
240
241 -- Merge the config file options with the command-line ones,
242 -- prefering the command-line ones.
243 let opt_config = rc_cfg <> cmd_cfg
244
245 -- Update a default config with any options that have been set in
246 -- either the config file or on the command-line. We initialize
247 -- logging before the missing parameter checks below so that we can
248 -- log the errors.
249 let cfg = (def :: Configuration) `merge_optional` opt_config
250 init_logging (syslog cfg)
251
252 -- Check the optional config for missing required options. This is
253 -- necessary because if the user specifies an empty list of
254 -- hostnames in e.g. the config file, we want to bail rather than
255 -- fall back on the default list (which was merged from a default
256 -- Configuration above).
257 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
258 report_error "No feed hosts supplied."
259 exitWith (ExitFailure exit_no_feed_hosts)
260
261 when (isNothing (OC.password opt_config)) $ do
262 report_error "No password supplied."
263 exitWith (ExitFailure exit_no_password)
264
265 when (isNothing (OC.username opt_config)) $ do
266 report_error "No username supplied."
267 exitWith (ExitFailure exit_no_username)
268
269 -- This may be superstition (and I believe stderr is unbuffered),
270 -- but it can't hurt.
271 hSetBuffering stderr NoBuffering
272 hSetBuffering stdout NoBuffering
273
274 -- Begin connecting to our feed hosts, starting with the first one.
275 round_robin cfg 0
276
277 where
278 -- | This is the top-level "loop forever" function. If an
279 -- exception is thrown, it will propagate up to this point, where
280 -- it will be logged and ignored in style.
281 --
282 -- Afterwards, we recurse (call ourself) again to loop more forevers.
283 --
284 round_robin :: Configuration -> Int -> IO ()
285 round_robin cfg feed_host_idx = do
286 let hosts = get_feed_hosts $ feed_hosts cfg
287 let host = hosts !! feed_host_idx
288 catchIOError (connect_and_loop cfg host) (report_error . show)
289 thread_sleep 5 -- Wait 5s before attempting to reconnect.
290 round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)