1
0
forked from x/ContextOS

mac mod context 简化了接口遍历方式和调用参数

This commit is contained in:
shaoying 2017-11-06 00:36:14 +08:00
parent 585ee2434b
commit da194af548
4 changed files with 257 additions and 226 deletions

View File

@ -1,16 +1,26 @@
# context # context
context: 通过提供动态接口,自由组合功能模块,让开发变得更简单 context: 通过提供自由的模块,简洁的接口,动态的结构,让编程变得更简单,让世界变得更美好
## 模块设计原则WCW(want, can, will) ## 设计理念
* 必须让自己容易知道想做什么 * 让用户轻松学会编程
* 必须让研发容易知道能做什么 * 让系统轻松学会编程
* 必须让用户容易知道会做什么 * 让自己轻松学会编程
## 模块设计接口CMOS(context, message, operation, system) ## 设计规范
* 协议与流程
* 框架与接口
* 语法与表达式
## 接口设计CCC(command, config, cache)
* 服务管理server
* 模块管理context * 模块管理context
* 消息管理message * 消息管理message
* 命令管理command * 命令管理command
* 配置管理config * 配置管理config
* 缓存管理cache * 缓存管理cache
* 服务管理server
## 结构设计
* 功能树
* 权限树
* 消息树

View File

@ -150,6 +150,9 @@ func (cli *CLI) Begin(m *ctx.Message) ctx.Server { // {{{
// }}} // }}}
func (cli *CLI) Start(m *ctx.Message) bool { // {{{ func (cli *CLI) Start(m *ctx.Message) bool { // {{{
cli.Capi("nterm", 1)
defer cli.Capi("nterm", -1)
if detail, ok := m.Data["detail"]; ok { if detail, ok := m.Data["detail"]; ok {
io := detail.(io.ReadWriteCloser) io := detail.(io.ReadWriteCloser)
cli.out = io cli.out = io
@ -221,44 +224,60 @@ func (cli *CLI) Exit(m *ctx.Message, arg ...string) bool { // {{{
// }}} // }}}
var Index = &ctx.Context{Name: "cli", Help: "本地控制", var Index = &ctx.Context{Name: "cli", Help: "本地控制",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{
"nterm": &ctx.Cache{Name: "nterm", Value: "0", Help: "终端数量"},
"nhistory": &ctx.Cache{Name: "nhistory", Value: "0", Help: "终端数量", Hand: func(c *ctx.Context, x *ctx.Cache, arg ...string) string {
if cli, ok := c.Server.(*CLI); ok {
return fmt.Sprintf("%d", len(cli.history))
}
return ""
}},
},
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"开场白": &ctx.Config{Name: "开场白", Value: "\n~~~ Hello Context & Message World ~~~\n", Help: "开场白"}, "开场白": &ctx.Config{Name: "开场白", Value: "\n~~~ Hello Context & Message World ~~~\n", Help: "开场白"},
"结束语": &ctx.Config{Name: "结束语", Value: "\n~~~ Byebye Context & Message World ~~~\n", Help: "结束语"}, "结束语": &ctx.Config{Name: "结束语", Value: "\n~~~ Byebye Context & Message World ~~~\n", Help: "结束语"},
"slient": &ctx.Config{Name: "slient", Value: "yes", Help: "屏蔽脚本输出"}, "slient": &ctx.Config{Name: "slient", Value: "yes", Help: "屏蔽脚本输出"},
"PS1": &ctx.Config{Name: "PS1", Value: "etcvpn>", Help: "命令行提示符", Hand: func(c *ctx.Context, arg string) string { "PS1": &ctx.Config{Name: "PS1", Value: "target", Help: "命令行提示符", Hand: func(c *ctx.Context, x *ctx.Config, arg ...string) string {
cli := c.Server.(*CLI) // {{{ cli, ok := c.Server.(*CLI)
if cli != nil && cli.target != nil { if ok && cli.target != nil { // {{{
arg = cli.target.Name + ">" // c = cli.target
switch x.Value {
case "target":
return fmt.Sprintf("%s[%s]\033[32m%s\033[0m> ", c.Cap("nhistory"), time.Now().Format("15:04:05"), cli.target.Name)
case "detail":
return fmt.Sprintf("%s[%s](%s,%s,%s)\033[32m%s\033[0m> ", c.Cap("nhistory"), time.Now().Format("15:04:05"), c.Cap("ncontext"), c.Cap("nmessage"), c.Cap("nserver"), c.Name)
} }
return fmt.Sprintf("%d[%s]\033[32m%s\033[0m ", len(cli.history), time.Now().Format("15:04:05"), arg)
}
return fmt.Sprintf("[%s]\033[32m%s\033[0m ", time.Now().Format("15:04:05"), x.Value)
// }}} // }}}
}}, }},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"context": &ctx.Command{"context [spawn|find|search name [which]]|root|back|home", "查看上下文", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "context": &ctx.Command{"context [spawn|find|search name [which]]|root|back|home", "查看上下文", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
cli, ok := c.Server.(*CLI) // {{{ cli, ok := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
case 1: case 0:
cs := []*ctx.Context{msg.Target} cs := []*ctx.Context{m.Target}
for i := 0; i < len(cs); i++ { for i := 0; i < len(cs); i++ {
if len(cs[i].Contexts) > 0 { if len(cs[i].Contexts) > 0 {
msg.Echo("%s: ", cs[i].Name) m.Echo("%s: ", cs[i].Name)
for k, v := range cs[i].Contexts { for k, v := range cs[i].Contexts {
cs = append(cs, v) cs = append(cs, v)
msg.Echo("%s, ", k) m.Echo("%s, ", k)
} }
msg.Echo("\n") m.Echo("\n")
} }
} }
case 2: case 1:
switch arg[1] { switch arg[0] {
case "root": case "root":
if ok { if ok {
cli.target = cli.Context.Root cli.target = cli.Context.Root
} else { } else {
msg.Target = msg.Target.Root m.Target = m.Target.Root
} }
case "back": case "back":
if ok { if ok {
@ -266,58 +285,58 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
cli.target = cli.Context.Context cli.target = cli.Context.Context
} }
} else { } else {
if msg.Target.Context != nil { if m.Target.Context != nil {
msg.Target = msg.Target.Context m.Target = m.Target.Context
} }
} }
case "home": case "home":
if ok { if ok {
cli.target = cli.Context cli.target = cli.Context
} else { } else {
msg.Target = msg.Context m.Target = m.Context
} }
default: default:
// if cs := msg.Target.Find(strings.Split(arg[1], ".")); cs != nil { // if cs := m.Target.Find(strings.Split(arg[1], ".")); cs != nil {
if cs := c.Root.Search(arg[1]); cs != nil && len(cs) > 0 { if cs := c.Root.Search(arg[0]); cs != nil && len(cs) > 0 {
if ok { if ok {
cli.target = cs[0] cli.target = cs[0]
} else { } else {
msg.Target = cs[0] m.Target = cs[0]
} }
} }
} }
case 3, 4: case 2, 3:
switch arg[1] { switch arg[0] {
case "spawn": case "spawn":
msg.Target.Spawn(msg, arg[2]) m.Target.Spawn(m, arg[1])
case "find": case "find":
cs := msg.Target.Find(strings.Split(arg[2], ".")) cs := m.Target.Find(strings.Split(arg[1], "."))
if cs != nil { if cs != nil {
msg.Echo("%s: %s\n", cs.Name, cs.Help) m.Echo("%s: %s\n", cs.Name, cs.Help)
if len(arg) == 4 { if len(arg) == 4 {
if ok { if ok {
cli.target = cs cli.target = cs
} else { } else {
msg.Target = cs m.Target = cs
} }
} }
} }
case "search": case "search":
cs := msg.Target.Search(arg[2]) cs := m.Target.Search(arg[1])
for i, v := range cs { for i, v := range cs {
msg.Echo("[%d] %s: %s\n", i, v.Name, v.Help) m.Echo("[%d] %s: %s\n", i, v.Name, v.Help)
} }
if len(arg) == 4 { if len(arg) == 3 {
n, e := strconv.Atoi(arg[3]) n, e := strconv.Atoi(arg[2])
if 0 <= n && n < len(cs) && e == nil { if 0 <= n && n < len(cs) && e == nil {
if ok { if ok {
cli.target = cs[n] cli.target = cs[n]
} else { } else {
msg.Target = cs[n] m.Target = cs[n]
} }
} else { } else {
msg.Echo("参数错误(0<=n<%s)", len(cs)) m.Echo("参数错误(0<=n<%s)", len(cs))
} }
} }
} }
@ -326,7 +345,7 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"message": &ctx.Command{"message detail...", "查看上下文", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "message": &ctx.Command{"message detail...", "查看上下文", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
// {{{ // {{{
ms := []*ctx.Message{ctx.Pulse} ms := []*ctx.Message{ctx.Pulse}
for i := 0; i < len(ms); i++ { for i := 0; i < len(ms); i++ {
@ -337,16 +356,16 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"server": &ctx.Command{"server start|stop|switch", "服务启动停止切换", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "server": &ctx.Command{"server start|stop|switch", "服务启动停止切换", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
s := msg.Target // {{{ s := m.Target // {{{
switch len(arg) { switch len(arg) {
case 1: case 0:
return "server start" return "server start"
case 2: case 1:
switch arg[1] { switch arg[0] {
case "start": case "start":
if s != nil { if s != nil {
go s.Start(msg) go s.Start(m)
} }
case "stop": case "stop":
case "switch": case "switch":
@ -355,11 +374,11 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"source": &ctx.Command{"source file", "运行脚本", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "source": &ctx.Command{"source file", "运行脚本", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
cli := c.Server.(*CLI) // {{{ cli := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
case 2: case 1:
f, e := os.Open(arg[1]) f, e := os.Open(arg[0])
c.Check(e) c.Check(e)
cli.push(f) cli.push(f)
} }
@ -367,34 +386,34 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"alias": &ctx.Command{"alias [short [long]]", "查看日志", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "alias": &ctx.Command{"alias [short [long]]", "查看日志", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
cli := c.Server.(*CLI) // {{{ cli := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
case 1: case 0:
for k, v := range cli.alias { for k, v := range cli.alias {
msg.Echo("%s: %s\n", k, v) m.Echo("%s: %s\n", k, v)
} }
case 3: case 2:
switch arg[1] { switch arg[0] {
case "delete": case "delete":
delete(cli.alias, arg[2]) delete(cli.alias, arg[1])
default: default:
cli.alias[arg[1]] = arg[2] cli.alias[arg[0]] = arg[1]
msg.Echo("%s: %s\n", arg[1], cli.alias[arg[1]]) m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]])
} }
} }
return "" return ""
// }}} // }}}
}}, }},
"history": &ctx.Command{"history number", "查看日志", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "history": &ctx.Command{"history number", "查看日志", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
cli := c.Server.(*CLI) // {{{ cli := c.Server.(*CLI) // {{{
switch len(arg) { switch len(arg) {
case 1: case 0:
for i, v := range cli.history { for i, v := range cli.history {
msg.Echo("%d %s %s\n", i, v["time"], v["cli"]) m.Echo("%d %s %s\n", i, v["time"], v["cli"])
} }
case 2: case 1:
n, e := strconv.Atoi(arg[1]) n, e := strconv.Atoi(arg[0])
if e == nil && 0 <= n && n < len(cli.history) { if e == nil && 0 <= n && n < len(cli.history) {
return cli.history[n]["cli"] return cli.history[n]["cli"]
} }
@ -402,85 +421,85 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"command": &ctx.Command{"command [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "command": &ctx.Command{"command [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
switch len(arg) { // {{{ switch len(arg) { // {{{
case 1: case 0:
for k, v := range msg.Target.Commands { for k, v := range m.Target.Commands {
msg.Echo("%s: %s\n", k, v.Help) m.Echo("%s: %s\n", k, v.Help)
} }
case 2: case 2:
if v, ok := msg.Target.Commands[arg[1]]; ok { if v, ok := m.Target.Commands[arg[0]]; ok {
msg.Echo("%s: %s\n", v.Name, v.Help) m.Echo("%s: %s\n", v.Name, v.Help)
} }
case 3: case 3:
switch arg[1] { switch arg[0] {
case "delete": case "delete":
if _, ok := msg.Target.Commands[arg[2]]; ok { if _, ok := m.Target.Commands[arg[1]]; ok {
delete(msg.Target.Commands, arg[2]) delete(m.Target.Commands, arg[1])
} }
} }
} }
msg.Echo("\n") m.Echo("\n")
return "" return ""
// }}} // }}}
}}, }},
"config": &ctx.Command{"config [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "config": &ctx.Command{"config [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
switch len(arg) { // {{{ switch len(arg) { // {{{
case 0:
for k, v := range m.Target.Configs {
m.Echo("%s(%s): %s\n", k, v.Value, v.Help)
}
case 1: case 1:
for k, v := range msg.Target.Configs { if v, ok := m.Target.Configs[arg[0]]; ok {
msg.Echo("%s(%s): %s\n", k, v.Value, v.Help) m.Echo("%s: %s\n", v.Name, v.Help)
} }
case 2: case 2:
if v, ok := msg.Target.Configs[arg[1]]; ok { switch arg[0] {
msg.Echo("%s: %s\n", v.Name, v.Help)
}
case 3:
switch arg[1] {
case "delete": case "delete":
if _, ok := msg.Target.Configs[arg[2]]; ok { if _, ok := m.Target.Configs[arg[1]]; ok {
delete(msg.Target.Configs, arg[2]) delete(m.Target.Configs, arg[1])
} }
default: default:
if _, ok := msg.Target.Configs[arg[1]]; ok { if _, ok := m.Target.Configs[arg[0]]; ok {
msg.Target.Conf(arg[1:]...) m.Target.Conf(arg[0], arg[1:]...)
} }
} }
case 5: case 4:
msg.Target.Conf(arg[1:]...) m.Target.Conf(arg[0], arg[1:]...)
} }
return "" return ""
// }}} // }}}
}}, }},
"cache": &ctx.Command{"cache [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { "cache": &ctx.Command{"cache [name [value [help]]]", "查看修改添加配置", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
switch len(arg) { // {{{ switch len(arg) { // {{{
case 0:
for k, v := range m.Target.Caches {
m.Echo("%s(%s): %s\n", k, v.Value, v.Help)
}
case 1: case 1:
for k, v := range msg.Target.Caches { if v, ok := m.Target.Caches[arg[0]]; ok {
msg.Echo("%s(%s): %s\n", k, v.Value, v.Help) m.Echo("%s: %s\n", v.Name, v.Help)
} }
case 2: case 2:
if v, ok := msg.Target.Caches[arg[1]]; ok { switch arg[0] {
msg.Echo("%s: %s\n", v.Name, v.Help)
}
case 3:
switch arg[1] {
case "delete": case "delete":
if _, ok := msg.Target.Caches[arg[2]]; ok { if _, ok := m.Target.Caches[arg[1]]; ok {
delete(msg.Target.Caches, arg[2]) delete(m.Target.Caches, arg[1])
} }
default: default:
if _, ok := msg.Target.Caches[arg[1]]; ok { if _, ok := m.Target.Caches[arg[0]]; ok {
msg.Echo("%s: %s\n", arg[1], msg.Target.Cap(arg[1:]...)) m.Echo("%s: %s\n", arg[0], m.Target.Cap(arg[0], arg[1:]...))
} }
} }
case 5: case 4:
msg.Target.Cap(arg[1:]...) m.Target.Cap(arg[0], arg[1:]...)
} }
return "" return ""
// }}} // }}}
}}, }},
"exit": &ctx.Command{"exit", "退出", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "exit": &ctx.Command{"exit", "退出", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
cli, ok := m.Target.Server.(*CLI) cli, ok := m.Target.Server.(*CLI) // {{{
if !ok { if !ok {
cli, ok = m.Context.Server.(*CLI) cli, ok = m.Context.Server.(*CLI)
} }
@ -493,19 +512,20 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
} }
return "" return ""
// }}}
}}, }},
"remote": &ctx.Command{"remote master|slave listen|dial address protocol", "建立远程连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "remote": &ctx.Command{"remote master|slave listen|dial address protocol", "建立远程连接", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
switch len(arg) { // {{{ switch len(arg) { // {{{
case 1: case 0:
case 5: case 4:
if arg[1] == "master" { if arg[0] == "master" {
if arg[2] == "dial" { if arg[1] == "dial" {
} else { } else {
} }
} else { } else {
if arg[2] == "listen" { if arg[1] == "listen" {
s := c.Root.Find(strings.Split(arg[4], ".")) s := c.Root.Find(strings.Split(arg[3], "."))
m.Message.Spawn(s, arg[3]).Cmd("listen", arg[3]) m.Message.Spawn(s, arg[2]).Cmd("listen", arg[2])
} else { } else {
} }
} }
@ -513,11 +533,12 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
return "" return ""
// }}} // }}}
}}, }},
"accept": &ctx.Command{"accept address protocl", "建立远程连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "accept": &ctx.Command{"accept address protocl", "建立远程连接", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
m.Start(arg[1:]...) // {{{ m.Start(fmt.Sprintf("PTS%d", c.Capi("nterm")), arg[1]) // {{{
return "" return ""
// }}} // }}}
}}, }},
"void": &ctx.Command{"", "", nil},
}, },
Messages: make(chan *ctx.Message, 10), Messages: make(chan *ctx.Message, 10),
} }

