Skip to content

Commit

Permalink
add skynet.harbor.queryname
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Aug 13, 2014
1 parent cfe8b19 commit cde423c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lualib/skynet/harbor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function harbor.globalname(name, handle)
skynet.send(".cslave", "lua", "REGISTER", name, handle)
end

function harbor.queryname(name)
return skynet.call(".cslave", "lua", "QUERYNAME", name)
end

function harbor.link(id)
skynet.call(".cslave", "lua", "LINK", id)
end
Expand Down
28 changes: 28 additions & 0 deletions service/cdummy.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local skynet = require "skynet"

local globalname = {}
local queryname = {}
local harbor = {}

skynet.register_protocol {
Expand All @@ -17,12 +18,39 @@ skynet.register_protocol {
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)
assert(globalname[name] == nil)
globalname[name] = handle
response_name(name)
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
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)
skynet.ret()
end
Expand Down
38 changes: 34 additions & 4 deletions service/cslave.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local skynet = require "skynet"
local socket = require "socket"
local table = table

local slaves = {}
local connect_queue = {}
local globalname = {}
local queryname = {}
local harbor = {}
local harbor_service
local monitor = {}
Expand All @@ -28,7 +30,7 @@ local function monitor_clear(id)
if v then
monitor[id] = nil
for _, v in ipairs(v) do
v()
v(true)
end
end
end
Expand Down Expand Up @@ -60,6 +62,17 @@ local function ready()
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)
while true do
local ok, t, id_name, address = pcall(read_package,master_fd)
Expand All @@ -72,6 +85,7 @@ local function monitor_master(master_fd)
end
elseif t == 'N' then
globalname[id_name] = address
response_name(id_name)
if connect_queue == nil then
skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name)
end
Expand Down Expand Up @@ -152,6 +166,7 @@ end
function harbor.REGISTER(fd, name, handle)
assert(globalname[name] == nil)
globalname[name] = handle
response_name(name)
socket.write(fd, pack_package("R", name, handle))
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
end
Expand All @@ -161,7 +176,7 @@ function harbor.LINK(fd, id)
if monitor[id] == nil then
monitor[id] = {}
end
table.insert(monitor[id], skynet.response(true))
table.insert(monitor[id], skynet.response())
else
skynet.ret()
end
Expand All @@ -172,19 +187,34 @@ function harbor.CONNECT(fd, id)
if monitor[id] == nil then
monitor[id] = {}
end
table.insert(monitor[id], skynet.response(true))
table.insert(monitor[id], skynet.response())
else
skynet.ret()
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()
local master_addr = skynet.getenv "master"
local harbor_id = tonumber(skynet.getenv "harbor")
local slave_address = assert(skynet.getenv "address")
local slave_fd = socket.listen(slave_address)
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,...)
local f = assert(harbor[command])
Expand Down
1 change: 1 addition & 0 deletions test/testharborlink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ skynet.start(function()
print("run skynet examples/config_log please")
harbor.connect(2)
print("harbor 2 connected")
print("LOG =", skynet.address(harbor.queryname "LOG"))
harbor.link(2)
print("disconnected")
end)

0 comments on commit cde423c

Please sign in to comment.