change source dir

This commit is contained in:
云风
2012-08-09 15:36:56 +08:00
parent d23c4aa905
commit eed9c42fd4
46 changed files with 284 additions and 118 deletions

17
service/agent.lua Normal file
View File

@@ -0,0 +1,17 @@
local skynet = require "skynet"
local client = ...
skynet.dispatch(function(msg, sz , session, address)
local message = skynet.tostring(msg,sz)
if session == 0 then
print("client command",msg)
local result = skynet.call("SIMPLEDB",msg)
skynet.send(client, result)
else
print("server command",msg)
if msg == "CLOSE" then
skynet.kill(client)
skynet.exit()
end
end
end)

16
service/console.lua Normal file
View File

@@ -0,0 +1,16 @@
local skynet = require "skynet"
skynet.dispatch()
skynet.start(function()
while true do
local cmd = io.read()
local handle = skynet.launch(cmd)
if handle == nil then
print("Launch error:",cmd)
else
print("Launch:",handle,cmd)
end
skynet.yield()
end
end)

8
service/globallog.lua Normal file
View File

@@ -0,0 +1,8 @@
local skynet = require "skynet"
skynet.dispatch(function(msg, sz , session , from)
local message = skynet.tostring(msg,sz)
print("[GLOBALLOG]", from,message)
end)
skynet.register "LOG"

27
service/launcher.lua Normal file
View File

@@ -0,0 +1,27 @@
local skynet = require "skynet"
local string = string
local instance = {}
skynet.dispatch(function(msg, sz , session, address)
local message = skynet.tostring(msg,sz)
if session == 0 then
-- init notice
local reply = instance[address]
if reply then
skynet.send(reply[2] , reply[1], address)
instance[address] = nil
end
else
-- launch request
local service, param = string.match(message, "([^ ]+) (.*)")
local inst = skynet.launch(service, param)
if inst then
instance[inst] = { session, address }
else
skynet.send(address, session)
end
end
end)
skynet.register(".launcher")

22
service/main.lua Normal file
View File

@@ -0,0 +1,22 @@
local skynet = require "skynet"
skynet.dispatch()
skynet.start(function()
print("Server start")
local launcher = skynet.launch("snlua","launcher")
print("launcher", launcher)
local console = skynet.launch("snlua","console")
print("console",console)
local watchdog = skynet.launch("snlua","watchdog")
print("watchdog",watchdog)
local gate = skynet.launch("gate","8888 4 0")
print("gate",gate)
local db = skynet.launch("snlua","simpledb")
print("simpledb",db)
local connection = skynet.launch("connection","256")
print("connection",connection)
local redis = skynet.call(".launcher", "broker .redis snlua redis-cli 127.0.0.1:6379")
print("redis",redis)
skynet.exit()
end)

8
service/main_log.lua Normal file
View File

@@ -0,0 +1,8 @@
local skynet = require "skynet"
print("Log server start")
local log = skynet.launch("snlua","globallog.lua")
print("log",log)
skynet.exit()

113
service/redis-cli.lua Normal file
View File

