global group multicast

This commit is contained in:
云风
2012-08-30 23:31:33 +08:00
parent 161528e686
commit 1ee802c5c5
17 changed files with 285 additions and 40 deletions

View File

@@ -8,8 +8,6 @@ skynet.start(function()
local handle = skynet.launch(cmd)
if handle == nil then
print("Launch error:",cmd)
else
print("Launch:",handle,cmd)
end
skynet.yield()
end

58
service/group_agent.lua Normal file
View File

@@ -0,0 +1,58 @@
local skynet = require "skynet"
local command = {}
local group = {}
function command.CREATE(id)
assert(group[id] == nil)
group[id] = {}
local recv_id = id * 2
local addr = skynet.query_group(recv_id)
skynet.ret(skynet.pack(addr))
end
function command.AGENT(id, handle)
local tunnel = skynet.launch("tunnel", skynet.address(handle))
assert(tunnel)
local send_id = id * 2 + 1
skynet.enter_group(send_id, tunnel)
table.insert(group[id] , tunnel)
end
function command.ENTER(id, handle)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.enter_group(recv_id, handle)
skynet.enter_group(send_id, handle)
end
function command.LEAVE(id, handle)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.leave_group(recv_id, handle)
skynet.leave_group(send_id, handle)
end
function command.CLEAR(id)
local g = group[id]
assert(g)
local recv_id = id * 2
local send_id = id * 2 + 1
skynet.clear_group(recv_id)
skynet.clear_group(send_id)
group[id] = nil
for _,v in ipairs(g) do
skynet.kill(v)
end
end
skynet.dispatch(function (msg,sz)
local cmd , id , handle = skynet.unpack(msg,sz)
local f = command[cmd]
assert(f, cmd)
f(id,handle)
end)
skynet.start(function()
skynet.send("GROUPMGR" , 0, skynet.pack("MASTER", skynet.self()))
end)

69
service/group_mgr.lua Normal file
View File

@@ -0,0 +1,69 @@
local skynet = require "skynet"
local id = 0
local harbor_ctrl = {}
local multicast = {}
local command = {}
function command.MASTER(address, harbor)
harbor_ctrl[harbor] = address
end
local function create_group_in(harbor, id)
local local_ctrl = assert(harbor_ctrl[harbor])
local g = multicast[id]
local local_mc = skynet.call(local_ctrl, skynet.unpack, skynet.pack("CREATE", id))
for _,mc in pairs(g) do
skynet.send(local_ctrl, 0, skynet.pack("AGENT", id, mc))
end
for harbor_id,ctrl in pairs(harbor_ctrl) do
if harbor_id ~= harbor then
skynet.send(ctrl,0, skynet.pack("AGENT", id, local_mc))
end
end
g[harbor] = local_mc
end
function command.NEW()
id = id + 1
multicast[id] = {}
skynet.ret(skynet.pack(id))
end
function command.ENTER(address, harbor, id)
local g = multicast[id]
assert(g,id)
if g[harbor] == nil then
create_group_in(harbor, id)
end
local local_ctrl = assert(harbor_ctrl[harbor])
skynet.send(local_ctrl, 0, skynet.pack("ENTER", id, address))
end
function command.LEAVE(address, harbor, id)
local g = multicast[id]
assert(g,id)
local local_ctrl = assert(harbor_ctrl[harbor])
skynet.send(local_ctrl, 0, skynet.pack("LEAVE", id, address))
end
function command.DELETE(_,_, id)
local g = multicast[id]
assert(g,id)
multicast[id] = nil
for harbor_id,_ in pairs(g) do
skynet.send(harbor_ctrl[harbor_id],0, skynet.pack("CLEAR", id))
end
end
skynet.dispatch(function (msg,sz)
local cmd , address , param = skynet.unpack(msg,sz)
local f = command[cmd]
assert(f, cmd, param)
local harbor = skynet.harbor(address)
f(address, harbor, param)
end)
skynet.register("GROUPMGR")

View File

@@ -6,11 +6,14 @@ skynet.start(function()
print("Server start")
local lualog = skynet.launch("snlua","lualog")
local launcher = skynet.launch("snlua","launcher")
local group_mgr = skynet.launch("snlua", "group_mgr")
local group_agent = skynet.launch("snlua", "group_agent")
local remoteroot = skynet.launch("snlua","remote_root")
local console = skynet.launch("snlua","console")
local watchdog = skynet.launch("snlua","watchdog","8888 4 0")
local db = skynet.launch("snlua","simpledb")
local connection = skynet.launch("connection","256")
local redis = skynet.launch("snlua","redis-mgr")
-- skynet.launch("snlua","testgroup")
skynet.exit()
end)

View File

@@ -1,7 +1,15 @@
local skynet = require "skynet"
print("Log server start")
skynet.dispatch()
skynet.launch("snlua","globallog")
skynet.start(function()
print("Log server start")
local lualog = skynet.launch("snlua","lualog")
local launcher = skynet.launch("snlua","launcher")
local group_agent = skynet.launch("snlua", "group_agent")
local console = skynet.launch("snlua","console")
local log = skynet.launch("snlua","globallog")
-- skynet.launch("snlua","testgroup_c 11 1")
skynet.exit()
end)
skynet.exit()

View File

@@ -1,13 +1,24 @@
local skynet = require "skynet"
local group = require "mcgroup"
skynet.dispatch()
skynet.start(function()
local group = skynet.query_group(1)
local gid = group.create()
local gaddr = group.address(gid)
print("=== Create Group ===",gid,skynet.address(gaddr))
for i=1,10 do
local address = skynet.newservice("testgroup_c", tostring(i))
skynet.enter_group(1 , address)
group.enter(gid , address)
end
skynet.send(group,"Hello World")
skynet.exit()
skynet.sleep(1000)
skynet.send(gaddr,"Hello World")
-- local group = skynet.query_group(1)
-- for i=1,10 do
-- local address = skynet.newservice("testgroup_c", tostring(i))
-- skynet.enter_group(1 , address)
-- end
-- skynet.send(group,"Hello World")
-- skynet.exit()
end)

View File

@@ -1,11 +1,14 @@
local skynet = require "skynet"
local id = ...
local group = require "mcgroup"
local id, g = ...
skynet.dispatch(function (msg,sz)
print(id, skynet.tostring(msg,sz))
print("===>",id, skynet.tostring(msg,sz))
end
)
skynet.start(function()
print("start",id)
if g then
group.enter(tonumber(g))
end
end)