diff --git a/service/abort.lua b/examples/abort.lua similarity index 50% rename from service/abort.lua rename to examples/abort.lua index 755675a6..305cfdb5 100644 --- a/service/abort.lua +++ b/examples/abort.lua @@ -1,3 +1,4 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.abort skynet.abort() diff --git a/examples/cluster1.lua b/examples/cluster1.lua index 27c00371..6623aba3 100644 --- a/examples/cluster1.lua +++ b/examples/cluster1.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local cluster = require "cluster" +require "skynet.manager" -- import skynet.name skynet.start(function() local sdb = skynet.newservice("simpledb") diff --git a/examples/globallog.lua b/examples/globallog.lua index 2939756c..e45d33dc 100644 --- a/examples/globallog.lua +++ b/examples/globallog.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register skynet.start(function() skynet.dispatch("lua", function(session, address, ...) diff --git a/examples/main_log.lua b/examples/main_log.lua index 2a3fbd30..4dc700c4 100644 --- a/examples/main_log.lua +++ b/examples/main_log.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.monitor local function monitor_master() harbor.linkmaster() diff --git a/examples/simpledb.lua b/examples/simpledb.lua index 3a7649e6..d073dfe6 100644 --- a/examples/simpledb.lua +++ b/examples/simpledb.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register local db = {} local command = {} diff --git a/lualib/skynet.lua b/lualib/skynet.lua index 39b867c5..4e099ba6 100644 --- a/lualib/skynet.lua +++ b/lualib/skynet.lua @@ -284,35 +284,6 @@ function skynet.wait() session_id_coroutine[session] = nil end -local function globalname(name, handle) - local c = string.sub(name,1,1) - assert(c ~= ':') - if c == '.' then - return false - end - - assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h - assert(tonumber(name) == nil) -- global name can't be number - - local harbor = require "skynet.harbor" - - harbor.globalname(name, handle) - - return true -end - -function skynet.register(name) - if not globalname(name) then - c.command("REG", name) - end -end - -function skynet.name(name, handle) - if not globalname(name, handle) then - c.command("NAME", name .. " " .. skynet.address(handle)) - end -end - local self_handle function skynet.self() if self_handle then @@ -329,13 +300,6 @@ function skynet.localname(name) end end -function skynet.launch(...) - local addr = c.command("LAUNCH", table.concat({...}," ")) - if addr then - return string_to_handle(addr) - end -end - function skynet.now() return tonumber(c.command("NOW")) end @@ -374,14 +338,6 @@ function skynet.exit() coroutine_yield "QUIT" end -function skynet.kill(name) - if type(name) == "number" then - skynet.send(".launcher","lua","REMOVE",name, true) - name = skynet.address(name) - end - c.command("KILL",name) -end - function skynet.getenv(key) local ret = c.command("GETENV",key) if ret == "" then @@ -528,7 +484,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...) end end -local function dispatch_message(...) +function skynet.dispatch_message(...) local succ, err = pcall(raw_dispatch_message,...) while true do local key,co = next(fork_queue) @@ -650,7 +606,7 @@ function skynet.pcall(start) return xpcall(init_template, debug.traceback, start) end -local function init_service(start) +function skynet.init_service(start) local ok, err = skynet.pcall(start) if not ok then skynet.error("init service failed: " .. tostring(err)) @@ -662,33 +618,9 @@ local function init_service(start) end function skynet.start(start_func) - c.callback(dispatch_message) + c.callback(skynet.dispatch_message) skynet.timeout(0, function() - init_service(start_func) - end) -end - -function skynet.filter(f ,start_func) - c.callback(function(...) - dispatch_message(f(...)) - end) - skynet.timeout(0, function() - init_service(start_func) - end) -end - -function skynet.forward_type(map, start_func) - c.callback(function(ptype, msg, sz, ...) - local prototype = map[ptype] - if prototype then - dispatch_message(prototype, msg, sz, ...) - else - dispatch_message(ptype, msg, sz, ...) - c.trash(msg, sz) - end - end, true) - skynet.timeout(0, function() - init_service(start_func) + skynet.init_service(start_func) end) end @@ -696,22 +628,6 @@ function skynet.endless() return c.command("ENDLESS")~=nil end -function skynet.abort() - c.command("ABORT") -end - -function skynet.monitor(service, query) - local monitor - if query then - monitor = skynet.queryservice(true, service) - else - monitor = skynet.uniqueservice(true, service) - end - assert(monitor, "Monitor launch failed") - c.command("MONITOR", string.format(":%08x", monitor)) - return monitor -end - function skynet.mqlen() return tonumber(c.command "MQLEN") end @@ -738,7 +654,7 @@ end -- Inject internal debug framework local debug = require "skynet.debug" debug(skynet, { - dispatch = dispatch_message, + dispatch = skynet.dispatch_message, clear = clear_pool, suspend = suspend, }) diff --git a/lualib/skynet/manager.lua b/lualib/skynet/manager.lua new file mode 100644 index 00000000..4365d2e2 --- /dev/null +++ b/lualib/skynet/manager.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local c = require "skynet.core" + +function skynet.launch(...) + local addr = c.command("LAUNCH", table.concat({...}," ")) + if addr then + return tonumber("0x" .. string.sub(addr , 2)) + end +end + +function skynet.kill(name) + if type(name) == "number" then + skynet.send(".launcher","lua","REMOVE",name, true) + name = skynet.address(name) + end + c.command("KILL",name) +end + +function skynet.abort() + c.command("ABORT") +end + +local function globalname(name, handle) + local c = string.sub(name,1,1) + assert(c ~= ':') + if c == '.' then + return false + end + + assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h + assert(tonumber(name) == nil) -- global name can't be number + + local harbor = require "skynet.harbor" + + harbor.globalname(name, handle) + + return true +end + +function skynet.register(name) + if not globalname(name) then + c.command("REG", name) + end +end + +function skynet.name(name, handle) + if not globalname(name, handle) then + c.command("NAME", name .. " " .. skynet.address(handle)) + end +end + +local dispatch_message = skynet.dispatch_message + +function skynet.forward_type(map, start_func) + c.callback(function(ptype, msg, sz, ...) + local prototype = map[ptype] + if prototype then + dispatch_message(prototype, msg, sz, ...) + else + dispatch_message(ptype, msg, sz, ...) + c.trash(msg, sz) + end + end, true) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.filter(f ,start_func) + c.callback(function(...) + dispatch_message(f(...)) + end) + skynet.timeout(0, function() + skynet.init_service(start_func) + end) +end + +function skynet.monitor(service, query) + local monitor + if query then + monitor = skynet.queryservice(true, service) + else + monitor = skynet.uniqueservice(true, service) + end + assert(monitor, "Monitor launch failed") + c.command("MONITOR", string.format(":%08x", monitor)) + return monitor +end + +return skynet diff --git a/service/bootstrap.lua b/service/bootstrap.lua index e1df7170..269654c3 100644 --- a/service/bootstrap.lua +++ b/service/bootstrap.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local harbor = require "skynet.harbor" +require "skynet.manager" -- import skynet.launch, ... skynet.start(function() local standalone = skynet.getenv "standalone" diff --git a/service/cdummy.lua b/service/cdummy.lua index 52b70d7c..62865222 100644 --- a/service/cdummy.lua +++ b/service/cdummy.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.launch, ... local globalname = {} local queryname = {} diff --git a/service/clusterproxy.lua b/service/clusterproxy.lua index ad81bc3d..abdff0cc 100644 --- a/service/clusterproxy.lua +++ b/service/clusterproxy.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local cluster = require "cluster" +require "skynet.manager" -- inject skynet.forward_type local node, address = ... diff --git a/service/cslave.lua b/service/cslave.lua index d1717bb8..63d98eea 100644 --- a/service/cslave.lua +++ b/service/cslave.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local socket = require "socket" +require "skynet.manager" -- import skynet.launch, ... local table = table local slaves = {} diff --git a/service/launcher.lua b/service/launcher.lua index 634eeb1e..82e7d4be 100644 --- a/service/launcher.lua +++ b/service/launcher.lua @@ -1,5 +1,6 @@ local skynet = require "skynet" local core = require "skynet.core" +require "skynet.manager" -- import manager apis local string = string local services = {} diff --git a/service/service_mgr.lua b/service/service_mgr.lua index 4d9e4119..d94b0a3d 100644 --- a/service/service_mgr.lua +++ b/service/service_mgr.lua @@ -1,4 +1,5 @@ local skynet = require "skynet" +require "skynet.manager" -- import skynet.register local snax = require "snax" local cmd = {}