Archived
1
0
Fork 0

Rebooting now works great, thanks to the communication channel preservation.

This commit is contained in:
Julien Dessaux 2009-08-23 23:07:43 +02:00
parent dfd0b3dcd7
commit d4103e3e18
3 changed files with 17 additions and 15 deletions

View file

@ -49,8 +49,8 @@ isConnected (Bot bot) ircServer = ircServer `M.member` bot
saveServerState :: Handle -> IrcServer -> Bot -> Bot
saveServerState handle ircServer x@(Bot bot) =
if ircServer `M.member` bot then x
else (Bot $ M.insert ircServer handle bot)
else (Bot $ M.insert ircServer handle bot)
saveServersStates :: [(IrcServer,Handle)] -> Bot -> Bot
saveServersStates liste bot = foldr (\(ircServer,handle) bot' -> saveServerState handle ircServer bot') bot liste
saveServersStates :: [(IrcServer, Handle)] -> Bot -> Bot
saveServersStates liste bot = foldl (\bot' (ircServer, handle) -> saveServerState handle ircServer bot') bot liste

View file

@ -14,28 +14,29 @@ import qualified Config as C
import Hsbot.Core
import Hsbot.IRC
type Reboot = (Module -> Bot -> IO ())
type Reboot = (Module -> Bot -> (Chan IrcLine) -> IO ())
-- | Bot's first main entry point
imain :: Module -> Reboot -> IO ()
imain modul' reboot = imain' modul' reboot newbot
imain modul' reboot = do
chan <- newChan :: IO (Chan IrcLine)
imain' modul' reboot newbot chan
-- | Bot's main entry point
imain' :: Module -> Reboot -> Bot -> IO ()
imain' modul' reboot bot = do
imain' :: Module -> Reboot -> Bot -> (Chan IrcLine) -> IO ()
imain' modul' reboot bot chan = do
-- The chan passing to reboot (or another way to keep it) is still missing
putStrLn "Connecting servers..."
let newServers = filter (not . isConnected bot) (ircServers C.config)
putStrLn $ "Connecting servers : " ++ show (map address newServers)
newServers' <- mapM connectServer newServers
putStrLn "Joining channels..."
putStrLn $ "Joining channels : " ++ show (map channels newServers)
mapM_ initServer newServers'
putStrLn "Spawning threads..."
let bot' = saveServersStates newServers' bot
Bot x = bot'
chan <- newChan :: IO (Chan IrcLine)
mapM_ (forkIO . listener chan) (M.toList x)
mapM_ (forkIO . listener chan) newServers' -- (M.toList x)
bot'' <- monitor chan bot'
reboot modul' bot''
reboot modul' bot'' chan
-- | Bot main loop, monitors the threads states and handle reboot
monitor :: (Chan IrcLine) -> Bot -> IO Bot

View file

@ -1,4 +1,5 @@
module Main where
import System.Exit
import System.Plugins
@ -27,8 +28,8 @@ main = do
imain modul' reboot
-- | Dynamic rebooting function
reboot :: Module -> a -> IO ()
reboot modul' state = do
reboot :: Module -> a -> b -> IO ()
reboot modul' state chan = do
mkstat <- makeAll "Hsbot.hs" ghcargs
case mkstat of
MakeSuccess _ _ -> do
@ -37,7 +38,7 @@ reboot modul' state = do
case ldstat of
LoadSuccess modul'' imain' -> do
putStrLn "REBOOT: Successful recompilation & reloading, rebooting..."
imain' modul'' reboot state
imain' modul'' reboot state chan
LoadFailure e -> fatality e
MakeFailure e -> fatality e
where