From 908571214ee1b8aeb73c629203d80755d6192474 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 24 Nov 2010 20:27:08 +0000 Subject: debugged dispatch table added basic mode added runtype to modules git-svn-id: https://svn.spreadspace.org/gcsd/trunk@61 ac14a137-c7f1-4531-abe0-07747231d213 --- examples/basic_mode.lua | 6 ++++++ examples/simple_dispatcher.lua | 6 ------ src/command_queue.lua | 3 +++ src/dispatch_table.lua | 9 ++++++--- src/main_loop.lua | 4 ++-- src/module_list.lua | 16 ++++++++++++++-- src/modules/debug_shell.lua | 2 +- src/modules/dummy.lua | 2 +- src/modules/exec.lua | 13 ++++++++----- src/modules/stdio.lua | 13 ++++++++----- src/modules/stdout.lua | 2 +- 11 files changed, 50 insertions(+), 26 deletions(-) create mode 100644 examples/basic_mode.lua delete mode 100644 examples/simple_dispatcher.lua diff --git a/examples/basic_mode.lua b/examples/basic_mode.lua new file mode 100644 index 0000000..04c16df --- /dev/null +++ b/examples/basic_mode.lua @@ -0,0 +1,6 @@ +return { + ["%w+%s*\n"] = function(match) + command_queue:enqueue(string.match(match , "(%w+)%s*")) + end, + [".*\n"] = function(match) end +} diff --git a/examples/simple_dispatcher.lua b/examples/simple_dispatcher.lua deleted file mode 100644 index 4c935fe..0000000 --- a/examples/simple_dispatcher.lua +++ /dev/null @@ -1,6 +0,0 @@ -return { - ["%w+%s*\n"] = function(match) - local cmd = string.match("(%w+)%s*", match) - command_queue:enqueue_command(cmd) - end -} diff --git a/src/command_queue.lua b/src/command_queue.lua index a7ed70b..d63fa26 100644 --- a/src/command_queue.lua +++ b/src/command_queue.lua @@ -38,6 +38,9 @@ command_queue.commands = {} command_queue.current = nil function command_queue:enqueue(command, expected_response, timeout) + if(not command) then + log.printf(log.INFO, "ignoring empty command") + end local new_cmd = {} new_cmd.command = command new_cmd.sent = false diff --git a/src/dispatch_table.lua b/src/dispatch_table.lua index 7783113..1b74624 100644 --- a/src/dispatch_table.lua +++ b/src/dispatch_table.lua @@ -32,7 +32,10 @@ dispatch_table = {} dispatch_table.regex_to_handler = { - -- TODO basic mode + ["%w+%s*\n"] = function(match) + command_queue:enqueue(string.match(match , "(%w+)%s*")) + end, + [".*\n"] = function(match) end } function dispatch_table:load_handler_table_file(filename) @@ -42,9 +45,9 @@ function dispatch_table:load_handler_table_file(filename) log.printf(log.ERROR, "filename must be of type 'string' and denote a valid path") return defines.KILL_DAEMON end - local chunk = loadfile(filename) + local chunk, err = loadfile(filename) if (not chunk) then - log.printf(log.ERROR, "failed to load lua-file '%s'", filename) + log.printf(log.ERROR, "failed to load dispatch table: %s", err) return defines.KILL_DAEMON end self:register_handler_table(chunk()) diff --git a/src/main_loop.lua b/src/main_loop.lua index 42cc74c..ee85c4e 100644 --- a/src/main_loop.lua +++ b/src/main_loop.lua @@ -80,9 +80,9 @@ function main_loop(opt) else return_value = module_list:init(opt) if(return_value == defines.KILL_DAEMON) then - return_value = -1 + return_value = -1 else - return_value = 0 + return_value = 0 end end diff --git a/src/module_list.lua b/src/module_list.lua index 26f2ff0..eb12b1f 100644 --- a/src/module_list.lua +++ b/src/module_list.lua @@ -58,20 +58,32 @@ module_list.output = nil function module_list:init(opt) local class, config = self:parse_config(opt.cmd_out) if(not class) then return defines.KILL_DAEMON end + if(self.classes[class].properties.type ~= defines.OUT_MODULE and + self.classes[class].properties.type ~= defines.INOUT_MODULE) then + log.printf(log.ERROR, "module class '%s' cannot be used as output module", class); + return defines.KILL_DAEMON + end log.printf(log.DEBUG, "output module class='%s'", class) for k, v in pairs(config) do log.printf(log.DEBUG, " config['%s'] = '%s'", k, v) end - self.output = self.classes[class]:new(config) + self.output = self.classes[class]:new(config, defines.OUT_MODULE) for idx, input in ipairs(opt.cmd_ins) do local class, config = self:parse_config(input) if(not class) then return defines.KILL_DAEMON end + if(self.classes[class].properties.type ~= defines.IN_MODULE and + self.classes[class].properties.type ~= defines.INOUT_MODULE and + self.classes[class].properties.type ~= defines.MISC_MODULE) then + log.printf(log.ERROR, "module class '%s' cannot be used as input module", class); + return defines.KILL_DAEMON + end + log.printf(log.DEBUG, "input[%d] module class='%s'", idx, class) for k, v in pairs(config) do log.printf(log.DEBUG, " config['%s'] = '%s'", k, v) end - table.insert(self.inputs, self.classes[class]:new(config)) + table.insert(self.inputs, self.classes[class]:new(config, defines.IN_MODULE)) end return defines.OK diff --git a/src/modules/debug_shell.lua b/src/modules/debug_shell.lua index 652be45..1a28acd 100644 --- a/src/modules/debug_shell.lua +++ b/src/modules/debug_shell.lua @@ -40,7 +40,7 @@ debug_shell.defaults = { host = "localhost", port = 9000 } debug_shell.next_id = 0 -- create new instance of debug shell module class -function debug_shell:new(config) +function debug_shell:new(config, runtype) local inst = {} inst.class = self.properties.name inst.config = config diff --git a/src/modules/dummy.lua b/src/modules/dummy.lua index 62fe6ec..e8d534e 100644 --- a/src/modules/dummy.lua +++ b/src/modules/dummy.lua @@ -38,7 +38,7 @@ dummy.properties = { type=defines.INOUT_MODULE, name="dummy", max_instances=-1 } dummy.next_id = 0 -- create new instance of dummy module class -function dummy:new(config) +function dummy:new(config, runtype) local inst = {} inst.class = self.properties.name inst.config = config diff --git a/src/modules/exec.lua b/src/modules/exec.lua index 09a6335..61eb34c 100644 --- a/src/modules/exec.lua +++ b/src/modules/exec.lua @@ -39,10 +39,11 @@ exec.defaults = { script = "exec.sh" } exec.next_id = 0 -- create new instance of exec module class -function exec:new(config) +function exec:new(config, runtype) local inst = {} inst.class = self.properties.name inst.config = config + inst.config.runtype = runtype if(config.name == nil or config.name == "") then inst.name = self.properties.name .. self.next_id self.next_id = self.next_id + 1 @@ -84,10 +85,12 @@ function exec:new(config) end self.in_buffer = self.in_buffer .. buffer - self.in_buffer = dispatch_table:dispatch(self.in_buffer) - - -- TODO: part of expected response handling - command_queue:command_completed() + if(inst.config.runtype == defines.IN_MODULE) then + self.in_buffer = dispatch_table:dispatch(self.in_buffer) + else + -- TODO: part of expected response handling + command_queue:command_completed() + end return defines.OK end diff --git a/src/modules/stdio.lua b/src/modules/stdio.lua index 4069a87..597386b 100644 --- a/src/modules/stdio.lua +++ b/src/modules/stdio.lua @@ -37,10 +37,11 @@ local stdio = {} stdio.properties = { type=defines.INOUT_MODULE, name="stdio", max_instances=1 } -- create new instance of stdio module class -function stdio:new(config) +function stdio:new(config, runtype) local inst = {} inst.class = self.properties.name inst.config = config + inst.config.runtype = runtype if(config.name == nil or config.name == "") then inst.name = self.properties.name else @@ -62,10 +63,12 @@ function stdio:new(config) end self.in_buffer = self.in_buffer .. buffer - self.in_buffer = dispatch_table:dispatch(self.in_buffer) - - -- TODO: part of expected response handling - command_queue:command_completed() + if(inst.config.runtype == defines.IN_MODULE) then + self.in_buffer = dispatch_table:dispatch(self.in_buffer) + else + -- TODO: part of expected response handling + command_queue:command_completed() + end return defines.OK end diff --git a/src/modules/stdout.lua b/src/modules/stdout.lua index 98a2212..b407f80 100644 --- a/src/modules/stdout.lua +++ b/src/modules/stdout.lua @@ -37,7 +37,7 @@ local stdout = {} stdout.properties = { type=defines.OUT_MODULE, name="stdout", max_instances=1 } -- create new instance of stdout module class -function stdout:new(config) +function stdout:new(config, runtype) local inst = {} inst.class = self.properties.name inst.config = config -- cgit v1.2.3