diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 54bd3e51..e039888a 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -138,10 +138,14 @@ function skynet.starttime() end function skynet.exit() + skynet.send(".launcher","lua","REMOVE",skynet.self()) c.command("EXIT") end function skynet.kill(name) + if type(name) == "number" then + name = skynet.address(name) + end c.command("KILL",name) end @@ -245,7 +249,8 @@ local function dispatch_message(prototype, msg, sz, session, source, ...) end function skynet.newservice(name, ...) - local handle = skynet.call(".launcher", "lua" , "snlua", name, ...) + local param = table.concat({"snlua", name, ...}, " ") + local handle = skynet.call(".launcher", "text" , param) return handle end @@ -360,6 +365,25 @@ do } end +----- debug + +local dbgcmd = {} + +function dbgcmd.MEM() + local kb, bytes = collectgarbage "count" + skynet.ret(skynet.pack(kb,bytes)) +end + +function dbgcmd.GC() + collectgarbage "collect" +end + +local function _debug_dispatch(session, address, cmd, ...) + local f = dbgcmd[cmd] + assert(f, cmd) + f(...) +end + ----- register protocol do local REG = skynet.register_protocol @@ -391,6 +415,14 @@ do name = "response", id = 1, } + + REG { + name = "debug", + id = 9, + pack = skynet.pack, + unpack = skynet.unpack, + dispatch = _debug_dispatch, + } end local init_func = {} @@ -428,7 +460,7 @@ function skynet.start(start_func) c.callback(dispatch_message) skynet.timeout(0, function() init_template(start_func) - skynet.send(".launcher","lua", nil) + skynet.send(".launcher","text", "") end) end @@ -438,7 +470,7 @@ function skynet.filter(f ,start_func) end) skynet.timeout(0, function() init_template(start_func) - skynet.send(".launcher","lua", nil) + skynet.send(".launcher","text", "") end) end diff --git a/service/console.lua b/service/console.lua index f7669273..effaa462 100644 --- a/service/console.lua +++ b/service/console.lua @@ -15,10 +15,7 @@ local function split_package() while true do local cmd = readline "\n" if cmd ~= "" then - local handle = skynet.launch("snlua", cmd) - if handle == nil then - print("Launch error:",cmd) - end + skynet.send(skynet.self(), "text", cmd) end end end @@ -39,5 +36,11 @@ skynet.register_protocol { skynet.start(function() + skynet.dispatch("text", function (session, address, cmd) + local handle = skynet.newservice(cmd) + if handle == nil then + print("Launch error:",cmd) + end + end) socket.stdin() end) diff --git a/service/dbg.lua b/service/dbg.lua new file mode 100644 index 00000000..e4b42dc2 --- /dev/null +++ b/service/dbg.lua @@ -0,0 +1,11 @@ +local skynet = require "skynet" + +local cmd = { ... } + +skynet.start(function() + local list = skynet.call(".launcher","lua", unpack(cmd)) + for k,v in pairs(list) do + print(k,v) + end + skynet.exit() +end) \ No newline at end of file diff --git a/service/launcher.lua b/service/launcher.lua index 8af6cc59..98d9561f 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -1,13 +1,51 @@ local skynet = require "skynet" local string = string +local services = {} + +local command = {} + +function command.LIST() + local list = {} + for k,v in pairs(services) do + list[skynet.address(k)] = v + end + skynet.ret(skynet.pack(list)) +end + +function command.KILL(handle) + skynet.kill(handle) + skynet.ret( skynet.pack({ [handle] = tostring(services[handle]) })) + services[handle] = nil +end + +function command.MEM() + local list = {} + for k,v in pairs(services) do + local kb, bytes = skynet.call(k,"debug","MEM") + list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v) + end + skynet.ret(skynet.pack(list)) +end + +function command.GC() + for k,v in pairs(services) do + skynet.send(k,"debug","GC") + end + command.MEM() +end + +function command.REMOVE(handle) + services[handle] = nil +end + local instance = {} skynet.register(".launcher") skynet.start(function() - skynet.dispatch("lua" , function(session, address , service, ...) - if service == nil then + skynet.dispatch("text" , function(session, address , cmd) + if cmd == "" then -- init notice local reply = instance[address] if reply then @@ -16,13 +54,18 @@ skynet.start(function() end else -- launch request - local param = table.concat({...}, " ") + local service, param = string.match(cmd,"([^ ]+) (.*)") local inst = skynet.launch(service, param) if inst then + services[inst] = cmd instance[inst] = { session = session, address = address } else skynet.ret(skynet.pack(nil)) end end end) + skynet.dispatch("lua", function(session, address, cmd , ...) + cmd = string.upper(cmd) + command[cmd](...) + end) end) diff --git a/skynet-src/skynet_server.c b/skynet-src/skynet_server.c index 79bf6c3e..d5a7bda3 100644 --- a/skynet-src/skynet_server.c +++ b/skynet-src/skynet_server.c @@ -394,6 +394,7 @@ skynet_command(struct skynet_context * context, const char * cmd , const char * } else if (param[0] == '.') { handle = skynet_handle_findname(param+1); } else { + skynet_error(context, "Can't kill %s",param); // todo : kill global service } if (handle) { diff --git a/skynet-src/skynet_start.c b/skynet-src/skynet_start.c index 509669cb..c0e9eb62 100644 --- a/skynet-src/skynet_start.c +++ b/skynet-src/skynet_start.c @@ -84,7 +84,7 @@ skynet_start(struct skynet_config * config) { } ctx = skynet_context_new("snlua", "launcher"); if (ctx == NULL) { - fprintf(stderr,"launch launcher error"); + fprintf(stderr,"launch launcher error\n"); exit(1); } ctx = skynet_context_new("snlua", config->start);