]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/Main.hs
Move FeedHosts under the TSN namespace.
[dead/htsn.git] / src / Main.hs
index 829440ec35fa1a364d11a6b9605edf866df0bd94..374981248fd9bf2b17905175949914d89cea70e4 100644 (file)
@@ -5,9 +5,8 @@ module Main
 where
 
 import Control.Concurrent (threadDelay)
-import Control.DeepSeq (deepseq)
 import Control.Exception.Base (bracket)
-import Control.Monad (forever, when)
+import Control.Monad (when)
 import Data.List (isPrefixOf)
 import Data.Maybe (isNothing)
 import Data.Monoid ((<>))
@@ -29,6 +28,7 @@ import System.IO (
   stderr,
   stdout )
 import System.IO.Error (catchIOError)
+import System.Timeout (timeout)
 
 import CommandLine (get_args)
 import Configuration (Configuration(..), merge_optional)
@@ -36,18 +36,14 @@ import ExitCodes (
   exit_no_feed_hosts,
   exit_no_password,
   exit_no_username )
-import FeedHosts (FeedHosts(..))
 import qualified OptionalConfiguration as OC (
   OptionalConfiguration(..),
   from_rc )
-import Terminal (hPutRedLn, putGreenLn)
+import Terminal (putGreenLn, report_error)
+import TSN.FeedHosts (FeedHosts(..))
 import TSN.Xml (parse_xmlfid, xml_prologue)
 
 
-report_error :: String -> IO ()
-report_error = hPutRedLn stderr
-
-
 recv_line :: Handle -> IO String
 recv_line h = do
   line <- hGetLine h
@@ -56,7 +52,7 @@ recv_line h = do
 
 
 save_document :: Configuration -> String -> IO ()
-save_document cfg doc = do
+save_document cfg doc =
   case maybe_path of
     Nothing ->
       report_error "ERROR: document missing XML_File_ID element."
@@ -104,8 +100,8 @@ log_in cfg h = do
       report_error "ERROR: didn't receive password prompt."
     else do
       send_line h (password cfg)
-      banner <- recv_line h -- "The Sports Network"
-      banner `deepseq` return ()
+      _ <- recv_line h -- "The Sports Network"
+      return ()
   where
     username_prompt = "Username: "
     password_prompt = "Password: "
@@ -124,19 +120,34 @@ log_in cfg h = do
     recv_prompt :: Handle -> IO String
     recv_prompt = recv_chars 10
 
-connect_and_loop :: Configuration -> IO ()
-connect_and_loop cfg =
+connect_and_loop :: Configuration -> String -> IO ()
+connect_and_loop cfg host = do
+  putStrLn $ "Connecting to " ++ host ++ "..."
   bracket acquire_handle release_handle action
+  return ()
   where
-    --acquire_handle = connectTo "feed1.sportsnetwork.com" (PortNumber 4500)
-    acquire_handle = connectTo "feed2.sportsnetwork.com" (PortNumber 4500)
-    --acquire_handle = connectTo "127.0.0.1" (PortNumber 13337)
+    five_seconds :: Int
+    five_seconds = 5000000
+
+    acquire_handle = connectTo host (PortNumber 4500)
     release_handle = hClose
     action h = do
       -- No buffering anywhere.
       hSetBuffering h NoBuffering
-      log_in cfg h
-      loop cfg h []
+
+      -- The feed is often unresponsive after we send out username. It
+      -- happens in a telnet session, too (albeit less frequently?),
+      -- so there might be a bug on their end.
+      --
+      -- If we dump the packets with tcpdump, it looks like their
+      -- software is getting confused: they send us some XML in
+      -- the middle of the log-in procedure. In any case, the easiest
+      -- fix is to disconnect and try again.
+      --
+      login_worked <- timeout five_seconds $ log_in cfg h
+      case login_worked of
+        Nothing -> putStrLn "Login timed out (5s)."
+        Just _ ->  loop cfg h []
 
 
 -- | A wrapper around threadDelay which takes seconds instead of
@@ -156,6 +167,10 @@ main = do
   -- prefering the command-line ones.
   let opt_config = rc_cfg <> cmd_cfg
 
+  -- This is necessary because if the user specifies an empty list of
+  -- hostnames in e.g. the config file, we want to bail rather than
+  -- fall back on the default list (which gets merged from a
+  -- Configuration below).
   when (null $ get_feed_hosts (OC.feed_hosts opt_config)) $ do
     report_error "ERROR: no feed hosts supplied."
     exitWith (ExitFailure exit_no_feed_hosts)
@@ -175,6 +190,13 @@ main = do
   hSetBuffering stderr NoBuffering
   hSetBuffering stdout NoBuffering
 
-  forever $ do
-    catchIOError (connect_and_loop cfg) (report_error . show)
-    thread_sleep 10 -- Wait 10s before attempting to reconnect.
+  round_robin cfg 0
+
+  where
+    round_robin :: Configuration -> Int -> IO ()
+    round_robin cfg feed_host_idx = do
+      let hosts = get_feed_hosts $ feed_hosts cfg
+      let host = hosts !! feed_host_idx
+      catchIOError (connect_and_loop cfg host) (report_error . show)
+      thread_sleep 10 -- Wait 10s before attempting to reconnect.
+      round_robin cfg $ (feed_host_idx + 1) `mod` (length hosts)