View File

@ -20,7 +20,7 @@ type Cache struct { // {{{
Name string Name string
Value string Value string
Help string Help string
Hand func(c *Context, arg string) string Hand func(c *Context, x *Cache, arg ...string) string
} }
// }}} // }}}
@ -28,15 +28,14 @@ type Config struct { // {{{
Name string Name string
Value string Value string
Help string Help string
Hand func(c *Context, arg string) string Hand func(c *Context, x *Config, arg ...string) string
Spawn bool
} }
// }}} // }}}
type Command struct { // {{{ type Command struct { // {{{
Name string Name string
Help string Help string
Hand func(c *Context, m *Message, arg ...string) string Hand func(c *Context, m *Message, key string, arg ...string) string
} }
// }}} // }}}
@ -158,8 +157,7 @@ func (m *Message) Cmd(arg ...string) string { // {{{
m.Meta["detail"] = arg m.Meta["detail"] = arg
} }
log.Printf("%s command(%s->%s): %v", m.Target.Name, m.Context.Name, m.Target.Name, m.Meta["detail"]) return m.Target.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...)
return m.Target.Cmd(m, m.Meta["detail"]...)
} }
// }}} // }}}
@ -275,16 +273,16 @@ func (c *Context) Register(s *Context, x Server) *Context { // {{{
s.Context = c s.Context = c
s.Server = x s.Server = x
log.Printf("%s register(%d): %s", c.Name, Index.Capi("ncontext", 1), s.Name) log.Printf("%s sub(%d): %s", c.Name, Index.Capi("ncontext", 1), s.Name)
return s return s
} }
// }}} // }}}
func (c *Context) Begin(m *Message) *Context { // {{{ func (c *Context) Begin(m *Message) *Context { // {{{
for _, v := range c.Configs { for _, x := range c.Configs {
if v.Hand != nil { if x.Hand != nil {
v.Hand(c, v.Value) x.Hand(c, x, x.Value)
log.Println(c.Name, "config:", v.Name, v.Value) log.Println(c.Name, "conf:", x.Name, x.Value)
} }
} }
@ -340,8 +338,7 @@ func (c *Context) Deal(pre func(m *Message) bool, post func(m *Message) bool) (l
} }
c.Safe(m, func(c *Context, m *Message) { c.Safe(m, func(c *Context, m *Message) {
c.Cmd(m, m.Meta["detail"]...) c.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...)
log.Printf("%s command(%s->%s): %v", c.Name, m.Context.Name, m.Target.Name, m.Meta["detail"])
}, func(c *Context, m *Message) { }, func(c *Context, m *Message) {
m.Cmd() m.Cmd()
@ -602,116 +599,111 @@ func (c *Context) Del(arg ...string) { // {{{
// }}} // }}}
func (c *Context) Cmd(m *Message, arg ...string) string { // {{{ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{
if x, ok := c.Commands[arg[0]]; ok { for s := c; s != nil; s = s.Context {
return x.Hand(c, m, arg...) if x, ok := s.Commands[key]; ok {
log.Printf("%s cmd(%s->%s): %v", c.Name, m.Context.Name, m.Target.Name, m.Meta["detail"])
if x.Hand == nil {
panic(errors.New(fmt.Sprintf(key + "没有权限")))
}
return x.Hand(c, m, key, arg...)
}
} }
if c.Context != nil { panic(errors.New(fmt.Sprintf(key + "命令项不存在")))
return c.Context.Cmd(m, arg...)
}
panic(errors.New(fmt.Sprintf(arg[0] + "命令项不存在")))
} }
// }}} // }}}
func (c *Context) Conf(arg ...string) string { // {{{ func (c *Context) Conf(key string, arg ...string) string { // {{{
for s := c; s != nil; s = s.Context {
if x, ok := s.Configs[key]; ok {
switch len(arg) { switch len(arg) {
case 0:
if x.Hand != nil {
return x.Hand(c, x)
}
return x.Value
case 1: case 1:
if v, ok := c.Configs[arg[0]]; ok { log.Println(c.Name, "conf:", key, arg[0])
if v.Hand != nil { x.Value = arg[0]
return v.Hand(c, v.Value) if x.Hand != nil {
return x.Hand(c, x, x.Value)
} }
return v.Value return x.Value
case 3:
log.Println(c.Name, "conf:", key, arg)
if s == c {
panic(errors.New(x.Name + "配置项已存在"))
} }
c.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: x.Hand}
if c.Context != nil { return arg[1]
return c.Context.Conf(arg...)
}
case 2:
if v, ok := c.Configs[arg[0]]; ok {
v.Value = arg[1]
if v.Hand != nil {
return v.Hand(c, v.Value)
}
log.Println(c.Name, "config:", arg)
return v.Value
}
if c.Context != nil {
return c.Context.Conf(arg...)
}
case 4:
if v, ok := c.Configs[arg[0]]; ok {
panic(errors.New(v.Name + "配置项已存在"))
}
c.Configs[arg[0]] = &Config{Name: arg[1], Value: arg[2], Help: arg[3]}
log.Println(c.Name, "config:", arg)
return arg[2]
default: default:
panic(errors.New(arg[0] + "配置项参数错误")) panic(errors.New(key + "配置项参数错误"))
}
}
} }
panic(errors.New(arg[0] + "配置项不存在")) if len(arg) == 3 {
log.Println(c.Name, "conf:", key, arg)
c.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2]}
return arg[1]
}
panic(errors.New(key + "配置项不存在"))
} }
// }}} // }}}
func (c *Context) Confi(arg ...string) int { // {{{ func (c *Context) Confi(key string, arg ...string) int { // {{{
n, e := strconv.Atoi(c.Conf(arg...)) n, e := strconv.Atoi(c.Conf(key, arg...))
c.Check(e) c.Check(e)
return n return n
} }
// }}} // }}}
func (c *Context) Cap(arg ...string) string { // {{{ func (c *Context) Cap(key string, arg ...string) string { // {{{
for s := c; s != nil; s = s.Context {
if x, ok := s.Caches[key]; ok {
switch len(arg) { switch len(arg) {
case 0:
if x.Hand != nil {
return x.Hand(c, x)
}
return x.Value
case 1: case 1:
if v, ok := c.Caches[arg[0]]; ok { if x.Hand != nil {
if v.Hand != nil { return x.Hand(c, x, x.Value)
v.Value = v.Hand(c, v.Value)
} }
// log.Println(c.Name, "cache:", arg, v.Value) x.Value = arg[0]
return v.Value return x.Value
case 3:
if s == c {
panic(errors.New(key + "缓存项已存在"))
} }
c.Caches[key] = &Cache{arg[0], arg[1], arg[2], x.Hand}
if c.Context != nil { return arg[1]
return c.Context.Cap(arg...)
}
case 2:
if v, ok := c.Caches[arg[0]]; ok {
v.Value = arg[1]
if v.Hand != nil {
v.Value = v.Hand(c, v.Value)
}
// log.Println(c.Name, "cache:", arg)
return v.Value
}
if c.Context != nil {
return c.Context.Cap(arg...)
}
case 4:
// if v, ok := c.Caches[arg[0]]; ok {
// panic(errors.New(v.Name + "缓存项已存在"))
// }
//
c.Caches[arg[0]] = &Cache{arg[1], arg[2], arg[3], nil}
// log.Println(c.Name, "cache:", arg)
return arg[2]
default: default:
panic(errors.New(arg[0] + "缓存项参数错误")) panic(errors.New(key + "缓存项参数错误"))
}
}
}
if len(arg) == 3 {
c.Caches[key] = &Cache{arg[0], arg[1], arg[2], nil}
return arg[1]
} }
panic(errors.New(arg[0] + "缓存项不存在")) panic(errors.New(key + "缓存项不存在"))
} }
// }}} // }}}
func (c *Context) Capi(key string, value int) int { // {{{ func (c *Context) Capi(key string, arg ...int) int { // {{{
n, e := strconv.Atoi(c.Cap(key)) n, e := strconv.Atoi(c.Cap(key))
c.Check(e) c.Check(e)
c.Cap(key, strconv.Itoa(n+value)) if len(arg) > 0 {
return n + value n += arg[0]
c.Cap(key, strconv.Itoa(n))
}
return n
} }
// }}} // }}}
@ -736,15 +728,22 @@ var Index = &Context{Name: "ctx", Help: "根上下文",
"debug": &Config{Name: "debug", Value: "off", Help: "调试模式"}, "debug": &Config{Name: "debug", Value: "off", Help: "调试模式"},
"start": &Config{Name: "start", Value: "cli", Help: "默认启动模块"}, "start": &Config{Name: "start", Value: "cli", Help: "默认启动模块"},
"init.sh": &Config{Name: "init.sh", Value: "etc/init.sh", Help: "默认启动脚本"}, "init.sh": &Config{Name: "init.sh", Value: "etc/init.sh", Help: "默认启动脚本"},
"bench.log": &Config{Name: "bench.log", Value: "var/bench.log", Help: "默认日志文件", Hand: func(c *Context, arg string) string { "bench.log": &Config{Name: "bench.log", Value: "var/bench.log", Help: "默认日志文件", Hand: func(c *Context, x *Config, arg ...string) string {
l, e := os.Create(arg) // {{{ if len(arg) > 0 { // {{{
l, e := os.Create(x.Value)
c.Check(e) c.Check(e)
log.SetOutput(l) log.SetOutput(l)
return arg }
return x.Value
// }}} // }}}
}}, }},
}, },
Commands: map[string]*Command{}, Commands: map[string]*Command{
"void": &Command{"void", "建立远程连接", func(c *Context, m *Message, key string, arg ...string) string {
m.Echo("hello void!\n")
return ""
}},
},
Session: map[string]*Message{"root": Pulse}, Session: map[string]*Message{"root": Pulse},
Resource: []*Message{Pulse}, Resource: []*Message{Pulse},
} }

