From 8edc1eccac63fbe2fcb7d7e3eab76cb8646df9a4 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 29 Dec 2010 12:24:43 +0000 Subject: requester gets now stored with commands response and timeout handlers get called now git-svn-id: https://svn.spreadspace.org/gcsd/trunk@91 ac14a137-c7f1-4531-abe0-07747231d213 --- TODO | 2 +- examples/basic_mode.lua | 4 ++-- src/command_queue.lua | 12 ++++++++++-- src/command_table.lua | 8 ++++---- src/modules/debug_shell.lua | 10 +++++++--- src/modules/dummy.lua | 2 +- src/modules/exec.lua | 4 ++-- src/modules/stdio.lua | 4 ++-- src/modules/stdout.lua | 2 +- src/modules/tcp_connect.lua | 4 ++-- src/modules/tcp_listen.lua | 4 ++-- src/response_table.lua | 2 +- 12 files changed, 35 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index cee0f00..0b6206f 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,5 @@ Core: - * send response to requestor (store requestor client in command_queue) + * debug/test command and response handling * out-of-order responses (send to all listeners?, all clients? basic mode?) * add listener tables (for requests, responses and other messages) * config-parser (config file and improve parser for modules config) diff --git a/examples/basic_mode.lua b/examples/basic_mode.lua index 97cccd1..9f9b8ad 100644 --- a/examples/basic_mode.lua +++ b/examples/basic_mode.lua @@ -1,8 +1,8 @@ local commands = { - ["%w+%s*\n"] = function(match) + ["%w+%s*\n"] = function(match, requester) command_queue:enqueue(string.match(match , "(%w+)%s*")) end, - [".*\n"] = function(match) end + [".*\n"] = function(match, requester) end } local responses = { diff --git a/src/command_queue.lua b/src/command_queue.lua index 7f95d9b..861ccfc 100644 --- a/src/command_queue.lua +++ b/src/command_queue.lua @@ -35,13 +35,14 @@ command_queue = {} command_queue.commands = {} command_queue.current = nil -function command_queue:enqueue(command, expected_response, timeout) +function command_queue:enqueue(command, expected_response, requestor, timeout) if(not command) then log.printf(log.INFO, "ignoring empty command") end local new_cmd = {} new_cmd.command = command new_cmd.sent = false + new_cmd.requester = requester if(expected_response and module_list.output.class.properties.type == defines.OUT_MODULE) then log.printf(log.WARNING, "output module doesn't support responses, ignoring expected response"); else @@ -96,12 +97,19 @@ function command_queue:check_timeout() if(timer.cmp(timer.now(), self.current.expiration_time) == 1) then log.printf(log.NOTICE, "command timed out") + if(self.current.requester) then + self.current.requester:process_timeout() + end self:command_completed() end end -function command_queue:command_completed() +function command_queue:command_completed(response) if(self.current == nil) then return end + + if(response and self.current.requester) then + self.current.requester:process_response(response) + end table.remove(self.commands, 1) self.current = nil diff --git a/src/command_table.lua b/src/command_table.lua index e747729..384ab50 100644 --- a/src/command_table.lua +++ b/src/command_table.lua @@ -32,10 +32,10 @@ command_table = {} command_table.regex_to_handler = { - ["%w+%s*\n"] = function(match) + ["%w+%s*\n"] = function(match, requester) command_queue:enqueue(string.match(match , "(%w+)%s*")) end, - [".*\n"] = function(match) end + [".*\n"] = function(match, requester) end } function command_table:register_handler_table(handler_table) @@ -46,14 +46,14 @@ function command_table:register_handler_table(handler_table) log.printf(log.DEBUG, "loaded new command handler table") end -function command_table:dispatch(command_buffer) +function command_table:dispatch(command_buffer, requester) local init = 1 repeat local had_match = false for regex, handler in pairs(self.regex_to_handler) do local match = string.match(command_buffer, "^" .. regex, init) if match then - handler(match) + handler(match, requester) had_match = true init = init + string.len(match) break diff --git a/src/modules/debug_shell.lua b/src/modules/debug_shell.lua index 9c3b605..b2cd4a1 100644 --- a/src/modules/debug_shell.lua +++ b/src/modules/debug_shell.lua @@ -100,8 +100,12 @@ function debug_shell:new(config, runtype) client.module_instance = inst client.addr = addr client.name = inst.name .. "#" .. tcp.endtostring(addr) - function client:process_response() end - function client:process_timeout() end + function client:process_response(response) + client_handle.out_buffer = client_handle.out_buffer .. "received response: '" .. response .. "'\n" + end + function client:process_timeout() + client_handle.out_buffer = client_handle.out_buffer .. "command timed out\n" + end function client:get_read_handles() return { client_handle } end @@ -217,7 +221,7 @@ function debug_shell:exec_cmd(handle, buffer) if(not param or param == '') then handle.out_buffer = handle.out_buffer .. "Error: please specify a command\n" else handle.out_buffer = handle.out_buffer .. "enqueing command: " .. param .. "\n" - command_queue:enqueue(param, "ok\n", 2500) + command_queue:enqueue(param, "ok\n", handle.client_instance, 2500) end else handle.out_buffer = handle.out_buffer .. "unknown command\n" diff --git a/src/modules/dummy.lua b/src/modules/dummy.lua index 501e52e..b91ddf3 100644 --- a/src/modules/dummy.lua +++ b/src/modules/dummy.lua @@ -56,7 +56,7 @@ function dummy:new(config, runtype) local client = {} client.module_instance = inst client.name = inst.name .. "#0" - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return handle end function client:get_write_handles() return {} end diff --git a/src/modules/exec.lua b/src/modules/exec.lua index 6e56069..9487729 100644 --- a/src/modules/exec.lua +++ b/src/modules/exec.lua @@ -83,7 +83,7 @@ function exec:new(config, runtype) self.in_buffer = self.in_buffer .. buffer if(inst.config.runtype == defines.IN_MODULE) then - self.in_buffer = command_table:dispatch(self.in_buffer) + self.in_buffer = command_table:dispatch(self.in_buffer, self.client_instance) else self.in_buffer = response_table:dispatch(self.in_buffer) end @@ -116,7 +116,7 @@ function exec:new(config, runtype) client.module_instance = inst client.name = inst.name .. "#0" client.pid = child.pid - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return { in_handle } diff --git a/src/modules/stdio.lua b/src/modules/stdio.lua index 9392eb2..a1c955f 100644 --- a/src/modules/stdio.lua +++ b/src/modules/stdio.lua @@ -61,7 +61,7 @@ function stdio:new(config, runtype) self.in_buffer = self.in_buffer .. buffer if(inst.config.runtype == defines.IN_MODULE) then - self.in_buffer = command_table:dispatch(self.in_buffer) + self.in_buffer = command_table:dispatch(self.in_buffer, self.client_instance) else self.in_buffer = response_table:dispatch(self.in_buffer) end @@ -93,7 +93,7 @@ function stdio:new(config, runtype) local client = {} client.module_instance = inst client.name = inst.name .. "#0" - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return { in_handle } diff --git a/src/modules/stdout.lua b/src/modules/stdout.lua index 1ae48fe..740eed2 100644 --- a/src/modules/stdout.lua +++ b/src/modules/stdout.lua @@ -69,7 +69,7 @@ function stdout:new(config, runtype) local client = {} client.module_instance = inst client.name = inst.name .. "#0" - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return {} end function client:get_write_handles() diff --git a/src/modules/tcp_connect.lua b/src/modules/tcp_connect.lua index 9150c21..7d415bb 100644 --- a/src/modules/tcp_connect.lua +++ b/src/modules/tcp_connect.lua @@ -70,7 +70,7 @@ function tcp_connect:new(config, runtype) self.in_buffer = self.in_buffer .. buffer if(inst.config.runtype == defines.IN_MODULE) then - self.in_buffer = command_table:dispatch(self.in_buffer) + self.in_buffer = command_table:dispatch(self.in_buffer, self.client_instance) else self.in_buffer = response_table:dispatch(self.in_buffer) end @@ -94,7 +94,7 @@ function tcp_connect:new(config, runtype) local client = {} client.module_instance = inst client.name = inst.name .. "#0" - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return { handle } diff --git a/src/modules/tcp_listen.lua b/src/modules/tcp_listen.lua index 09f3400..aabb5be 100644 --- a/src/modules/tcp_listen.lua +++ b/src/modules/tcp_listen.lua @@ -85,7 +85,7 @@ function tcp_listen:new(config, runtype) self.in_buffer = self.in_buffer .. buffer if(inst.config.runtype == defines.IN_MODULE) then - self.in_buffer = command_table:dispatch(self.in_buffer) + self.in_buffer = command_table:dispatch(self.in_buffer, self.client_instance) else self.in_buffer = response_table:dispatch(self.in_buffer) end @@ -110,7 +110,7 @@ function tcp_listen:new(config, runtype) client.module_instance = inst client.addr = addr client.name = inst.name .. "#" .. tcp.endtostring(addr) - function client:process_response() end + function client:process_response(response) end function client:process_timeout() end function client:get_read_handles() return { client_handle } diff --git a/src/response_table.lua b/src/response_table.lua index 42fb05b..f178d9c 100644 --- a/src/response_table.lua +++ b/src/response_table.lua @@ -53,7 +53,7 @@ function response_table:dispatch(response_buffer) if match then log.printf(log.DEBUG, "got expected response ('%s'), command completed", match) -- TODO: send expected response to original requestor - command_queue:command_completed() + command_queue:command_completed(match) init = init + string.len(match) end end -- cgit v1.2.3