]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Main.hs
Initial commit of something working.
[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.DeepSeq (deepseq)
9 import Control.Exception.Base (bracket)
10 import Control.Monad (forever, 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 System.Console.CmdArgs (def)
18 import System.Directory (doesFileExist)
19 import System.Exit (ExitCode(..), exitWith)
20 import System.FilePath ((</>))
21 import System.IO (
22 BufferMode (NoBuffering),
23 Handle,
24 hClose,
25 hGetChar,
26 hGetLine,
27 hPutStr,
28 hSetBuffering,
29 stderr,
30 stdout )
31 import System.IO.Error (catchIOError)
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 FeedHosts (FeedHosts(..))
40 import qualified OptionalConfiguration as OC (
41 OptionalConfiguration(..),
42 from_rc )
43 import Terminal (hPutRedLn, putGreenLn)
44 import TSN.Xml (parse_xmlfid, xml_prologue)
45
46
47 report_error :: String -> IO ()
48 report_error = hPutRedLn stderr
49
50
51 recv_line :: Handle -> IO String
52 recv_line h = do
53 line <- hGetLine h
54 putStrLn line
55 return line
56
57
58 save_document :: Configuration -> String -> IO ()
59 save_document cfg doc = do
60 case maybe_path of
61 Nothing ->
62 report_error "ERROR: document missing XML_File_ID element."
63 Just path -> do
64 already_exists <- doesFileExist path
65 when already_exists $ do
66 let msg = "WARNING: file " ++ path ++ " already exists. Overwriting."
67 report_error msg
68 writeFile path doc
69 where
70 xmlfid = fmap show (parse_xmlfid doc)
71 filename = fmap (++ ".xml") xmlfid
72 maybe_path = fmap ((output_directory cfg) </>) filename
73
74 -- | Loop forever, writing the buffer to file whenever a new XML
75 -- prologue is seen.
76 loop :: Configuration -> Handle -> [String] -> IO ()
77 loop !cfg !h !buffer = do
78 line <- recv_line h
79
80 if (xml_prologue `isPrefixOf` line && not (null buffer))
81 then do
82 -- This is the beginning of a new document, and we have an "old"
83 -- one to save. The buffer is in reverse (newest first) order,
84 -- though, so we have to reverse it first. We then concatenate all
85 -- of its lines into one big string.
86 let document = concat $ reverse buffer
87 save_document cfg document
88 loop cfg h [line] -- empty the buffer before looping again
89 else
90 loop cfg h (line : buffer) -- append line to the head of the buffer and loop
91
92
93 log_in :: Configuration -> Handle -> IO ()
94 log_in cfg h = do
95 prompt1 <- recv_prompt h
96
97 if prompt1 /= username_prompt then
98 report_error "ERROR: didn't receive username prompt."
99 else do
100 send_line h (username cfg)
101 prompt2 <- recv_prompt h
102
103 if prompt2 /= password_prompt then
104 report_error "ERROR: didn't receive password prompt."
105 else do
106 send_line h (password cfg)
107 banner <- recv_line h -- "The Sports Network"
108 banner `deepseq` return ()
109 where
110 username_prompt = "Username: "
111 password_prompt = "Password: "
112
113 send_line :: Handle -> String -> IO ()
114 send_line h' s = do
115 hPutStr h' (s ++ "\r\n")
116 putGreenLn s
117
118 recv_chars :: Int -> Handle -> IO String
119 recv_chars n h' = do
120 s <- sequence [ hGetChar h' | _ <- [1..n] ]
121 putStr s
122 return s
123
124 recv_prompt :: Handle -> IO String
125 recv_prompt = recv_chars 10
126
127 connect_and_loop :: Configuration -> IO ()
128 connect_and_loop cfg =
129 bracket acquire_handle release_handle action
130 where
131 --acquire_handle = connectTo "feed1.sportsnetwork.com" (PortNumber 4500)
132 acquire_handle = connectTo "feed2.sportsnetwork.com" (PortNumber 4500)
133 --acquire_handle = connectTo "127.0.0.1" (PortNumber 13337)
134 release_handle = hClose
135 action h = do
136 -- No buffering anywhere.
137 hSetBuffering h NoBuffering
138 log_in cfg h
139 loop cfg h []
140
141
142 -- | A wrapper around threadDelay which takes seconds instead of
143 -- microseconds as its argument.
144 thread_sleep :: Int -> IO ()
145 thread_sleep seconds = do
146 let microseconds = seconds * (10 ^ (6 :: Int))
147 threadDelay microseconds
148
149
150 main :: IO ()
151 main = do
152 rc_cfg <- OC.from_rc
153 cmd_cfg <- get_args
154
155 -- Merge the config file options with the command-line ones,
156 -- prefering the command-line ones.
157 let opt_config = rc_cfg <> cmd_cfg
158
159 when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
160 report_error "ERROR: no feed hosts supplied."
161 exitWith (ExitFailure exit_no_feed_hosts)
162
163 when (isNothing (OC.password opt_config)) $ do
164 report_error "ERROR: no password supplied."
165 exitWith (ExitFailure exit_no_password)
166
167 when (isNothing (OC.username opt_config)) $ do
168 report_error "ERROR: no username supplied."
169 exitWith (ExitFailure exit_no_username)
170
171 -- Finally, update a default config with any options that have been
172 -- set in either the config file or on the command-line.
173 let cfg = (def :: Configuration) `merge_optional` opt_config
174
175 hSetBuffering stderr NoBuffering
176 hSetBuffering stdout NoBuffering
177
178 forever $ do
179 catchIOError (connect_and_loop cfg) (report_error . show)
180 thread_sleep 10 -- Wait 10s before attempting to reconnect.