View File

@ -23,7 +23,7 @@ func (tcp *TCP) Start(m *ctx.Message) bool { // {{{
return true return true
} }
l, e := net.Listen("tcp", tcp.Conf("address")) l, e := net.Listen("tcp4", tcp.Conf("address"))
tcp.Check(e) tcp.Check(e)
tcp.listener = l tcp.listener = l
@ -88,36 +88,36 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络连接",
"address": &ctx.Config{Name: "address", Value: "", Help: "监听地址"}, "address": &ctx.Config{Name: "address", Value: "", Help: "监听地址"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"listen": &ctx.Command{"listen address", "监听端口", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "listen": &ctx.Command{"listen address", "监听端口", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
switch len(arg) { // {{{ switch len(arg) { // {{{
case 1: case 0:
for k, s := range m.Target.Contexts { for k, s := range m.Target.Contexts {
m.Echo("%s %s\n", k, s.Server.(*TCP).listener.Addr().String()) m.Echo("%s %s\n", k, s.Server.(*TCP).listener.Addr().String())
} }
case 2: case 1:
m.Start(arg[1:]...) m.Start(arg...)
} }
return "" return ""
// }}} // }}}
}}, }},
"dial": &ctx.Command{"dial", "建立连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "dial": &ctx.Command{"dial", "建立连接", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
tcp := c.Server.(*TCP) // {{{ tcp := c.Server.(*TCP) // {{{
switch len(arg) { switch len(arg) {
case 1: case 0:
for i, v := range tcp.Resource { for i, v := range tcp.Resource {
conn := v.Data["result"].(net.Conn) conn := v.Data["result"].(net.Conn)
m.Echo(tcp.Name, "conn: %s %s -> %s\n", i, conn.LocalAddr(), conn.RemoteAddr()) m.Echo(tcp.Name, "conn: %s %s -> %s\n", i, conn.LocalAddr(), conn.RemoteAddr())
} }
case 2: case 2:
conn, e := net.Dial("tcp", arg[1]) conn, e := net.Dial("tcp", arg[0])
c.Check(e) c.Check(e)
log.Println(tcp.Name, "dial:", conn.LocalAddr(), "->", conn.RemoteAddr()) log.Println(tcp.Name, "dial:", conn.LocalAddr(), "->", conn.RemoteAddr())
} }
return "" return ""
// }}} // }}}
}}, }},
"exit": &ctx.Command{"exit", "退出", func(c *ctx.Context, m *ctx.Message, arg ...string) string { "exit": &ctx.Command{"exit", "退出", func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
tcp, ok := m.Target.Server.(*TCP) tcp, ok := m.Target.Server.(*TCP) // {{{
if !ok { if !ok {
tcp, ok = m.Context.Server.(*TCP) tcp, ok = m.Context.Server.(*TCP)
} }
@ -126,6 +126,7 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络连接",
} }
return "" return ""
// }}}
}}, }},
}, },
} }