Archived
1
0
Fork 0

Continue rewriting, found a problem in the way I kill plugins.

This commit is contained in:
Julien Dessaux 2010-02-04 23:34:15 +01:00
parent fd8d5faf5f
commit 416460886d
9 changed files with 48 additions and 25 deletions

View file

@ -67,7 +67,9 @@ processInternalCommand (InternalCmd intCmd) = do
plugins <- gets botPlugins plugins <- gets botPlugins
if intCmdTo intCmd == "CORE" if intCmdTo intCmd == "CORE"
then processCoreCommand intCmd then processCoreCommand intCmd
else sendToPlugin (InternalCmd intCmd) $ fromMaybe [] (M.lookup plugins (intCmdTo intCmd)) else case M.lookup (intCmdTo intCmd) plugins of
Just plugin -> sendToPlugin (InternalCmd intCmd) plugin
Nothing -> errorM $ "Invalid destination in message : " ++ (show intCmd)
processInternalCommand _ = return () processInternalCommand _ = return ()
-- | Processes a core command -- | Processes a core command
@ -76,8 +78,11 @@ processCoreCommand intCmd = do
let command' = intCmdCmd intCmd let command' = intCmdCmd intCmd
case command' of case command' of
"LOAD" -> loadPlugin $ intCmdMsg intCmd "LOAD" -> loadPlugin $ intCmdMsg intCmd
"RELOAD" -> reloadPlugin $ intCmdMsg intCmd
"UNLOAD" -> unloadPlugin $ intCmdMsg intCmd "UNLOAD" -> unloadPlugin $ intCmdMsg intCmd
"REGISTER" -> registerCommand (intCmdMsg intCmd) (intCmdFrom intCmd) "REGISTER" -> registerCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
"UNREGISTER" -> unregisterCommand (intCmdMsg intCmd) (intCmdFrom intCmd) "UNREGISTER" -> unregisterCommand (intCmdMsg intCmd) (intCmdFrom intCmd)
_ -> traceM $ inColor ("Invalid command : " ++ (show intCmd)) [31] _ -> traceM $ inColor ("Invalid command : " ++ (show intCmd)) [31]
bot' <- get
traceM $ show bot'

View file

@ -13,6 +13,7 @@ import System.IO
import System.Time (getClockTime) import System.Time (getClockTime)
import Hsbot.IRCParser import Hsbot.IRCParser
import Hsbot.Plugin
import Hsbot.Types import Hsbot.Types
import Hsbot.Utils import Hsbot.Utils
@ -32,11 +33,12 @@ connectServer server = do
return $ Bot server starttime handle [] M.empty chan threadId M.empty return $ Bot server starttime handle [] M.empty chan threadId M.empty
-- | Disconnect from the server -- | Disconnect from the server
disconnectServer :: Bot -> IO () -- IO Bot ? disconnectServer :: IrcBot ()
disconnectServer bot = do disconnectServer = do
killThread $ readerThreadId bot bot <- get
mapM_ (killThread . pluginThreadId . snd) (M.toList $ botPlugins bot) liftIO $ killThread $ readerThreadId bot
hClose $ botHandle bot mapM_ unloadPlugin (M.keys $ botPlugins bot)
liftIO $ hClose $ botHandle bot
return () return ()
-- | Socket reading loop -- | Socket reading loop

View file

