Cleaned further the bot updates handling.
This commit is contained in:
parent
451564ef5b
commit
c506c58e92
5 changed files with 53 additions and 49 deletions
|
@ -22,46 +22,48 @@ hsbot config = do
|
||||||
startTime <- getCurrentTime
|
startTime <- getCurrentTime
|
||||||
putStrLn "[Hsbot] Opening communication channel... "
|
putStrLn "[Hsbot] Opening communication channel... "
|
||||||
chan <- newChan :: IO (Chan BotMsg)
|
chan <- newChan :: IO (Chan BotMsg)
|
||||||
mvar <- newMVar "" :: IO (MVar String)
|
mvar <- newMVar M.empty :: IO (MVar BotResumeData)
|
||||||
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
putStrLn "[Hsbot] Spawning IrcBot plugins... "
|
||||||
botState <- execStateT spawnIrcPlugins BotState { botStartTime = startTime
|
botState <- execStateT spawnIrcPlugins BotState { botStartTime = startTime
|
||||||
, botPlugins = M.empty
|
, botPlugins = M.empty
|
||||||
, botChan = chan
|
, botChan = chan
|
||||||
, botConfig = config
|
, botConfig = config
|
||||||
, botMVar = mvar
|
, botResumeData = mvar }
|
||||||
, botResumeData = M.empty }
|
|
||||||
putStrLn "[Hsbot] Entering main loop... "
|
putStrLn "[Hsbot] Entering main loop... "
|
||||||
(reboot, botState') <- (runStateT botLoop botState) `catch` (\(_ :: IOException) -> return (False, botState))
|
(status, botState') <- runLoop botState
|
||||||
resumeData <- takeMVar mvar
|
resumeData <- takeMVar mvar
|
||||||
if reboot
|
if status == BotReboot
|
||||||
then resumeHsbot botState' resumeData
|
then resumeHsbot botState' resumeData
|
||||||
else return ()
|
else return ()
|
||||||
|
where
|
||||||
|
runLoop :: BotState -> IO (BotStatus, BotState)
|
||||||
|
runLoop botState = do
|
||||||
|
(status, botState') <- (runStateT botCore botState) `catch` (\(_ :: IOException) -> return (BotExit, botState))
|
||||||
|
case status of
|
||||||
|
BotContinue -> runLoop botState'
|
||||||
|
_ -> return (status, botState')
|
||||||
|
|
||||||
resumeHsbot :: BotState -> String -> IO ()
|
resumeHsbot :: BotState -> BotResumeData -> IO ()
|
||||||
resumeHsbot botState resumeData = do
|
resumeHsbot botState resumeData = do
|
||||||
print resumeData
|
print resumeData
|
||||||
|
|
||||||
-- | Run the bot main loop
|
-- | Run the bot main loop
|
||||||
botLoop :: Bot (Bool)
|
botCore :: Bot (BotStatus)
|
||||||
botLoop = do
|
botCore = do
|
||||||
chan <- gets botChan
|
chan <- gets botChan
|
||||||
msg <- liftIO $ readChan chan
|
msg <- liftIO $ readChan chan
|
||||||
case msg of
|
case msg of
|
||||||
InMsg _ -> botLoop
|
InMsg _ -> return BotContinue
|
||||||
OutMsg _ -> botLoop
|
OutMsg _ -> return BotContinue
|
||||||
IntMsg intMsg -> do
|
IntMsg intMsg -> processInternalMessage $ IntMsg intMsg
|
||||||
reboot <- processInternalMessage $ IntMsg intMsg
|
UpdMsg updMsg -> processUpdateMessage updMsg
|
||||||
reportUpdate
|
|
||||||
if not reboot
|
|
||||||
then botLoop
|
|
||||||
else return (True)
|
|
||||||
|
|
||||||
-- | Reports an update to the master bot
|
-- | Process an update command
|
||||||
reportUpdate :: Bot ()
|
processUpdateMessage :: ResumeMsg -> Bot (BotStatus)
|
||||||
reportUpdate = do
|
processUpdateMessage msg = do
|
||||||
bot <- get
|
resumeData <- gets botResumeData
|
||||||
let mvar = botMVar bot
|
let from = resMsgFrom msg
|
||||||
stuff = show $ botResumeData bot
|
stuff = resMsgData msg
|
||||||
_ <- liftIO $ swapMVar mvar stuff
|
liftIO $ modifyMVar_ resumeData (\oldData -> return $ M.insert from stuff oldData)
|
||||||
return ()
|
return BotContinue
|
||||||
|
|
||||||
|
|
|
@ -145,11 +145,8 @@ reportUpdate :: IrcBot ()
|
||||||
reportUpdate = do
|
reportUpdate = do
|
||||||
ircbot <- get
|
ircbot <- get
|
||||||
let masterChan = ircBotMasterChan ircbot
|
let masterChan = ircBotMasterChan ircbot
|
||||||
msg = IntMsg $ Msg { msgType = "UPDATE"
|
msg = UpdMsg $ ResMsg { resMsgFrom = ircConfigName $ ircBotConfig ircbot
|
||||||
, msgFrom = ircConfigName $ ircBotConfig ircbot
|
, resMsgData = ircBotResumeData ircbot }
|
||||||
, msgTo = "CORE"
|
|
||||||
, msgStuff = show $ ircBotResumeData ircbot
|
|
||||||
}
|
|
||||||
liftIO $ writeChan masterChan msg
|
liftIO $ writeChan masterChan msg
|
||||||
|
|
||||||
-- | Process a reboot command
|
-- | Process a reboot command
|
||||||
|
|
|
@ -32,7 +32,7 @@ data IrcBotState = IrcBotState
|
||||||
, ircBotConfig :: IrcConfig -- The starting configuration
|
, ircBotConfig :: IrcConfig -- The starting configuration
|
||||||
, ircBotReaderThreadId :: ThreadId -- the thread that process inputs from the socket
|
, ircBotReaderThreadId :: ThreadId -- the thread that process inputs from the socket
|
||||||
, ircBotMasterReaderThreadId :: ThreadId -- the thread that process inputs from the master bot
|
, ircBotMasterReaderThreadId :: ThreadId -- the thread that process inputs from the master bot
|
||||||
, ircBotResumeData :: M.Map String String -- the necessary data to resume the bot's operations on reboot
|
, ircBotResumeData :: ResumeData -- the necessary data to resume the bot's operations on reboot
|
||||||
}
|
}
|
||||||
|
|
||||||
-- | The IrcServer monad
|
-- | The IrcServer monad
|
||||||
|
|
|
@ -9,7 +9,7 @@ import Hsbot.PluginUtils
|
||||||
import Hsbot.Types
|
import Hsbot.Types
|
||||||
|
|
||||||
-- | Processes an internal message
|
-- | Processes an internal message
|
||||||
processInternalMessage :: BotMsg -> Bot (Bool)
|
processInternalMessage :: BotMsg -> Bot (BotStatus)
|
||||||
processInternalMessage (IntMsg msg)
|
processInternalMessage (IntMsg msg)
|
||||||
| msgTo msg == "CORE" = processCoreMessage msg
|
| msgTo msg == "CORE" = processCoreMessage msg
|
||||||
| otherwise = do
|
| otherwise = do
|
||||||
|
@ -17,22 +17,12 @@ processInternalMessage (IntMsg msg)
|
||||||
case M.lookup (msgTo msg) plugins of
|
case M.lookup (msgTo msg) plugins of
|
||||||
Just (plugin, _) -> sendToPlugin (IntMsg msg) plugin
|
Just (plugin, _) -> sendToPlugin (IntMsg msg) plugin
|
||||||
Nothing -> return ()
|
Nothing -> return ()
|
||||||
return False
|
return BotContinue
|
||||||
processInternalMessage _ = return (False)
|
processInternalMessage _ = return BotContinue
|
||||||
|
|
||||||
processCoreMessage :: Msg -> Bot (Bool)
|
processCoreMessage :: Msg -> Bot (BotStatus)
|
||||||
processCoreMessage msg = do
|
processCoreMessage msg = do
|
||||||
case msgType msg of
|
case msgType msg of
|
||||||
"UPDATE" -> processUpdateCommand msg
|
"REBOOT" -> return BotReboot
|
||||||
_ -> return ()
|
_ -> return BotContinue
|
||||||
return $ (msgType msg) == "REBOOT"
|
|
||||||
|
|
||||||
-- | Process an update command
|
|
||||||
processUpdateCommand :: Msg -> Bot ()
|
|
||||||
processUpdateCommand msg = do
|
|
||||||
bot <- get
|
|
||||||
let oldData = botResumeData bot
|
|
||||||
from = msgFrom msg
|
|
||||||
stuff = msgStuff msg
|
|
||||||
put $ bot { botResumeData = M.insert from stuff oldData }
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
module Hsbot.Types
|
module Hsbot.Types
|
||||||
( Bot
|
( Bot
|
||||||
, BotMsg (..)
|
, BotMsg (..)
|
||||||
|
, BotResumeData
|
||||||
, BotState (..)
|
, BotState (..)
|
||||||
|
, BotStatus (..)
|
||||||
, Msg (..)
|
, Msg (..)
|
||||||
, Plugin
|
, Plugin
|
||||||
, PluginState (..)
|
, PluginState (..)
|
||||||
|
, ResumeData
|
||||||
|
, ResumeMsg (..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Control.Concurrent
|
import Control.Concurrent
|
||||||
|
@ -24,10 +28,16 @@ data BotState = BotState
|
||||||
, botPlugins :: M.Map String (PluginState, ThreadId) -- Loaded plugins
|
, botPlugins :: M.Map String (PluginState, ThreadId) -- Loaded plugins
|
||||||
, botChan :: Chan BotMsg -- the bot's communication channel
|
, botChan :: Chan BotMsg -- the bot's communication channel
|
||||||
, botConfig :: Config -- the bot's starting config
|
, botConfig :: Config -- the bot's starting config
|
||||||
, botMVar :: MVar String -- the place where to put resume data
|
, botResumeData :: MVar BotResumeData -- the necessary data to resume the bot's operations on reboot
|
||||||
, botResumeData :: M.Map String String -- the necessary data to resume the bot's operations on reboot
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- | how we exit from the botLoop
|
||||||
|
data BotStatus = BotExit | BotReboot | BotContinue deriving (Eq)
|
||||||
|
|
||||||
|
-- | Types to factorise resume data
|
||||||
|
type ResumeData = M.Map String String
|
||||||
|
type BotResumeData = M.Map String ResumeData
|
||||||
|
|
||||||
-- | The Plugin monad
|
-- | The Plugin monad
|
||||||
type Plugin = StateT PluginState IO
|
type Plugin = StateT PluginState IO
|
||||||
|
|
||||||
|
@ -46,5 +56,10 @@ data Msg = Msg
|
||||||
, msgStuff :: String -- the message to be transfered
|
, msgStuff :: String -- the message to be transfered
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
data BotMsg = InMsg Msg | OutMsg Msg | IntMsg Msg deriving (Show)
|
data ResumeMsg = ResMsg
|
||||||
|
{ resMsgFrom :: String
|
||||||
|
, resMsgData :: ResumeData
|
||||||
|
} deriving (Show)
|
||||||
|
|
||||||
|
data BotMsg = InMsg Msg | OutMsg Msg | IntMsg Msg | UpdMsg ResumeMsg deriving (Show)
|
||||||
|
|
||||||
|
|
Reference in a new issue