/
cmd.go
52 lines (46 loc) · 1.26 KB
/
cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package actor
import (
"fmt"
"strings"
)
type ICmd interface {
Start(actorSystem *ActorSystem)
RegistCmd(actorId, cmd string, f func(...string))
}
// 设置Actor监听的端口
func WithCMD(cmd ICmd) SystemOption {
return func(system *ActorSystem) error {
system.cmd = cmd
cmd.RegistCmd("", "actorinfo", system.actorInfo)
cmd.RegistCmd("", "loadlua", system.loadlua)
cmd.Start(system)
return nil
}
}
func (as *ActorSystem) RegistCmd(actorId, cmd string, fn func(...string)) {
if as.cmd != nil {
as.cmd.RegistCmd(actorId, cmd, fn)
}
}
func (s *ActorSystem) actorInfo(param ...string) {
actors := []string{}
s.actorCache.Range(func(key, value interface{}) bool {
actors = append(actors, fmt.Sprintf("[actorId=%v mail-box=%v]", key, len(value.(*actor).mailBox)))
return true
})
format := `
--------------------------------- local actor ---------------------------------
%s
--------------------------------- local actor ---------------------------------
`
logger.Info(fmt.Sprintf(format, strings.Join(actors, "\n")))
}
func (s *ActorSystem) loadlua(param ...string) {
s.actorCache.Range(func(key, value interface{}) bool {
a := value.(*actor)
if a.lua != nil {
s.Send("", a.id, "LOAD LUA", func() { a.lua.Load(a.luapath) })
}
return true
})
}