@ -1,5 +1,6 @@
module Hsbot.IRCPlugin module Hsbot.IRCPlugin
( readMsg ( readMsg
, sendCommand
, sendRegisterCommand , sendRegisterCommand
, sendUnregisterCommand , sendUnregisterCommand
, writeMsg , writeMsg

View file

@ -15,8 +15,8 @@ import Hsbot.Types
imain :: IO () imain :: IO ()
imain = do imain = do
bot <- connectServer $ ircServer config bot <- connectServer $ ircServer config
(runStateT run bot) `catch` (const $ return ((), bot)) bot' <- (execStateT run bot) `catch` (const $ return bot)
disconnectServer bot evalStateT disconnectServer bot'
-- | The Bot monad main function -- | The Bot monad main function
run :: IrcBot () run :: IrcBot ()

View file

@ -1,10 +1,13 @@
module Hsbot.Plugin module Hsbot.Plugin
( loadPlugin ( loadPlugin
, sendToPlugin , sendToPlugin
, reloadPlugin
, unloadPlugin
) where ) where
import Control.Concurrent import Control.Concurrent
import Control.Concurrent.Chan import Control.Concurrent.Chan
import Control.Exception.Extensible
import Control.Monad.State import Control.Monad.State
import qualified Data.Map as M import qualified Data.Map as M
import System.IO import System.IO
@ -53,6 +56,12 @@ effectivelyLoadPlugin name serverChan = do
return Nothing return Nothing
return plugin return plugin
-- | Reloads a plugin
reloadPlugin :: String -> IrcBot ()
reloadPlugin name = do
unloadPlugin name
loadPlugin name
-- | Unloads a plugin -- | Unloads a plugin
unloadPlugin :: String -> IrcBot () unloadPlugin :: String -> IrcBot ()
unloadPlugin name = do unloadPlugin name = do
@ -61,17 +70,12 @@ unloadPlugin name = do
case M.lookup name oldPlugins of case M.lookup name oldPlugins of
Just plugin -> do Just plugin -> do
let newPlugins = M.delete name oldPlugins let newPlugins = M.delete name oldPlugins
liftIO $ killPlugin plugin -- TODO : forkIO to get this asynchronous and non blocking liftIO $ throwTo (pluginThreadId plugin) UserInterrupt -- TODO : fix this!
-- or let's see if closing one's chan kills him. --sendToPlugin (InternalCmd $ IntCmd "STOP" "CORE" name "") plugin
unloadAll $ pluginModule $ M.lookup name oldPlugins liftIO $ unloadAll $ pluginModule plugin
put $ bot { botPlugins = newPlugins } put $ bot { botPlugins = newPlugins }
Nothing -> return () Nothing -> return ()
-- | stop a plugin
killPlugin :: Plugin -> IO ()
killPlugin plugin = do
-- TODO : send stop, sleep and kill thread (if necessary) and remove its commands
-- | Sends a msg to a plugin -- | Sends a msg to a plugin
sendToPlugin :: BotMsg -> Plugin -> IrcBot () sendToPlugin :: BotMsg -> Plugin -> IrcBot ()
sendToPlugin msg plugin = do sendToPlugin msg plugin = do

View file

@ -34,9 +34,9 @@ traceM :: String -> IrcBot ()
traceM msg = liftIO $ trace msg traceM msg = liftIO $ trace msg
-- | Logs an error message -- | Logs an error message
error :: String -> IO () traceRed :: String -> IO ()
error msg = trace $ inColor msg [31] traceRed msg = trace $ inColor msg [31]
errorM :: String -> a () errorM :: String -> IrcBot ()
error msg = liftIO $ error msg errorM msg = liftIO $ traceRed msg

View file

@ -3,6 +3,7 @@ module Plugins.Core
) where ) where
import Control.Concurrent.Chan import Control.Concurrent.Chan
import Control.Monad.State
import Hsbot.IRCPlugin import Hsbot.IRCPlugin
import Hsbot.Types import Hsbot.Types
@ -18,9 +19,9 @@ mainCore serverChan chan = do
-- | The IrcPlugin monad main function -- | The IrcPlugin monad main function
run :: IrcPlugin () run :: IrcPlugin ()
run = do run = do
mapM_ sendRegisterCommand ["load", "unload"] mapM_ sendRegisterCommand ["load", "reload", "unload"]
runPlugin runPlugin
mapM_ sendUnregisterCommand ["load", "unload"] mapM_ sendUnregisterCommand ["load", "reload", "unload"]
runPlugin :: IrcPlugin () runPlugin :: IrcPlugin ()
runPlugin = forever $ do runPlugin = forever $ do
@ -33,6 +34,7 @@ runPlugin = forever $ do
"RUN" -> let stuff = words $ intCmdMsg intCmd "RUN" -> let stuff = words $ intCmdMsg intCmd
in case head stuff of in case head stuff of
"load" -> loadPlugin $ tail stuff "load" -> loadPlugin $ tail stuff
"reload" -> reloadPlugin $ tail stuff
"unload" -> unloadPlugin $ tail stuff "unload" -> unloadPlugin $ tail stuff
_ -> lift $ trace $ show intCmd -- TODO : help message _ -> lift $ trace $ show intCmd -- TODO : help message
_ -> lift $ trace $ show intCmd _ -> lift $ trace $ show intCmd
@ -43,6 +45,10 @@ runPlugin = forever $ do
loadPlugin :: [String] -> IrcPlugin () loadPlugin :: [String] -> IrcPlugin ()
loadPlugin pluginNames = mapM_ (sendCommand "LOAD" "CORE") pluginNames loadPlugin pluginNames = mapM_ (sendCommand "LOAD" "CORE") pluginNames
-- | The reload command
reloadPlugin :: [String] -> IrcPlugin ()
reloadPlugin pluginNames = mapM_ (sendCommand "RELOAD" "CORE") pluginNames
-- | The unload command -- | The unload command
unloadPlugin :: [String] -> IrcPlugin () unloadPlugin :: [String] -> IrcPlugin ()
unloadPlugin pluginNames = mapM_ (sendCommand "UNLOAD" "CORE") pluginNames unloadPlugin pluginNames = mapM_ (sendCommand "UNLOAD" "CORE") pluginNames

View file

@ -28,8 +28,9 @@ type QuoteBot a = StateT QuoteDB IO a
mainQuote :: Chan BotMsg -> Chan BotMsg -> IO () mainQuote :: Chan BotMsg -> Chan BotMsg -> IO ()
mainQuote serverChan chan = do mainQuote serverChan chan = do
let plugin = PluginInstance "Quote" serverChan chan let plugin = PluginInstance "Quote" serverChan chan
(runStateT run plugin) `catch` (const $ return ((), plugin)) plugin' <- (execStateT run plugin) `catch` (const $ return plugin)
return () putStrLn "graou"
evalStateT stop plugin'
-- | The IrcPlugin monad main function -- | The IrcPlugin monad main function
run :: IrcPlugin () run :: IrcPlugin ()
@ -37,6 +38,9 @@ run = do
-- TODO : init quote handling (sqlite + structure to handle temporary stuff) -- TODO : init quote handling (sqlite + structure to handle temporary stuff)
sendRegisterCommand "quote" sendRegisterCommand "quote"
runPlugin runPlugin
stop :: IrcPlugin ()
stop = do
sendUnregisterCommand "quote" sendUnregisterCommand "quote"
-- TODO : send cancel messages for all temporary stuff -- TODO : send cancel messages for all temporary stuff

3
TODO
View file

@ -1,7 +1,8 @@
:julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif :julien!~julien@ogu21.corp PRIVMSG #shbot :@quote graou snif
* fix unload plugins
* list plugins
* discard all trace with a color param and replace those with functions info/warn/error/debug * discard all trace with a color param and replace those with functions info/warn/error/debug
* unload plugin
* clean the plugin module * clean the plugin module
* kill threads * kill threads