@@ -0,0 +1,113 @@
local skynet = require "skynet"
local string = string
local table = table
local tonumber = tonumber
local ipairs = ipairs
local unpack = unpack
local redis_server = ...
local fd
local write_fd
local readline_fd
local read_fd
local close_fd
local function init_fd(fdstr)
fd = fdstr
write_fd = "WRITE "..fd.." "
readline_fd = "READLINE ".. fd .." \r\n"
read_fd = "READ " .. fd .. " "
close_fd = "CLOSE "..fd
end
local function init()
fd = skynet.call(".connection", "CONNECT " .. redis_server)
if fd == nil then
print("Connect to redis server error : ", redis_server)
skynet.exit()
return true
end
init_fd(fd)
end
local function compose_message(msg)
local lines = { "*" .. #msg }
for _,v in ipairs(msg) do
table.insert(lines,"$"..#v)
table.insert(lines,v)
end
table.insert(lines,"")
local cmd = table.concat(lines,"\r\n")
return cmd
end
local redcmd = {}
redcmd[42] = function(data) -- '*'
local n = tonumber(data)
if n < 1 then
skynet.ret(skynet.pack(true, nil))
return
end
local bulk = {}
for i = 1,n do
local line = skynet.call(".connection", readline_fd)
local bytes = tonumber(string.sub(line,2) + 2)
local data = skynet.call(".connection", read_fd .. bytes)
table.insert(result, string.sub(data,1,-3))
end
skynet.ret(skynet.pack(true,bulk))
end
redcmd[36] = function(data) -- '$'
local bytes = tonumber(data)
if bytes < 0 then
skynet.ret(skynet.pack(true, nil))
return
end
local firstline = skynet.call(".connection", read_fd .. (bytes + 2))
skynet.ret(skynet.pack(true,string.sub(firstline,1,-3)))
end
redcmd[43] = function(data) -- '+'
skynet.ret(skynet.pack(true, data))
end
redcmd[45] = function(data) -- '-'
skynet.ret(skynet.pack(false, data))
end
redcmd[58] = function(data) -- ':'
skynet.ret(skynet.pack(true, tonumber(data)))
end
skynet.dispatch(function(msg, sz, session, address)
local message = { skynet.unpack(msg,sz) }
local write_cmd = write_fd .. compose_message(message)
local result
while true do
skynet.send(".connection", write_cmd )
result = skynet.call(".connection", readline_fd)
if result then
break
end
-- reconnect
if init() then
skynet.ret(skynet.pack(false , "Disconnected"))
return
end
end
local firstchar = string.byte(result)
local data = string.sub(result,2)
local f = redcmd[firstchar]
if f == nil then
skynet.ret(skynet.pack(false , "Invalid result"))
skynet.send(".connection", close_fd)
init()
else
f(data)
end
end)
skynet.start(init)

28
service/simpledb.lua Normal file
View File

@@ -0,0 +1,28 @@
local skynet = require "skynet"
local db = {}
local command = {}
function command.GET(key)
skynet.ret(db[key])
end
function command.SET(key, value)
local last = db[key]
db[key] = value
skynet.ret(last)
end
skynet.dispatch(function(msg, sz , session, from)
local message = skynet.tostring(msg,sz)
print("simpledb",message, from, session)
local cmd, key , value = string.match(message, "(%w+) (%w+) ?(.*)")
local f = command[cmd]
if f then
f(key,value)
else
skynet.ret("Invalid command : "..message)
end
end)
skynet.register "SIMPLEDB"

25
service/testconn.lua Normal file
View File

@@ -0,0 +1,25 @@
local skynet = require "skynet"
-- register a dummy callback function
skynet.dispatch()
local connection = skynet.launch("connection","16")
print("connection",connection)
skynet.start(function()
local fd = skynet.call(".connection","CONNECT 127.0.0.1:8000")
if fd == nil then
print("Connect failed")
skynet.exit()
return
end
print("connect to ",fd)
skynet.send(".connection","WRITE "..fd.." Welcome\n")
local four = skynet.call(".connection","READ "..fd .." 4")
print("READ 4", four)
local line = skynet.call(".connection","READLINE "..fd .." \n")
skynet.send(".connection","WRITE "..fd.." "..line.."\n")
skynet.send(".connection","CLOSE "..fd)
skynet.exit()
end)

12
service/testdb.lua Normal file
View File

@@ -0,0 +1,12 @@
local skynet = require "skynet"
-- register a dummy callback function
skynet.dispatch()
skynet.start(function()
local result = skynet.call("SIMPLEDB","SET foobar hello")
print(result)
result = skynet.call("SIMPLEDB","GET foobar")
print(result)
skynet.exit()
end)

12
service/testredis.lua Normal file
View File

@@ -0,0 +1,12 @@
local skynet = require "skynet"
local redis = require "redis"
skynet.dispatch()
skynet.start(function()
print(redis.cmd.EXISTS("A"))
print(redis.cmd.GET("A"))
print(redis.cmd.SET("A","hello world"))
skynet.exit()
end)

13
service/testtimer.lua Normal file
View File

@@ -0,0 +1,13 @@
local skynet = require "skynet"
-- register a dummy callback function
skynet.dispatch()
skynet.start(function()
for i = 1, 10 do
print(i)
skynet.sleep(100)
end
skynet.exit()
print("Test timer exit")
end)

46
service/watchdog.lua Normal file
View File

@@ -0,0 +1,46 @@
local skynet = require "skynet"
local command = {}
local agent_all = {}
function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
fd = tonumber(fd)
skynet.send("LOG", string.format("%d %d %s",self,fd,addr))
local client = skynet.launch("client",fd)
skynet.send("LOG", "client " .. client)
local agent = skynet.launch("snlua","agent.lua",client)
if agent then
agent_all[self] = agent
skynet.send("gate", "forward ".. self .. " " .. agent)
end
end
function command:close()
skynet.send("LOG", string.format("close %d",self))
skynet.send(agent_all[self],-1,"CLOSE")
agent_all[self] = nil
end
function command:data(data)
local agent = agent_all[self]
if agent then
skynet.send(agent, data)
else
skynet.send("LOG", string.format("data %d size=%d",self,#data))
end
end
skynet.dispatch(function(msg, sz)
local message = skynet.tostring(msg,sz)
local id, cmd , parm = string.match(message, "(%d+) (%w+) ?(.*)")
id = tonumber(id)
local f = command[cmd]
if f then
f(id,parm)
else
skynet.error(string.format("[watchdog] Unknown command : %s",message))
end
end)
skynet.register ".watchdog"