mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
add skynet.harbor.queryname
This commit is contained in:
@@ -7,6 +7,10 @@ function harbor.globalname(name, handle)
|
|||||||
skynet.send(".cslave", "lua", "REGISTER", name, handle)
|
skynet.send(".cslave", "lua", "REGISTER", name, handle)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function harbor.queryname(name)
|
||||||
|
return skynet.call(".cslave", "lua", "QUERYNAME", name)
|
||||||
|
end
|
||||||
|
|
||||||
function harbor.link(id)
|
function harbor.link(id)
|
||||||
skynet.call(".cslave", "lua", "LINK", id)
|
skynet.call(".cslave", "lua", "LINK", id)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
|
||||||
local globalname = {}
|
local globalname = {}
|
||||||
|
local queryname = {}
|
||||||
local harbor = {}
|
local harbor = {}
|
||||||
|
|
||||||
skynet.register_protocol {
|
skynet.register_protocol {
|
||||||
@@ -17,12 +18,39 @@ skynet.register_protocol {
|
|||||||
unpack = skynet.tostring,
|
unpack = skynet.tostring,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
local function response_name(name)
|
||||||
|
local address = globalname[name]
|
||||||
|
if queryname[name] then
|
||||||
|
local tmp = queryname[name]
|
||||||
|
queryname[name] = nil
|
||||||
|
for _,resp in ipairs(tmp) do
|
||||||
|
resp(true, address)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function harbor.REGISTER(name, handle)
|
function harbor.REGISTER(name, handle)
|
||||||
assert(globalname[name] == nil)
|
assert(globalname[name] == nil)
|
||||||
globalname[name] = handle
|
globalname[name] = handle
|
||||||
|
response_name(name)
|
||||||
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function harbor.QUERYNAME(fd, name)
|
||||||
|
local result = globalname[name]
|
||||||
|
if result then
|
||||||
|
skynet.ret(skynet.pack(result))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local queue = queryname[name]
|
||||||
|
if queue == nil then
|
||||||
|
queue = { skynet.response() }
|
||||||
|
queryname[name] = queue
|
||||||
|
else
|
||||||
|
table.insert(queue, skynet.response())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function harbor.LINK(id)
|
function harbor.LINK(id)
|
||||||
skynet.ret()
|
skynet.ret()
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
|
local table = table
|
||||||
|
|
||||||
local slaves = {}
|
local slaves = {}
|
||||||
local connect_queue = {}
|
local connect_queue = {}
|
||||||
local globalname = {}
|
local globalname = {}
|
||||||
|
local queryname = {}
|
||||||
local harbor = {}
|
local harbor = {}
|
||||||
local harbor_service
|
local harbor_service
|
||||||
local monitor = {}
|
local monitor = {}
|
||||||
@@ -28,7 +30,7 @@ local function monitor_clear(id)
|
|||||||
if v then
|
if v then
|
||||||
monitor[id] = nil
|
monitor[id] = nil
|
||||||
for _, v in ipairs(v) do
|
for _, v in ipairs(v) do
|
||||||
v()
|
v(true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -60,6 +62,17 @@ local function ready()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function response_name(name)
|
||||||
|
local address = globalname[name]
|
||||||
|
if queryname[name] then
|
||||||
|
local tmp = queryname[name]
|
||||||
|
queryname[name] = nil
|
||||||
|
for _,resp in ipairs(tmp) do
|
||||||
|
resp(true, address)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function monitor_master(master_fd)
|
local function monitor_master(master_fd)
|
||||||
while true do
|
while true do
|
||||||
local ok, t, id_name, address = pcall(read_package,master_fd)
|
local ok, t, id_name, address = pcall(read_package,master_fd)
|
||||||
@@ -72,6 +85,7 @@ local function monitor_master(master_fd)
|
|||||||
end
|
end
|
||||||
elseif t == 'N' then
|
elseif t == 'N' then
|
||||||
globalname[id_name] = address
|
globalname[id_name] = address
|
||||||
|
response_name(id_name)
|
||||||
if connect_queue == nil then
|
if connect_queue == nil then
|
||||||
skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name)
|
skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name)
|
||||||
end
|
end
|
||||||
@@ -152,6 +166,7 @@ end
|
|||||||
function harbor.REGISTER(fd, name, handle)
|
function harbor.REGISTER(fd, name, handle)
|
||||||
assert(globalname[name] == nil)
|
assert(globalname[name] == nil)
|
||||||
globalname[name] = handle
|
globalname[name] = handle
|
||||||
|
response_name(name)
|
||||||
socket.write(fd, pack_package("R", name, handle))
|
socket.write(fd, pack_package("R", name, handle))
|
||||||
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
||||||
end
|
end
|
||||||
@@ -161,7 +176,7 @@ function harbor.LINK(fd, id)
|
|||||||
if monitor[id] == nil then
|
if monitor[id] == nil then
|
||||||
monitor[id] = {}
|
monitor[id] = {}
|
||||||
end
|
end
|
||||||
table.insert(monitor[id], skynet.response(true))
|
table.insert(monitor[id], skynet.response())
|
||||||
else
|
else
|
||||||
skynet.ret()
|
skynet.ret()
|
||||||
end
|
end
|
||||||
@@ -172,19 +187,34 @@ function harbor.CONNECT(fd, id)
|
|||||||
if monitor[id] == nil then
|
if monitor[id] == nil then
|
||||||
monitor[id] = {}
|
monitor[id] = {}
|
||||||
end
|
end
|
||||||
table.insert(monitor[id], skynet.response(true))
|
table.insert(monitor[id], skynet.response())
|
||||||
else
|
else
|
||||||
skynet.ret()
|
skynet.ret()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function harbor.QUERYNAME(fd, name)
|
||||||
|
local result = globalname[name]
|
||||||
|
if result then
|
||||||
|
skynet.ret(skynet.pack(result))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local queue = queryname[name]
|
||||||
|
if queue == nil then
|
||||||
|
queue = { skynet.response() }
|
||||||
|
queryname[name] = queue
|
||||||
|
else
|
||||||
|
table.insert(queue, skynet.response())
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
local master_addr = skynet.getenv "master"
|
local master_addr = skynet.getenv "master"
|
||||||
local harbor_id = tonumber(skynet.getenv "harbor")
|
local harbor_id = tonumber(skynet.getenv "harbor")
|
||||||
local slave_address = assert(skynet.getenv "address")
|
local slave_address = assert(skynet.getenv "address")
|
||||||
local slave_fd = socket.listen(slave_address)
|
local slave_fd = socket.listen(slave_address)
|
||||||
skynet.error("slave connect to master " .. tostring(master_addr))
|
skynet.error("slave connect to master " .. tostring(master_addr))
|
||||||
local master_fd = socket.open(master_addr)
|
local master_fd = assert(socket.open(master_addr), "Can't connect to master")
|
||||||
|
|
||||||
skynet.dispatch("lua", function (_,_,command,...)
|
skynet.dispatch("lua", function (_,_,command,...)
|
||||||
local f = assert(harbor[command])
|
local f = assert(harbor[command])
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ skynet.start(function()
|
|||||||
print("run skynet examples/config_log please")
|
print("run skynet examples/config_log please")
|
||||||
harbor.connect(2)
|
harbor.connect(2)
|
||||||
print("harbor 2 connected")
|
print("harbor 2 connected")
|
||||||
|
print("LOG =", skynet.address(harbor.queryname "LOG"))
|
||||||
harbor.link(2)
|
harbor.link(2)
|
||||||
print("disconnected")
|
print("disconnected")
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user