1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-25 16:58:06 +08:00

mac add Message.Assert 添加了消息的异常处理

This commit is contained in:
shaoying 2017-11-11 22:45:20 +08:00
parent bdc2fd7102
commit a2f4696979
4 changed files with 97 additions and 65 deletions

View File

@ -30,11 +30,11 @@ snippet c
*ctx.Context
}
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Begin(m *ctx.Message) ctx.Server {
return nil
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Begin(m *ctx.Message, arg ...string) ctx.Server {
return `Filename()`
}
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Start(m *ctx.Message) bool {
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Start(m *ctx.Message, arg ...string) bool {
return true
}

View File

@ -3,8 +3,6 @@ package cli // {{{
import ( // {{{
"bufio"
"context"
// _ "context/tcp"
// _ "context/web"
"fmt"
"io"
"os"
@ -350,9 +348,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端",
cli.target = v
case "spawn":
msg := m.Spawn(v)
v.Spawn(msg, args[0], args[1:]...).Begin(msg)
// msg.Add("detail", args[1:]...)
v.Spawn(msg, args[0]).Begin(msg)
case "start":
m.Message.Spawn(v, args[0]).Start(args...)
m.Message.Spawn(v, args[0]).Start(arg[0], args[1:]...)
case "show":
m.Echo("%s: %s\n", v.Name, v.Help)
m.Echo("引用模块:\n")

View File

@ -61,6 +61,22 @@ type Message struct {
Root *Message
}
func (m *Message) Assert(e error) bool { // {{{
if e != nil {
m.Add("result", "error:", fmt.Sprintln(e))
log.Println(m.Code, "error:", e)
if m.Conf("debug") == "on" {
fmt.Println(m.Code, "error:", e)
}
panic(e)
}
return true
}
// }}}
func (m *Message) Spawn(c *Context, key ...string) *Message { // {{{
msg := &Message{
@ -158,8 +174,10 @@ func (m *Message) Has(key string) bool { // {{{
// }}}
func (m *Message) Get(key string) string { // {{{
if meta, ok := m.Meta[key]; ok {
if len(meta) > 0 {
return meta[0]
}
}
return ""
}
@ -219,7 +237,7 @@ func (m *Message) Post(c *Context, arg ...string) bool { // {{{
}
// }}}
func (m *Message) Start(arg ...string) bool { // {{{
func (m *Message) Start(key string, arg ...string) bool { // {{{
if len(arg) > 0 {
if m.Meta == nil {
m.Meta = make(map[string][]string)
@ -227,7 +245,7 @@ func (m *Message) Start(arg ...string) bool { // {{{
m.Meta["detail"] = arg
}
go m.Target.Spawn(m, m.Meta["detail"][0], m.Meta["detail"][1:]...).Begin(m).Start(m)
m.Target.Spawn(m, key).Start(m)
return true
}
@ -349,6 +367,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
// }}}
func (c *Context) Start(m *Message) bool { // {{{
if _, ok := c.Caches["status"]; !ok {
c.Caches["status"] = &Cache{Name: "服务状态", Value: "stop", Help: "服务状态start:正在运行stop:未在运行"}
}
@ -371,11 +390,11 @@ func (c *Context) Start(m *Message) bool { // {{{
}
// }}}
func (c *Context) Spawn(m *Message, key string, arg ...string) *Context { // {{{
func (c *Context) Spawn(m *Message, key string) *Context { // {{{
s := &Context{Name: key, Help: c.Help}
m.Target = s
if c.Server != nil {
c.Register(s, c.Server.Spawn(s, m, arg...)).Begin(m)
c.Register(s, c.Server.Spawn(s, m, m.Meta["detail"]...)).Begin(m)
} else {
c.Register(s, nil).Begin(m)
}
@ -402,10 +421,10 @@ func (c *Context) Deal(pre func(m *Message, arg ...string) bool, post func(m *Me
arg := m.Meta["detail"]
c.AssertOne(m, true, func(c *Context, m *Message) {
m.Add("result", c.Cmd(m, arg[0], arg[1:]...))
m.Cmd()
}, func(c *Context, m *Message) {
m.Add("result", m.Cmd())
c.Cmd(m, arg[0], arg[1:]...)
}, func(c *Context, m *Message) {
log.Printf("system command(%s->%s): %v", m.Context.Name, m.Target.Name, arg)
@ -427,7 +446,20 @@ func (c *Context) Deal(pre func(m *Message, arg ...string) bool, post func(m *Me
// }}}
func (c *Context) Exit(m *Message, arg ...string) { // {{{
log.Println(c.Sessions)
log.Println(c.Requests)
if m.Target == c {
if m.Index == -1 {
log.Println(c.Name, c.Help, "exit: resource", m.Code, m.Context.Name, m.Context.Help)
if c.Server != nil {
c.Server.Exit(m, arg...)
log.Println(c.Name, c.Help, "exit: self", m.Code)
}
return
}
m.Index = -1
for _, v := range c.Sessions {
if v.Name != "" {
v.Name = ""
@ -443,16 +475,17 @@ func (c *Context) Exit(m *Message, arg ...string) { // {{{
for _, v := range c.Requests {
if v.Index != -1 {
v.Index = -1
v.Context.Exit(v, arg...)
log.Println(c.Name, c.Help, "exit: resource", v.Code, v.Context.Name, v.Context.Help)
v.Index = -1
}
}
} else if m.Context == c {
if m.Name != "" {
m.Target.Exit(m, arg...)
log.Println(c.Name, c.Help, "exit: session", m.Code, m.Target.Name, m.Target.Help)
m.Name = ""
log.Println(c.Name, c.Help, "exit: session", m.Code, m.Target.Name, m.Target.Help)
if c.Server != nil {
c.Server.Exit(m, arg...)
log.Println(c.Name, c.Help, "exit: self", m.Code)
}
}
@ -690,29 +723,31 @@ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{
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 + "没有权限")))
}
for _, v := range m.Meta["option"] {
if _, ok := x.Options[v]; !ok {
panic(errors.New(fmt.Sprintf(v + "未知参数")))
panic(errors.New(fmt.Sprintf("未知参数:" + v)))
}
}
value := x.Hand(c, m, key, arg...)
m.Meta["result"] = nil
c.AssertOne(m, true, func(c *Context, m *Message) {
x.Hand(c, m, key, arg...)
})
if x.Appends != nil {
for _, v := range m.Meta["append"] {
if _, ok := x.Appends[v]; !ok {
panic(errors.New(fmt.Sprintf(v + "未知参数")))
panic(errors.New(fmt.Sprintf("未知参数:" + v)))
}
}
}
return value
return m.Get("result")
}
}
panic(errors.New(fmt.Sprintf(key + "命令项不存在")))
panic(errors.New(fmt.Sprintf("未知命令:" + key)))
}
// }}}
@ -878,7 +913,7 @@ var Index = &Context{Name: "ctx", Help: "根模块",
"ContextSessionSize": &Config{Name: "会话队列长度", Value: "10", Help: "每个模块可以启动其它模块的数量"},
"MessageQueueSize": &Config{Name: "消息队列长度", Value: "10", Help: "每个模块接收消息的队列长度"},
"debug": &Config{Name: "调试模式(off/no)", Value: "off", Help: "是否打印错误信息off:不打印on:打印)"},
"debug": &Config{Name: "调试模式(off/on)", Value: "off", Help: "是否打印错误信息off:不打印on:打印)"},
"cert": &Config{Name: "证书文件", Value: "etc/cert.pem", Help: "证书文件"},
"key": &Config{Name: "私钥文件", Value: "etc/key.pem", Help: "私钥文件"},
},
@ -896,52 +931,47 @@ var Index = &Context{Name: "ctx", Help: "根模块",
}
func init() {
if len(os.Args) > 1 {
Index.Conf("start", os.Args[1])
}
if len(os.Args) > 2 {
Index.Conf("init.sh", os.Args[2])
}
if len(os.Args) > 3 {
Index.Conf("bench.log", os.Args[3])
} else {
Index.Conf("bench.log", Index.Conf("bench.log"))
}
if len(os.Args) > 4 {
Index.Conf("root", os.Args[4])
}
log.Println("\n\n\n")
}
func Start() {
Pulse.Root = Pulse
Pulse.Target = Index
Pulse.Context = Index
Pulse.Wait = make(chan bool, 10)
}
func Start(args ...string) {
if len(args) > 0 {
Index.Conf("start", args[0])
}
if len(args) > 1 {
Index.Conf("init.sh", args[1])
}
if len(args) > 2 {
Index.Conf("bench.log", args[2])
} else {
Index.Conf("bench.log", Index.Conf("bench.log"))
}
if len(args) > 3 {
Index.Conf("root", args[3])
}
log.Println("\n\n\n")
Index.Travel(func(s *Context) bool {
s.Root = Index
s.Begin(nil)
return true
})
n := 0
if n := 0; Index.Conf("start") != "" {
for _, s := range Index.Contexts {
if ok, _ := regexp.MatchString(Index.Conf("start"), s.Name); ok {
n++
go s.Start(Pulse.Spawn(s, s.Name).Put("option", "io", os.Stdout))
n++
}
}
for {
for n > 0 || Index.Capi("nserver", 0) > 0 {
<-Pulse.Wait
n--
if n <= 0 && Index.Capi("nserver", 0) == 0 {
return
}
}
}

View File

@ -3,8 +3,11 @@ package main
import (
"context"
_ "context/cli"
_ "context/mdb"
_ "context/tcp"
"os"
)
func main() {
ctx.Start()
ctx.Start(os.Args[1:]...)
}