move some api from skynet.lua to skynet/manager.lua

This commit is contained in:
Cloud Wu
2015-05-13 11:04:25 +08:00
parent 9e27f59033
commit 3baeb62b0b
13 changed files with 106 additions and 89 deletions

View File

@@ -1,3 +1,4 @@
local skynet = require "skynet"
require "skynet.manager" -- import skynet.abort
skynet.abort()

View File

@@ -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")

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
require "skynet.manager" -- import skynet.register
skynet.start(function()
skynet.dispatch("lua", function(session, address, ...)

View File

@@ -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()

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
require "skynet.manager" -- import skynet.register
local db = {}
local command = {}

View File

@@ -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,
})

90
lualib/skynet/manager.lua Normal file
View File

@@ -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

View File

@@ -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"

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
require "skynet.manager" -- import skynet.launch, ...
local globalname = {}
local queryname = {}

View File

@@ -1,5 +1,6 @@
local skynet = require "skynet"
local cluster = require "cluster"
require "skynet.manager" -- inject skynet.forward_type
local node, address = ...

View File

@@ -1,5 +1,6 @@
local skynet = require "skynet"
local socket = require "socket"
require "skynet.manager" -- import skynet.launch, ...
local table = table
local slaves = {}

View File

@@ -1,5 +1,6 @@
local skynet = require "skynet"
local core = require "skynet.core"
require "skynet.manager" -- import manager apis
local string = string
local services = {}

View File

@@ -1,4 +1,5 @@
local skynet = require "skynet"
require "skynet.manager" -- import skynet.register
local snax = require "snax"
local cmd = {}