From 6bd8edade1fa4bf4af247b555d473f2629f3b2a8 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 9 Nov 2010 13:55:06 +0000 Subject: modules can now be removed by type git-svn-id: https://svn.spreadspace.org/gcsd/trunk@19 ac14a137-c7f1-4531-abe0-07747231d213 --- src/main_loop.lua | 3 +-- src/module_list.lua | 17 +++++++++++++++++ src/modules/debug_shell.lua | 13 ++++++++++--- src/modules/dummy.lua | 2 ++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main_loop.lua b/src/main_loop.lua index 6b0e912..0abb03f 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -89,8 +89,7 @@ function main_loop(opt) return_value = 2 break elseif(ret == defines.KILL_MODULE_TYPE) then - -- TODO: remove all modules of same type as this and - -- unload module + module_list:unregister_by_type(input.client_instance.module_instance.type) elseif(ret == defines.KILL_MODULE) then module_list:unregister(input.client_instance.module_instance) elseif(ret == defines.KILL_CLIENT) then diff --git a/src/module_list.lua b/src/module_list.lua index a41b15e..aa644ee 100644 --- a/src/module_list.lua +++ b/src/module_list.lua @@ -51,6 +51,7 @@ function module_list:init(opt) table.insert(self.modules, dummy:new({})) + table.insert(self.modules, debug_shell:new({["host"]="127.0.0.1", ["port"]="9001"})) if(opt.debug) then table.insert(self.modules, debug_shell:new({["host"]="127.0.0.1", ["port"]="9000"})) end @@ -67,6 +68,22 @@ function module_list:unregister(module) end end +function module_list:unregister_by_type(type) + local free_list = {} + + for i, m in ipairs(self.modules) do + if(m.type == type) then + table.insert(free_list, 1, i) + end + end + + for _, i in ipairs(free_list) do + log.printf(log.INFO, "removing module: " .. self.modules[i].name) + self.modules[i]:cleanup() + table.remove(self.modules, i) + end +end + function module_list:cleanup() for _, module in ipairs(self.modules) do log.printf(log.INFO, "removing module: " .. module.name) diff --git a/src/modules/debug_shell.lua b/src/modules/debug_shell.lua index a9ba707..70b242a 100644 --- a/src/modules/debug_shell.lua +++ b/src/modules/debug_shell.lua @@ -34,10 +34,12 @@ local socket = require("socket") local defines = require("defines") local debug_shell = {} -debug_shell.properties = { input=true, output=true, max_instances=1, module_type_name="debug_shell" } +debug_shell.properties = { input=true, output=false, max_instances=-1, module_type_name="debug_shell" } debug_shell.next_id = 0 function debug_shell:new(config) local inst = {} + inst.type = self.properties.module_type_name + inst.config = config if(config.name == nil or config.name == "") then inst.name = self.properties.module_type_name .. self.next_id self.next_id = self.next_id + 1 @@ -200,8 +202,12 @@ function debug_shell:exec_cmd(socket) ret = defines.KILL_CLIENT elseif(cmd == 'disable') then if(sep and sep ~= "") then socket.out_buffer = socket.out_buffer .. "unknown command\n" end - log.printf(log.NOTICE, "debug shell: disabling debug shell and close all connections") + log.printf(log.NOTICE, "debug shell: disabling this debug shell instance and close all connections") ret = defines.KILL_MODULE + elseif(cmd == 'disableall') then + if(sep and sep ~= "") then socket.out_buffer = socket.out_buffer .. "unknown command\n" end + log.printf(log.NOTICE, "debug shell: disabling all debug shell instances and close all connections") + ret = defines.KILL_MODULE_TYPE elseif(cmd == 'terminate') then if(sep and sep ~= "") then socket.out_buffer = socket.out_buffer .. "unknown command\n" end log.printf(log.NOTICE, "debug shell: terminate command received") @@ -211,7 +217,8 @@ function debug_shell:exec_cmd(socket) socket.out_buffer = socket.out_buffer .. "! execute lua code\n" .. "quit quit this debug session\n" .. "ping[ ] echo request (response will be 'pong[ ]')\n" .. - "disable close debug shell and close all connections\n" .. + "disable close this debug shell instance and close all connections\n" .. + "disableall close all debug shell instances and close all connections\n" .. "terminate terminate the daemon\n" elseif(cmd == 'ping') then if(sep == ' ') then socket.out_buffer = socket.out_buffer .. "pong " .. (param or "") .. "\n" diff --git a/src/modules/dummy.lua b/src/modules/dummy.lua index 5114e16..ea1679d 100644 --- a/src/modules/dummy.lua +++ b/src/modules/dummy.lua @@ -35,6 +35,8 @@ dummy.properties = { input=true, output=true, max_instances=-1, module_type_name dummy.next_id = 0 function dummy:new(config) local inst = {} + inst.type = self.properties.module_type_name + inst.config = config if(config.name == nil or config.name == "") then inst.name = self.properties.module_type_name .. self.next_id self.next_id = self.next_id + 1 -- cgit v1.2.3