forked from x/ContextOS
vpn pro ctx 修改了一些语法细节
This commit is contained in:
parent
47c81a79fd
commit
bdc2fd7102
@ -184,7 +184,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
cli.Messages = make(chan *ctx.Message, cli.Confi("MessageQueueSize"))
|
cli.Messages = make(chan *ctx.Message, cli.Confi("MessageQueueSize"))
|
||||||
}
|
}
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
cli.Configs["init.sh"] = &ctx.Config{Name: "启动脚本", Value: arg[0], Help: "模块启动时自动运行的脚本"}
|
cli.Conf("init.sh", "启动脚本", arg[0], "模块启动时自动运行的脚本")
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream, ok := m.Data["io"]; ok {
|
if stream, ok := m.Data["io"]; ok {
|
||||||
@ -214,7 +214,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
cli.history = append(cli.history, map[string]string{
|
cli.history = append(cli.history, map[string]string{
|
||||||
"time": time.Now().Format("15:04:05"),
|
"time": time.Now().Format("15:04:05"),
|
||||||
"index": fmt.Sprintf("%d", len(cli.history)),
|
"index": fmt.Sprintf("%d", len(cli.history)),
|
||||||
"cli": strings.Join(arg, " "),
|
"cli": strings.Join(msg.Meta["detail"], " "),
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
@ -296,7 +296,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
target := m.Target.Root
|
target := m.Target
|
||||||
method := "search"
|
method := "search"
|
||||||
action := "switch"
|
action := "switch"
|
||||||
which := ""
|
which := ""
|
||||||
@ -350,18 +350,17 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
|||||||
cli.target = v
|
cli.target = v
|
||||||
case "spawn":
|
case "spawn":
|
||||||
msg := m.Spawn(v)
|
msg := m.Spawn(v)
|
||||||
v.Spawn(msg, args[0], args[1:]...)
|
v.Spawn(msg, args[0], args[1:]...).Begin(msg)
|
||||||
v.Begin(msg)
|
|
||||||
case "start":
|
case "start":
|
||||||
m.Spawn(v).Start(args...)
|
m.Message.Spawn(v, args[0]).Start(args...)
|
||||||
case "show":
|
case "show":
|
||||||
m.Echo("%s: %s\n", v.Name, v.Help)
|
m.Echo("%s: %s\n", v.Name, v.Help)
|
||||||
m.Echo("引用模块:\n")
|
m.Echo("引用模块:\n")
|
||||||
for k, v := range v.Session {
|
for k, v := range v.Sessions {
|
||||||
m.Echo("\t%s(%d): %s %s\n", k, v.Code, v.Target.Name, v.Target.Help)
|
m.Echo("\t%s(%d): %s %s\n", k, v.Code, v.Target.Name, v.Target.Help)
|
||||||
}
|
}
|
||||||
m.Echo("索引模块:\n")
|
m.Echo("索引模块:\n")
|
||||||
for i, v := range v.Resource {
|
for i, v := range v.Requests {
|
||||||
m.Echo("\t%d(%d): %s %s\n", i, v.Code, v.Context.Name, v.Context.Help)
|
m.Echo("\t%d(%d): %s %s\n", i, v.Code, v.Context.Name, v.Context.Help)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -604,7 +603,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
|||||||
}},
|
}},
|
||||||
"open": &ctx.Command{Name: "open address protocl", Help: "建立远程连接",
|
"open": &ctx.Command{Name: "open address protocl", Help: "建立远程连接",
|
||||||
Options: map[string]string{"io": "读写流"},
|
Options: map[string]string{"io": "读写流"},
|
||||||
Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string {
|
Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { // {{{
|
||||||
if m.Has("io") {
|
if m.Has("io") {
|
||||||
m.Start(fmt.Sprintf("PTS%d", c.Capi("nterm")), arg[1])
|
m.Start(fmt.Sprintf("PTS%d", c.Capi("nterm")), arg[1])
|
||||||
} else {
|
} else {
|
||||||
@ -612,7 +611,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
|
|||||||
case "tcp":
|
case "tcp":
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// {{{
|
|
||||||
return ""
|
return ""
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
|
@ -67,7 +67,7 @@ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
|||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
Target: c,
|
Target: c,
|
||||||
Message: m,
|
Message: m,
|
||||||
Root: Pulse,
|
Root: m.Root,
|
||||||
}
|
}
|
||||||
msg.Context = m.Target
|
msg.Context = m.Target
|
||||||
|
|
||||||
@ -81,10 +81,10 @@ func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
|
|||||||
m.Messages = append(m.Messages, msg)
|
m.Messages = append(m.Messages, msg)
|
||||||
msg.Code = m.Capi("nmessage", 1)
|
msg.Code = m.Capi("nmessage", 1)
|
||||||
|
|
||||||
if msg.Session == nil {
|
if msg.Sessions == nil {
|
||||||
msg.Session = make(map[string]*Message)
|
msg.Sessions = make(map[string]*Message)
|
||||||
}
|
}
|
||||||
msg.Session[key[0]] = msg
|
msg.Sessions[key[0]] = msg
|
||||||
msg.Name = key[0]
|
msg.Name = key[0]
|
||||||
|
|
||||||
log.Printf("%d spawn %d: %s.%s->%s.%d", m.Code, msg.Code, msg.Context.Name, msg.Name, msg.Target.Name, msg.Index)
|
log.Printf("%d spawn %d: %s.%s->%s.%d", m.Code, msg.Code, msg.Context.Name, msg.Name, msg.Target.Name, msg.Index)
|
||||||
@ -221,6 +221,9 @@ func (m *Message) Post(c *Context, arg ...string) bool { // {{{
|
|||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Start(arg ...string) bool { // {{{
|
func (m *Message) Start(arg ...string) bool { // {{{
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
|
if m.Meta == nil {
|
||||||
|
m.Meta = make(map[string][]string)
|
||||||
|
}
|
||||||
m.Meta["detail"] = arg
|
m.Meta["detail"] = arg
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,11 +255,11 @@ type Context struct {
|
|||||||
Message *Message
|
Message *Message
|
||||||
Server
|
Server
|
||||||
|
|
||||||
Resource []*Message
|
Requests []*Message
|
||||||
Session map[string]*Message
|
Sessions map[string]*Message
|
||||||
|
|
||||||
Index map[string]*Context
|
Index map[string]*Context
|
||||||
Shadows map[string]*Context
|
Groups map[string]*Context
|
||||||
|
|
||||||
Contexts map[string]*Context
|
Contexts map[string]*Context
|
||||||
Context *Context
|
Context *Context
|
||||||
@ -329,8 +332,7 @@ func (c *Context) Register(s *Context, x Server) *Context { // {{{
|
|||||||
func (c *Context) Begin(m *Message) *Context { // {{{
|
func (c *Context) Begin(m *Message) *Context { // {{{
|
||||||
for k, x := range c.Configs {
|
for k, x := range c.Configs {
|
||||||
if x.Hand != nil {
|
if x.Hand != nil {
|
||||||
log.Printf("%s conf: %s(%s)", c.Name, k, x.Value)
|
c.Conf(k, x.Value)
|
||||||
x.Hand(c, x, x.Value)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,6 +343,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
|||||||
c.Server.Begin(m)
|
c.Server.Begin(m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,9 +360,9 @@ func (c *Context) Start(m *Message) bool { // {{{
|
|||||||
|
|
||||||
log.Printf("%d start(%d): %s %s", m.Code, Index.Capi("nserver", 1), c.Name, c.Help)
|
log.Printf("%d start(%d): %s %s", m.Code, Index.Capi("nserver", 1), c.Name, c.Help)
|
||||||
defer Index.Capi("nserver", -1)
|
defer Index.Capi("nserver", -1)
|
||||||
defer log.Printf("%d stop(%d): %s %s", m.Code, Index.Capi("nserver"), c.Name, c.Help)
|
defer log.Printf("%d stop(%s): %s %s", m.Code, Index.Cap("nserver"), c.Name, c.Help)
|
||||||
|
|
||||||
c.Resource = []*Message{m}
|
c.Requests = []*Message{m}
|
||||||
c.Server.Start(m, m.Meta["detail"]...)
|
c.Server.Start(m, m.Meta["detail"]...)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -425,7 +428,7 @@ func (c *Context) Deal(pre func(m *Message, arg ...string) bool, post func(m *Me
|
|||||||
// }}}
|
// }}}
|
||||||
func (c *Context) Exit(m *Message, arg ...string) { // {{{
|
func (c *Context) Exit(m *Message, arg ...string) { // {{{
|
||||||
if m.Target == c {
|
if m.Target == c {
|
||||||
for _, v := range c.Session {
|
for _, v := range c.Sessions {
|
||||||
if v.Name != "" {
|
if v.Name != "" {
|
||||||
v.Name = ""
|
v.Name = ""
|
||||||
v.Target.Exit(v, arg...)
|
v.Target.Exit(v, arg...)
|
||||||
@ -438,7 +441,7 @@ func (c *Context) Exit(m *Message, arg ...string) { // {{{
|
|||||||
log.Println(c.Name, c.Help, "exit: self", m.Code)
|
log.Println(c.Name, c.Help, "exit: self", m.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range c.Resource {
|
for _, v := range c.Requests {
|
||||||
if v.Index != -1 {
|
if v.Index != -1 {
|
||||||
v.Context.Exit(v, arg...)
|
v.Context.Exit(v, arg...)
|
||||||
log.Println(c.Name, c.Help, "exit: resource", v.Code, v.Context.Name, v.Context.Help)
|
log.Println(c.Name, c.Help, "exit: resource", v.Code, v.Context.Name, v.Context.Help)
|
||||||
@ -470,18 +473,18 @@ func (c *Context) Add(arg ...string) { // {{{
|
|||||||
panic(errors.New(v.Name + "上下文已存在"))
|
panic(errors.New(v.Name + "上下文已存在"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Shadows == nil {
|
if c.Groups == nil {
|
||||||
c.Shadows = make(map[string]*Context)
|
c.Groups = make(map[string]*Context)
|
||||||
}
|
}
|
||||||
c.Shadows[arg[1]] = &Context{Name: arg[2], Help: arg[3], Index: c.Index}
|
c.Groups[arg[1]] = &Context{Name: arg[2], Help: arg[3], Index: c.Index}
|
||||||
c.Index[arg[1]] = c.Shadows[arg[1]]
|
c.Index[arg[1]] = c.Groups[arg[1]]
|
||||||
log.Println(c.Name, "add context:", arg[1:])
|
log.Println(c.Name, "add context:", arg[1:])
|
||||||
case "command":
|
case "command":
|
||||||
if len(arg) != 3 {
|
if len(arg) != 3 {
|
||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
if v.Commands == nil {
|
if v.Commands == nil {
|
||||||
v.Commands = make(map[string]*Command)
|
v.Commands = make(map[string]*Command)
|
||||||
}
|
}
|
||||||
@ -502,7 +505,7 @@ func (c *Context) Add(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
if v.Configs == nil {
|
if v.Configs == nil {
|
||||||
v.Configs = make(map[string]*Config)
|
v.Configs = make(map[string]*Config)
|
||||||
}
|
}
|
||||||
@ -523,7 +526,7 @@ func (c *Context) Add(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
if v.Caches == nil {
|
if v.Caches == nil {
|
||||||
v.Caches = make(map[string]*Cache)
|
v.Caches = make(map[string]*Cache)
|
||||||
}
|
}
|
||||||
@ -552,14 +555,14 @@ func (c *Context) Del(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(c.Index, arg[1])
|
delete(c.Index, arg[1])
|
||||||
delete(c.Shadows, arg[1])
|
delete(c.Groups, arg[1])
|
||||||
log.Println(c.Name, "del context:", arg[1])
|
log.Println(c.Name, "del context:", arg[1])
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cs); i++ {
|
for i := 0; i < len(cs); i++ {
|
||||||
for k, v := range cs[i].Shadows {
|
for k, v := range cs[i].Groups {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(c.Index, k)
|
delete(c.Index, k)
|
||||||
log.Println(c.Name, "del context:", k)
|
log.Println(c.Name, "del context:", k)
|
||||||
@ -570,13 +573,13 @@ func (c *Context) Del(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Commands, arg[2])
|
delete(v.Commands, arg[2])
|
||||||
log.Println(v.Name, "del command:", arg[2])
|
log.Println(v.Name, "del command:", arg[2])
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cs); i++ {
|
for i := 0; i < len(cs); i++ {
|
||||||
for _, v := range cs[i].Shadows {
|
for _, v := range cs[i].Groups {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Commands, arg[2])
|
delete(v.Commands, arg[2])
|
||||||
log.Println(v.Name, "del command:", arg[2])
|
log.Println(v.Name, "del command:", arg[2])
|
||||||
@ -587,13 +590,13 @@ func (c *Context) Del(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Configs, arg[2])
|
delete(v.Configs, arg[2])
|
||||||
log.Println(v.Name, "del config:", arg[2])
|
log.Println(v.Name, "del config:", arg[2])
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cs); i++ {
|
for i := 0; i < len(cs); i++ {
|
||||||
for _, v := range cs[i].Shadows {
|
for _, v := range cs[i].Groups {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Configs, arg[2])
|
delete(v.Configs, arg[2])
|
||||||
log.Println(v.Name, "del config:", arg[2])
|
log.Println(v.Name, "del config:", arg[2])
|
||||||
@ -604,13 +607,13 @@ func (c *Context) Del(arg ...string) { // {{{
|
|||||||
panic(errors.New("参数错误"))
|
panic(errors.New("参数错误"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := c.Shadows[arg[1]]; ok {
|
if v, ok := c.Groups[arg[1]]; ok {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Caches, arg[2])
|
delete(v.Caches, arg[2])
|
||||||
log.Println(v.Name, "del cache:", arg[2])
|
log.Println(v.Name, "del cache:", arg[2])
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cs); i++ {
|
for i := 0; i < len(cs); i++ {
|
||||||
for _, v := range cs[i].Shadows {
|
for _, v := range cs[i].Groups {
|
||||||
cs = append(cs, v)
|
cs = append(cs, v)
|
||||||
delete(v.Caches, arg[2])
|
delete(v.Caches, arg[2])
|
||||||
log.Println(v.Name, "del cache:", arg[2])
|
log.Println(v.Name, "del cache:", arg[2])
|
||||||
@ -827,9 +830,9 @@ func (c *Context) Capi(key string, arg ...int) int { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
var Pulse = &Message{Code: 1, Time: time.Now(), Index: 0, Name: "root"}
|
var Pulse = &Message{Code: 1, Time: time.Now(), Name: "ctx", Index: 0}
|
||||||
|
|
||||||
var Index = &Context{Name: "ctx", Help: "所有模块的祖模块",
|
var Index = &Context{Name: "ctx", Help: "根模块",
|
||||||
Caches: map[string]*Cache{
|
Caches: map[string]*Cache{
|
||||||
"nserver": &Cache{Name: "服务数量", Value: "0", Help: "显示已经启动运行模块的数量"},
|
"nserver": &Cache{Name: "服务数量", Value: "0", Help: "显示已经启动运行模块的数量"},
|
||||||
"ncontext": &Cache{Name: "模块数量", Value: "1", Help: "显示功能树已经注册模块的数量"},
|
"ncontext": &Cache{Name: "模块数量", Value: "1", Help: "显示功能树已经注册模块的数量"},
|
||||||
@ -871,21 +874,25 @@ var Index = &Context{Name: "ctx", Help: "所有模块的祖模块",
|
|||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
|
|
||||||
"debug": &Config{Name: "调试模式(off/no)", Value: "off", Help: "是否打印错误信息,off:不打印,on:打印)"},
|
"ContextRequestSize": &Config{Name: "请求队列长度", Value: "10", Help: "每个模块可以被其它模块引用的的数量"},
|
||||||
"ContextSessionSize": &Config{Name: "会话队列长度", Value: "10", Help: "每个模块可以启动其它模块的数量"},
|
"ContextSessionSize": &Config{Name: "会话队列长度", Value: "10", Help: "每个模块可以启动其它模块的数量"},
|
||||||
"MessageQueueSize": &Config{Name: "消息队列长度", Value: "10", Help: "每个模块接收消息的队列长度"},
|
"MessageQueueSize": &Config{Name: "消息队列长度", Value: "10", Help: "每个模块接收消息的队列长度"},
|
||||||
|
|
||||||
|
"debug": &Config{Name: "调试模式(off/no)", Value: "off", Help: "是否打印错误信息,off:不打印,on:打印)"},
|
||||||
"cert": &Config{Name: "证书文件", Value: "etc/cert.pem", Help: "证书文件"},
|
"cert": &Config{Name: "证书文件", Value: "etc/cert.pem", Help: "证书文件"},
|
||||||
"key": &Config{Name: "私钥文件", Value: "etc/key.pem", Help: "私钥文件"},
|
"key": &Config{Name: "私钥文件", Value: "etc/key.pem", Help: "私钥文件"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*Command{
|
Commands: map[string]*Command{
|
||||||
"void": &Command{Name: "输出简单的信息", Help: "建立远程连接", Hand: func(c *Context, m *Message, key string, arg ...string) string {
|
"show": &Command{Name: "输出简单的信息", Help: "建立远程连接", Hand: func(c *Context, m *Message, key string, arg ...string) string {
|
||||||
m.Echo("hello void!\n") // {{{
|
m.Echo("nserver: %s\n", c.Cap("nserver")) // {{{
|
||||||
|
m.Echo("ncontext: %s\n", c.Cap("ncontext"))
|
||||||
|
m.Echo("nmessage: %s\n", c.Cap("nmessage"))
|
||||||
return ""
|
return ""
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Session: map[string]*Message{"root": Pulse},
|
Requests: []*Message{Pulse},
|
||||||
Resource: []*Message{Pulse},
|
Sessions: map[string]*Message{"root": Pulse},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -916,15 +923,11 @@ func Start() {
|
|||||||
Pulse.Context = Index
|
Pulse.Context = Index
|
||||||
Pulse.Wait = make(chan bool, 10)
|
Pulse.Wait = make(chan bool, 10)
|
||||||
|
|
||||||
cs := []*Context{Index}
|
Index.Travel(func(s *Context) bool {
|
||||||
for i := 0; i < len(cs); i++ {
|
s.Root = Index
|
||||||
cs[i].Root = Index
|
s.Begin(nil)
|
||||||
cs[i].Begin(nil)
|
return true
|
||||||
|
})
|
||||||
for _, v := range cs[i].Contexts {
|
|
||||||
cs = append(cs, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n := 0
|
n := 0
|
||||||
for _, s := range Index.Contexts {
|
for _, s := range Index.Contexts {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user