Skip to content

Commit 3baeb62

Browse files
committed
move some api from skynet.lua to skynet/manager.lua
1 parent 9e27f59 commit 3baeb62

13 files changed

+106
-89
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
local skynet = require "skynet"
2+
require "skynet.manager" -- import skynet.abort
23

34
skynet.abort()

examples/cluster1.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local cluster = require "cluster"
3+
require "skynet.manager" -- import skynet.name
34

45
skynet.start(function()
56
local sdb = skynet.newservice("simpledb")

examples/globallog.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local skynet = require "skynet"
2+
require "skynet.manager" -- import skynet.register
23

34
skynet.start(function()
45
skynet.dispatch("lua", function(session, address, ...)

examples/main_log.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local harbor = require "skynet.harbor"
3+
require "skynet.manager" -- import skynet.monitor
34

45
local function monitor_master()
56
harbor.linkmaster()

examples/simpledb.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local skynet = require "skynet"
2+
require "skynet.manager" -- import skynet.register
23
local db = {}
34

45
local command = {}

lualib/skynet.lua

+5-89
Original file line numberDiff line numberDiff line change
@@ -284,35 +284,6 @@ function skynet.wait()
284284
session_id_coroutine[session] = nil
285285
end
286286

287-
local function globalname(name, handle)
288-
local c = string.sub(name,1,1)
289-
assert(c ~= ':')
290-
if c == '.' then
291-
return false
292-
end
293-
294-
assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h
295-
assert(tonumber(name) == nil) -- global name can't be number
296-
297-
local harbor = require "skynet.harbor"
298-
299-
harbor.globalname(name, handle)
300-
301-
return true
302-
end
303-
304-
function skynet.register(name)
305-
if not globalname(name) then
306-
c.command("REG", name)
307-
end
308-
end
309-
310-
function skynet.name(name, handle)
311-
if not globalname(name, handle) then
312-
c.command("NAME", name .. " " .. skynet.address(handle))
313-
end
314-
end
315-
316287
local self_handle
317288
function skynet.self()
318289
if self_handle then
@@ -329,13 +300,6 @@ function skynet.localname(name)
329300
end
330301
end
331302

332-
function skynet.launch(...)
333-
local addr = c.command("LAUNCH", table.concat({...}," "))
334-
if addr then
335-
return string_to_handle(addr)
336-
end
337-
end
338-
339303
function skynet.now()
340304
return tonumber(c.command("NOW"))
341305
end
@@ -374,14 +338,6 @@ function skynet.exit()
374338
coroutine_yield "QUIT"
375339
end
376340

377-
function skynet.kill(name)
378-
if type(name) == "number" then
379-
skynet.send(".launcher","lua","REMOVE",name, true)
380-
name = skynet.address(name)
381-
end
382-
c.command("KILL",name)
383-
end
384-
385341
function skynet.getenv(key)
386342
local ret = c.command("GETENV",key)
387343
if ret == "" then
@@ -528,7 +484,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
528484
end
529485
end
530486

531-
local function dispatch_message(...)
487+
function skynet.dispatch_message(...)
532488
local succ, err = pcall(raw_dispatch_message,...)
533489
while true do
534490
local key,co = next(fork_queue)
@@ -650,7 +606,7 @@ function skynet.pcall(start)
650606
return xpcall(init_template, debug.traceback, start)
651607
end
652608

653-
local function init_service(start)
609+
function skynet.init_service(start)
654610
local ok, err = skynet.pcall(start)
655611
if not ok then
656612
skynet.error("init service failed: " .. tostring(err))
@@ -662,56 +618,16 @@ local function init_service(start)
662618
end
663619

664620
function skynet.start(start_func)
665-
c.callback(dispatch_message)
666-
skynet.timeout(0, function()
667-
init_service(start_func)
668-
end)
669-
end
670-
671-
function skynet.filter(f ,start_func)
672-
c.callback(function(...)
673-
dispatch_message(f(...))
674-
end)
621+
c.callback(skynet.dispatch_message)
675622
skynet.timeout(0, function()
676-
init_service(start_func)
677-
end)
678-
end
679-
680-
function skynet.forward_type(map, start_func)
681-
c.callback(function(ptype, msg, sz, ...)
682-
local prototype = map[ptype]
683-
if prototype then
684-
dispatch_message(prototype, msg, sz, ...)
685-
else
686-
dispatch_message(ptype, msg, sz, ...)
687-
c.trash(msg, sz)
688-
end
689-
end, true)
690-
skynet.timeout(0, function()
691-
init_service(start_func)
623+
skynet.init_service(start_func)
692624
end)
693625
end
694626

695627
function skynet.endless()
696628
return c.command("ENDLESS")~=nil
697629
end
698630

699-
function skynet.abort()
700-
c.command("ABORT")
701-
end
702-
703-
function skynet.monitor(service, query)
704-
local monitor
705-
if query then
706-
monitor = skynet.queryservice(true, service)
707-
else
708-
monitor = skynet.uniqueservice(true, service)
709-
end
710-
assert(monitor, "Monitor launch failed")
711-
c.command("MONITOR", string.format(":%08x", monitor))
712-
return monitor
713-
end
714-
715631
function skynet.mqlen()
716632
return tonumber(c.command "MQLEN")
717633
end
@@ -738,7 +654,7 @@ end
738654
-- Inject internal debug framework
739655
local debug = require "skynet.debug"
740656
debug(skynet, {
741-
dispatch = dispatch_message,
657+
dispatch = skynet.dispatch_message,
742658
clear = clear_pool,
743659
suspend = suspend,
744660
})

lualib/skynet/manager.lua

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
local skynet = require "skynet"
2+
local c = require "skynet.core"
3+
4+
function skynet.launch(...)
5+
local addr = c.command("LAUNCH", table.concat({...}," "))
6+
if addr then
7+
return tonumber("0x" .. string.sub(addr , 2))
8+
end
9+
end
10+
11+
function skynet.kill(name)
12+
if type(name) == "number" then
13+
skynet.send(".launcher","lua","REMOVE",name, true)
14+
name = skynet.address(name)
15+
end
16+
c.command("KILL",name)
17+
end
18+
19+
function skynet.abort()
20+
c.command("ABORT")
21+
end
22+
23+
local function globalname(name, handle)
24+
local c = string.sub(name,1,1)
25+
assert(c ~= ':')
26+
if c == '.' then
27+
return false
28+
end
29+
30+
assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h
31+
assert(tonumber(name) == nil) -- global name can't be number
32+
33+
local harbor = require "skynet.harbor"
34+
35+
harbor.globalname(name, handle)
36+
37+
return true
38+
end
39+
40+
function skynet.register(name)
41+
if not globalname(name) then
42+
c.command("REG", name)
43+
end
44+
end
45+
46+
function skynet.name(name, handle)
47+
if not globalname(name, handle) then
48+
c.command("NAME", name .. " " .. skynet.address(handle))
49+
end
50+
end
51+
52+
local dispatch_message = skynet.dispatch_message
53+
54+
function skynet.forward_type(map, start_func)
55+
c.callback(function(ptype, msg, sz, ...)
56+
local prototype = map[ptype]
57+
if prototype then
58+
dispatch_message(prototype, msg, sz, ...)
59+
else
60+
dispatch_message(ptype, msg, sz, ...)
61+
c.trash(msg, sz)
62+
end
63+
end, true)
64+
skynet.timeout(0, function()
65+
skynet.init_service(start_func)
66+
end)
67+
end
68+
69+
function skynet.filter(f ,start_func)
70+
c.callback(function(...)
71+
dispatch_message(f(...))
72+
end)
73+
skynet.timeout(0, function()
74+
skynet.init_service(start_func)
75+
end)
76+
end
77+
78+
function skynet.monitor(service, query)
79+
local monitor
80+
if query then
81+
monitor = skynet.queryservice(true, service)
82+
else
83+
monitor = skynet.uniqueservice(true, service)
84+
end
85+
assert(monitor, "Monitor launch failed")
86+
c.command("MONITOR", string.format(":%08x", monitor))
87+
return monitor
88+
end
89+
90+
return skynet

service/bootstrap.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local harbor = require "skynet.harbor"
3+
require "skynet.manager" -- import skynet.launch, ...
34

45
skynet.start(function()
56
local standalone = skynet.getenv "standalone"

service/cdummy.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local skynet = require "skynet"
2+
require "skynet.manager" -- import skynet.launch, ...
23

34
local globalname = {}
45
local queryname = {}

service/clusterproxy.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local cluster = require "cluster"
3+
require "skynet.manager" -- inject skynet.forward_type
34

45
local node, address = ...
56

service/cslave.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local socket = require "socket"
3+
require "skynet.manager" -- import skynet.launch, ...
34
local table = table
45

56
local slaves = {}

service/launcher.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local skynet = require "skynet"
22
local core = require "skynet.core"
3+
require "skynet.manager" -- import manager apis
34
local string = string
45

56
local services = {}

service/service_mgr.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local skynet = require "skynet"
2+
require "skynet.manager" -- import skynet.register
23
local snax = require "snax"
34

45
local cmd = {}

0 commit comments

Comments
 (0)