forked from x/ContextOS
mac pro ctx
This commit is contained in:
parent
62af8f9a2e
commit
5ca0bca18b
@ -59,7 +59,6 @@ func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
aaa.Context.Master(nil)
|
|
||||||
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: "", Help: "用户组"}
|
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: "", Help: "用户组"}
|
||||||
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: "", Help: "用户名"}
|
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: "", Help: "用户名"}
|
||||||
aaa.Caches["password"] = &ctx.Cache{Name: "用户密码", Value: "", Help: "用户密码,加密存储", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
aaa.Caches["password"] = &ctx.Cache{Name: "用户密码", Value: "", Help: "用户密码,加密存储", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||||
@ -98,10 +97,9 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
m.Cap("stream", m.Cap("username"))
|
m.Cap("stream", m.Cap("username"))
|
||||||
m.Cap("sessid", aaa.Session(arg[1]))
|
m.Cap("sessid", aaa.Session(arg[1]))
|
||||||
Pulse.Capi("nuser", 1)
|
Pulse.Capi("nuser", 1)
|
||||||
aaa.Owner = aaa.Context
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Log("info", m.Source(), "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
m.Log("info", "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +109,7 @@ func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
case m.Target():
|
case m.Target():
|
||||||
root := Pulse.Target().Server.(*AAA)
|
root := Pulse.Target().Server.(*AAA)
|
||||||
delete(root.sessions, m.Cap("sessid"))
|
delete(root.sessions, m.Cap("sessid"))
|
||||||
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
m.Log("info", "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||||
case m.Source():
|
case m.Source():
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,25 +129,25 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Target(c) // {{{
|
// m.Target(c) // {{{
|
||||||
aaa := c.Server.(*AAA)
|
aaa := c.Server.(*AAA)
|
||||||
|
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
case 0:
|
case 0:
|
||||||
m.Travel(c, func(m *ctx.Message) bool {
|
m.Travel(func(m *ctx.Message, i int) bool {
|
||||||
m.Echo("%s(%s): %s\n", m.Target().Name, m.Cap("group"), m.Cap("time"))
|
m.Echo("%s(%s): %s\n", m.Target().Name, m.Cap("group"), m.Cap("time"))
|
||||||
if int64(m.Capi("expire")) < time.Now().Unix() {
|
if int64(m.Capi("expire")) < time.Now().Unix() {
|
||||||
m.Target().Close(m)
|
m.Target().Close(m)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
}, c)
|
||||||
case 1:
|
case 1:
|
||||||
s, ok := aaa.sessions[arg[0]]
|
_, ok := aaa.sessions[arg[0]]
|
||||||
m.Assert(ok, "会话失败")
|
m.Assert(ok, "会话失败")
|
||||||
m.Target(s)
|
// m.Target(s)
|
||||||
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
||||||
|
|
||||||
m.Log("info", m.Source(), "logon %s %s", m.Cap("username"), m.Cap("group"))
|
m.Log("info", "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||||
m.Echo(m.Cap("username"))
|
m.Echo(m.Cap("username"))
|
||||||
|
|
||||||
m.Append("username", m.Cap("username"))
|
m.Append("username", m.Cap("username"))
|
||||||
@ -165,17 +163,14 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
msg := m
|
msg := m
|
||||||
if username == Pulse.Conf("rootname") {
|
if username == Pulse.Conf("rootname") {
|
||||||
msg = Pulse.Spawn(Pulse.Target())
|
msg = Pulse.Spawn(Pulse.Target())
|
||||||
ctx.Index.Sessions["aaa"] = msg
|
|
||||||
msg.Set("detail", group, username).Target().Start(msg)
|
msg.Set("detail", group, username).Target().Start(msg)
|
||||||
} else if msg = Pulse.Find(username, false); msg == nil {
|
} else if msg = Pulse.Find(username, false); msg == nil {
|
||||||
m.Start(username, "认证用户", group, username)
|
m.Start(username, "认证用户", group, username)
|
||||||
msg = m
|
msg = m
|
||||||
} else {
|
} else {
|
||||||
m.Target(msg.Target())
|
// m.Target(msg.Target())
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Target().Sessions["aaa"] = msg
|
|
||||||
|
|
||||||
msg.Cap("password", password)
|
msg.Cap("password", password)
|
||||||
|
|
||||||
aaa.sessions[m.Cap("sessid")] = msg.Target()
|
aaa.sessions[m.Cap("sessid")] = msg.Target()
|
||||||
@ -197,15 +192,15 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
group := m.Sess("aaa").Cap("group")
|
group := m.Sesss("aaa").Cap("group")
|
||||||
m.Travel(c, func(msg *ctx.Message) bool {
|
m.Travel(func(msg *ctx.Message, i int) bool {
|
||||||
aaa := msg.Target().Server.(*AAA)
|
aaa := msg.Target().Server.(*AAA)
|
||||||
if aaa.share == nil {
|
if aaa.share == nil {
|
||||||
aaa.share = make(map[string]*ctx.Context)
|
aaa.share = make(map[string]*ctx.Context)
|
||||||
}
|
}
|
||||||
aaa.share[group] = m.Target()
|
aaa.share[group] = m.Target()
|
||||||
return true
|
return true
|
||||||
})
|
}, c)
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"md5": &ctx.Command{Name: "md5 [file filename][content]", Help: "散列",
|
"md5": &ctx.Command{Name: "md5 [file filename][content]", Help: "散列",
|
||||||
|
@ -211,7 +211,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
if _, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
if _, ok := m.Target().Server.(*CLI); m.Assert(ok) { // {{{
|
||||||
m.Start(m.Confx("cli_name", arg, 2), m.Confx("cli_help", arg, 3), key, arg[0])
|
m.Start(m.Confx("cli_name", arg, 2), m.Confx("cli_help", arg, 3), key, arg[0])
|
||||||
if len(arg) < 2 || arg[1] != "async" {
|
if len(arg) < 2 || arg[1] != "async" {
|
||||||
<-m.Target().Exit
|
m.Target().Wait()
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
}},
|
}},
|
||||||
@ -274,7 +274,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
m.Echo("%s not exist", arg[1])
|
m.Echo("%s not exist", arg[1])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.Log("info", nil, "import %s", arg[1])
|
m.Log("info", "import %s", arg[1])
|
||||||
module := msg.Cap("module")
|
module := msg.Cap("module")
|
||||||
for k, _ := range msg.Target().Commands {
|
for k, _ := range msg.Target().Commands {
|
||||||
if len(arg) == 2 {
|
if len(arg) == 2 {
|
||||||
@ -292,16 +292,16 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
default:
|
default:
|
||||||
cli.alias[arg[0]] = arg[1:]
|
cli.alias[arg[0]] = arg[1:]
|
||||||
m.Echo("%s: %v\n", arg[0], cli.alias[arg[0]])
|
m.Echo("%s: %v\n", arg[0], cli.alias[arg[0]])
|
||||||
m.Log("info", nil, "%s: %v", arg[0], cli.alias[arg[0]])
|
m.Log("info", "%s: %v", arg[0], cli.alias[arg[0]])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
}},
|
}},
|
||||||
"sleep": &ctx.Command{Name: "sleep time", Help: "睡眠, time(ns/us/ms/s/m/h): 时间值(纳秒/微秒/毫秒/秒/分钟/小时)", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"sleep": &ctx.Command{Name: "sleep time", Help: "睡眠, time(ns/us/ms/s/m/h): 时间值(纳秒/微秒/毫秒/秒/分钟/小时)", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
if d, e := time.ParseDuration(arg[0]); m.Assert(e) { // {{{
|
if d, e := time.ParseDuration(arg[0]); m.Assert(e) { // {{{
|
||||||
m.Log("info", nil, "sleep %v", d)
|
m.Log("info", "sleep %v", d)
|
||||||
time.Sleep(d)
|
time.Sleep(d)
|
||||||
m.Log("info", nil, "sleep %v done", d)
|
m.Log("info", "sleep %v done", d)
|
||||||
} // }}}
|
} // }}}
|
||||||
}},
|
}},
|
||||||
"time": &ctx.Command{
|
"time": &ctx.Command{
|
||||||
@ -710,7 +710,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
m.Cap("ps_target", cli.target.Name)
|
m.Cap("ps_target", cli.target.Name)
|
||||||
} else {
|
} else {
|
||||||
msg.Hand = true
|
msg.Hand = true
|
||||||
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
msg.Log("system", "%v", msg.Meta["detail"])
|
||||||
|
|
||||||
msg.Set("result").Set("append")
|
msg.Set("result").Set("append")
|
||||||
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
||||||
|
@ -89,23 +89,16 @@ type Context struct {
|
|||||||
Caches map[string]*Cache
|
Caches map[string]*Cache
|
||||||
Configs map[string]*Config
|
Configs map[string]*Config
|
||||||
Commands map[string]*Command
|
Commands map[string]*Command
|
||||||
|
|
||||||
root *Context
|
|
||||||
context *Context
|
|
||||||
contexts map[string]*Context
|
|
||||||
|
|
||||||
Requests []*Message
|
|
||||||
Sessions []*Message
|
|
||||||
|
|
||||||
//TODO: delete
|
|
||||||
master *Context
|
|
||||||
messages chan *Message
|
|
||||||
Pulse *Message
|
|
||||||
Historys []*Message
|
|
||||||
Owner *Context
|
|
||||||
Index map[string]*Context
|
Index map[string]*Context
|
||||||
|
|
||||||
Exit chan bool
|
requests []*Message
|
||||||
|
sessions []*Message
|
||||||
|
|
||||||
|
contexts map[string]*Context
|
||||||
|
context *Context
|
||||||
|
root *Context
|
||||||
|
|
||||||
|
exit chan bool
|
||||||
Server
|
Server
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,17 +145,10 @@ func (c *Context) Begin(m *Message, arg ...string) *Context { // {{{
|
|||||||
c.Caches["status"] = &Cache{Name: "status(begin/start/close)", Value: "begin", Help: "模块状态,begin:初始完成,start:正在运行,close:未在运行"}
|
c.Caches["status"] = &Cache{Name: "status(begin/start/close)", Value: "begin", Help: "模块状态,begin:初始完成,start:正在运行,close:未在运行"}
|
||||||
c.Caches["stream"] = &Cache{Name: "stream", Value: "", Help: "模块数据"}
|
c.Caches["stream"] = &Cache{Name: "stream", Value: "", Help: "模块数据"}
|
||||||
|
|
||||||
c.Requests = append(c.Requests, m)
|
c.requests = append(c.requests, m)
|
||||||
m.source.Sessions = append(m.source.Sessions, m)
|
m.source.sessions = append(m.source.sessions, m)
|
||||||
|
|
||||||
//TODO: delete
|
m.Log("begin", "%d context %v %v", m.root.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"])
|
||||||
m.Index = 1
|
|
||||||
c.Pulse = m
|
|
||||||
c.Historys = []*Message{m}
|
|
||||||
c.master = m.master.master
|
|
||||||
c.Owner = m.master.Owner
|
|
||||||
|
|
||||||
m.Log("begin", nil, "%d context %v %v", m.root.Capi("ncontext", 1), m.Meta["detail"], m.Meta["option"])
|
|
||||||
for k, x := range c.Configs {
|
for k, x := range c.Configs {
|
||||||
if x.Hand != nil {
|
if x.Hand != nil {
|
||||||
m.Conf(k, x.Value)
|
m.Conf(k, x.Value)
|
||||||
@ -182,16 +168,17 @@ func (c *Context) Start(m *Message, arg ...string) bool { // {{{
|
|||||||
m.Meta["detail"] = arg
|
m.Meta["detail"] = arg
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Requests = append(c.Requests, m)
|
c.requests = append(c.requests, m)
|
||||||
|
m.source.sessions = append(m.source.sessions, m)
|
||||||
if m.Hand = true; m.Cap("status") == "start" {
|
if m.Hand = true; m.Cap("status") == "start" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
running := make(chan bool)
|
running := make(chan bool)
|
||||||
go m.TryCatch(m, true, func(m *Message) {
|
go m.TryCatch(m, true, func(m *Message) {
|
||||||
m.Log(m.Cap("status", "start"), nil, "%d server %v %v", m.root.Capi("nserver", 1), m.Meta["detail"], m.Meta["option"])
|
m.Log(m.Cap("status", "start"), "%d server %v %v", m.root.Capi("nserver", 1), m.Meta["detail"], m.Meta["option"])
|
||||||
c.Exit = make(chan bool, 1)
|
|
||||||
|
|
||||||
|
c.exit = make(chan bool, 1)
|
||||||
if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) {
|
if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) {
|
||||||
c.Close(m, m.Meta["detail"]...)
|
c.Close(m, m.Meta["detail"]...)
|
||||||
}
|
}
|
||||||
@ -201,58 +188,45 @@ func (c *Context) Start(m *Message, arg ...string) bool { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
func (c *Context) Close(m *Message, arg ...string) bool { // {{{
|
||||||
m.Log("close", c, "%d:%d %v", len(m.source.Sessions), len(m.target.Historys), arg)
|
m.Log("close", "%d %v", len(c.requests), arg)
|
||||||
|
|
||||||
if m.target == c {
|
if m.target == c {
|
||||||
if m.Index == 0 {
|
for i := len(c.requests) - 1; i >= 0; i-- {
|
||||||
for i := len(c.Requests) - 1; i >= 0; i-- {
|
if msg := c.requests[i]; msg.code == m.code {
|
||||||
v := c.Requests[i]
|
if msg.source == c || c.Server == nil || c.Server.Close(m, arg...) {
|
||||||
if v.Index = -1; v.source != c && !v.source.Close(v, arg...) {
|
for j := i + 1; j < len(c.requests)-1; j++ {
|
||||||
v.Index = i
|
c.requests[j] = c.requests[j+1]
|
||||||
return false
|
}
|
||||||
}
|
}
|
||||||
c.Requests = c.Requests[:i]
|
|
||||||
}
|
}
|
||||||
} else if m.Index > 0 {
|
|
||||||
for i := m.Index - 1; i < len(c.Requests)-1; i++ {
|
|
||||||
c.Requests[i] = c.Requests[i+1]
|
|
||||||
}
|
|
||||||
c.Requests = c.Requests[:len(c.Requests)-1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Server != nil && !c.Server.Close(m, arg...) {
|
if len(c.requests) > 0 {
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.source == c && m.target != c {
|
|
||||||
if _, ok := c.Sessions[m.Name]; ok {
|
|
||||||
delete(c.Sessions, m.Name)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(c.Requests) > 1 {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Cap("status") == "start" {
|
if m.Cap("status") == "start" {
|
||||||
m.Log(m.Cap("status", "close"), nil, "%d server %v", m.root.Capi("nserver", -1)+1, arg)
|
m.Log(m.Cap("status", "close"), "%d server %v", m.root.Capi("nserver", -1)+1, arg)
|
||||||
for _, v := range c.Sessions {
|
for _, msg := range c.sessions {
|
||||||
if v.target != c {
|
if msg.target != c {
|
||||||
v.target.Close(v, arg...)
|
msg.target.Close(msg, arg...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if m.Index == 0 && c.context != nil && len(c.contexts) == 0 {
|
|
||||||
if c.context != nil {
|
if c.context != nil {
|
||||||
m.Log("close", nil, "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
m.Log("close", "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
|
||||||
delete(c.context.contexts, c.Name)
|
delete(c.context.contexts, c.Name)
|
||||||
c.context = nil
|
c.Wait()
|
||||||
if c.Exit != nil {
|
}
|
||||||
c.Exit <- true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (c *Context) Wait() bool { // {{{
|
||||||
|
if c.exit != nil {
|
||||||
|
return <-c.exit
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -263,17 +237,6 @@ func (c *Context) Context() *Context { // {{{
|
|||||||
return c.context
|
return c.context
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (c *Context) Master(s ...*Context) *Context { // {{{
|
|
||||||
if len(s) > 0 {
|
|
||||||
switch s[0] {
|
|
||||||
case nil, c:
|
|
||||||
c.master = s[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return c.master
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (c *Context) Has(key ...string) bool { // {{{
|
func (c *Context) Has(key ...string) bool { // {{{
|
||||||
switch len(key) {
|
switch len(key) {
|
||||||
@ -300,36 +263,23 @@ func (c *Context) Has(key ...string) bool { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
type Callback struct {
|
|
||||||
ncall int
|
|
||||||
hand func(msg *Message) (sub *Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Message struct {
|
type Message struct {
|
||||||
code int
|
code int
|
||||||
time time.Time
|
time time.Time
|
||||||
|
|
||||||
Name string
|
|
||||||
source *Context
|
source *Context
|
||||||
master *Context
|
|
||||||
target *Context
|
target *Context
|
||||||
Index int
|
|
||||||
|
|
||||||
|
Hand bool
|
||||||
Meta map[string][]string
|
Meta map[string][]string
|
||||||
Data map[string]interface{}
|
Data map[string]interface{}
|
||||||
|
|
||||||
Sessions map[string]*Message
|
callback func(msg *Message) (sub *Message)
|
||||||
|
sessions map[string]*Message
|
||||||
|
|
||||||
messages []*Message
|
messages []*Message
|
||||||
message *Message
|
message *Message
|
||||||
root *Message
|
root *Message
|
||||||
|
|
||||||
callback Callback
|
|
||||||
|
|
||||||
Wait chan bool
|
|
||||||
Recv chan bool
|
|
||||||
Hand bool
|
|
||||||
|
|
||||||
Template *Message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) Code() int { // {{{
|
func (m *Message) Code() int { // {{{
|
||||||
@ -342,41 +292,196 @@ func (m *Message) Message() *Message { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Source(s ...*Context) *Context { // {{{
|
func (m *Message) Source() *Context { // {{{
|
||||||
if len(s) > 0 {
|
|
||||||
m.source = s[0]
|
|
||||||
}
|
|
||||||
return m.source
|
return m.source
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Master(s ...*Context) *Context { // {{{
|
func (m *Message) Target() *Context { // {{{
|
||||||
if len(s) > 0 && s[0] == m.source {
|
|
||||||
m.master = m.source
|
|
||||||
}
|
|
||||||
return m.master
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Target(s ...*Context) *Context { // {{{
|
|
||||||
if len(s) > 0 {
|
|
||||||
m.target = s[0]
|
|
||||||
}
|
|
||||||
return m.target
|
return m.target
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Format() string { // {{{
|
func (m *Message) Format() string { // {{{
|
||||||
name := fmt.Sprintf("%s->%s", m.source.Name, m.target.Name)
|
return fmt.Sprintf("%d(%s->%s): %s %v", m.code, m.source.Name, m.target.Name, m.time.Format("15:04:05"), m.Meta["detail"], m.Meta["option"])
|
||||||
if m.Name != "" {
|
|
||||||
name = fmt.Sprintf("%s.%s->%s.%d", m.source.Name, m.Name, m.target.Name, m.Index)
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%d(%s): %s %v", m.code, name, m.time.Format("15:04:05"), m.Meta["detail"])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
func (m *Message) Log(action string, ctx *Context, str string, arg ...interface{}) *Message { // {{{
|
func (m *Message) BackTrace(hand func(m *Message) bool, c ...*Context) { // {{{
|
||||||
|
target := m.target
|
||||||
|
if len(c) > 0 {
|
||||||
|
target = c[0]
|
||||||
|
}
|
||||||
|
for s := target; s != nil; s = s.context {
|
||||||
|
if m.target = s; !hand(m) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.target = target
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Travel(hand func(m *Message, i int) bool, c ...*Context) { // {{{
|
||||||
|
target := m.target
|
||||||
|
if len(c) > 0 {
|
||||||
|
m.target = c[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
cs := []*Context{m.target}
|
||||||
|
for i := 0; i < len(cs); i++ {
|
||||||
|
if m.target = cs[i]; !hand(m, i) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := []string{}
|
||||||
|
for k, _ := range cs[i].contexts {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
for _, k := range keys {
|
||||||
|
cs = append(cs, cs[i].contexts[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.target = target
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
func (m *Message) Spawn(arg ...*Context) *Message { // {{{
|
||||||
|
c := m.target
|
||||||
|
if len(arg) > 0 {
|
||||||
|
c = arg[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := &Message{
|
||||||
|
code: m.root.Capi("nmessage", 1),
|
||||||
|
time: time.Now(),
|
||||||
|
source: m.target,
|
||||||
|
target: c,
|
||||||
|
message: m,
|
||||||
|
root: m.root,
|
||||||
|
}
|
||||||
|
|
||||||
|
m.messages = append(m.messages, msg)
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
||||||
|
target := m.target.root
|
||||||
|
if len(root) > 0 && !root[0] {
|
||||||
|
target = m.target
|
||||||
|
}
|
||||||
|
|
||||||
|
cs := target.contexts
|
||||||
|
for _, v := range strings.Split(name, ".") {
|
||||||
|
if x, ok := cs[v]; ok {
|
||||||
|
target, cs = x, x.contexts
|
||||||
|
} else if target.Name == v {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
m.Log("find", "not find %s", v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.Log("find", "find %s", name)
|
||||||
|
return m.Spawn(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
||||||
|
reg, e := regexp.Compile(key)
|
||||||
|
m.Assert(e)
|
||||||
|
|
||||||
|
target := m.target
|
||||||
|
if len(root) > 0 && root[0] {
|
||||||
|
target = m.target.root
|
||||||
|
}
|
||||||
|
|
||||||
|
cs := make([]*Context, 0, 3)
|
||||||
|
m.Travel(func(m *Message, i int) bool {
|
||||||
|
if reg.MatchString(m.target.Name) || reg.FindString(m.target.Help) != "" {
|
||||||
|
m.Log("search", "%d match [%s]", len(cs)+1, key)
|
||||||
|
cs = append(cs, m.target)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}, target)
|
||||||
|
|
||||||
|
ms := make([]*Message, len(cs))
|
||||||
|
for i := 0; i < len(cs); i++ {
|
||||||
|
ms[i] = m.Spawn(cs[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return ms
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Sesss(key string, arg ...interface{}) *Message { // {{{
|
||||||
|
spawn := true
|
||||||
|
if _, ok := m.sessions[key]; !ok && len(arg) > 0 {
|
||||||
|
if m.sessions == nil {
|
||||||
|
m.sessions = make(map[string]*Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch value := arg[0].(type) {
|
||||||
|
case *Message:
|
||||||
|
m.sessions[key] = value
|
||||||
|
return m.sessions[key]
|
||||||
|
case *Context:
|
||||||
|
m.sessions[key] = m.Spawn(value)
|
||||||
|
return m.sessions[key]
|
||||||
|
case string:
|
||||||
|
root := true
|
||||||
|
if len(arg) > 2 {
|
||||||
|
root = Right(arg[2].(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
method := "find"
|
||||||
|
if len(arg) > 1 {
|
||||||
|
method = arg[1].(string)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch method {
|
||||||
|
case "find":
|
||||||
|
m.sessions[key] = m.Find(value, root)
|
||||||
|
case "search":
|
||||||
|
m.sessions[key] = m.Search(value, root)[0]
|
||||||
|
}
|
||||||
|
return m.sessions[key]
|
||||||
|
case bool:
|
||||||
|
spawn = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for msg := m; msg != nil; msg = msg.message {
|
||||||
|
if x, ok := msg.sessions[key]; ok {
|
||||||
|
if spawn {
|
||||||
|
x = m.Spawn(x.target)
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Tree(code int) *Message { // {{{
|
||||||
|
ms := []*Message{m}
|
||||||
|
for i := 0; i < len(ms); i++ {
|
||||||
|
ms = append(ms, ms[i].messages...)
|
||||||
|
if ms[i].Code() == code {
|
||||||
|
return ms[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
func (m *Message) Log(action string, str string, arg ...interface{}) *Message { // {{{
|
||||||
if !m.Options("log") {
|
if !m.Options("log") {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@ -439,7 +544,7 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
|
|||||||
e = strings.Join(v.Meta["result"][1:], "")
|
e = strings.Join(v.Meta["result"][1:], "")
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Log("error", nil, "error: %v", e)
|
msg.Log("error", "error: %v", e)
|
||||||
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
||||||
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.target.Name, e)
|
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.target.Name, e)
|
||||||
debug.PrintStack()
|
debug.PrintStack()
|
||||||
@ -463,246 +568,25 @@ func (m *Message) TryCatch(msg *Message, safe bool, hand ...func(msg *Message))
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
func (m *Message) Spawn(arg ...interface{}) *Message { // {{{
|
|
||||||
c := m.target
|
|
||||||
if len(arg) > 0 {
|
|
||||||
if v, ok := arg[0].(*Context); ok {
|
|
||||||
c, arg = v, arg[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
key := ""
|
|
||||||
if len(arg) > 0 {
|
|
||||||
if v, ok := arg[0].(string); ok {
|
|
||||||
key, arg = v, arg[1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := &Message{
|
|
||||||
code: m.root.Capi("nmessage", 1),
|
|
||||||
time: time.Now(),
|
|
||||||
message: m,
|
|
||||||
root: m.root,
|
|
||||||
source: m.target,
|
|
||||||
master: m.target,
|
|
||||||
target: c,
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.messages == nil {
|
|
||||||
m.messages = make([]*Message, 0, 10)
|
|
||||||
}
|
|
||||||
m.messages = append(m.messages, msg)
|
|
||||||
msg.Sessions = make(map[string]*Message)
|
|
||||||
|
|
||||||
msg.Wait = make(chan bool)
|
|
||||||
if key == "" {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.source.Sessions == nil {
|
|
||||||
msg.source.Sessions = make(map[string]*Message)
|
|
||||||
}
|
|
||||||
msg.source.Sessions[key] = msg
|
|
||||||
msg.Name = key
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Tree(code int) *Message { // {{{
|
|
||||||
ms := []*Message{m}
|
|
||||||
for i := 0; i < len(ms); i++ {
|
|
||||||
ms = append(ms, ms[i].messages...)
|
|
||||||
if ms[i].Code() == code {
|
|
||||||
return ms[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
func (m *Message) BackTrace(hand func(m *Message) bool) { // {{{
|
|
||||||
target := m.target
|
|
||||||
for s := target; s != nil; s = s.context {
|
|
||||||
if m.target = s; !hand(m) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.target = target
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Travel(c *Context, hand func(m *Message) bool) { // {{{
|
|
||||||
if c == nil {
|
|
||||||
c = m.target
|
|
||||||
}
|
|
||||||
target := m.target
|
|
||||||
|
|
||||||
cs := []*Context{c}
|
|
||||||
for i := 0; i < len(cs); i++ {
|
|
||||||
if m.target = cs[i]; !hand(m) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
keys := []string{}
|
|
||||||
for k, _ := range cs[i].contexts {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
sort.Strings(keys)
|
|
||||||
for _, k := range keys {
|
|
||||||
cs = append(cs, cs[i].contexts[k])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.target = target
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
|
||||||
reg, e := regexp.Compile(key)
|
|
||||||
m.Assert(e)
|
|
||||||
|
|
||||||
target := m.target
|
|
||||||
if len(root) > 0 && root[0] {
|
|
||||||
target = m.target.root
|
|
||||||
}
|
|
||||||
|
|
||||||
cs := make([]*Context, 0, 3)
|
|
||||||
m.Travel(target, func(m *Message) bool {
|
|
||||||
if reg.MatchString(m.target.Name) || reg.FindString(m.target.Help) != "" {
|
|
||||||
m.Log("search", nil, "%d match [%s]", len(cs)+1, key)
|
|
||||||
cs = append(cs, m.target)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
ms := make([]*Message, len(cs))
|
|
||||||
for i := 0; i < len(cs); i++ {
|
|
||||||
ms[i] = m.Spawn(cs[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return ms
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
|
||||||
target := m.target.root
|
|
||||||
if len(root) > 0 && !root[0] {
|
|
||||||
target = m.target
|
|
||||||
}
|
|
||||||
|
|
||||||
cs := target.contexts
|
|
||||||
for _, v := range strings.Split(name, ".") {
|
|
||||||
if x, ok := cs[v]; ok {
|
|
||||||
target, cs = x, x.contexts
|
|
||||||
} else if target.Name == v {
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
m.Log("find", target, "not find %s", v)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.Log("find", nil, "find %s", name)
|
|
||||||
return m.Spawn(target)
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Sess(key string, arg ...string) *Message { // {{{
|
|
||||||
|
|
||||||
if _, ok := m.target.Sessions[key]; !ok && len(arg) > 0 {
|
|
||||||
root := true
|
|
||||||
if len(arg) > 2 {
|
|
||||||
root = Right(arg[2])
|
|
||||||
}
|
|
||||||
method := "find"
|
|
||||||
if len(arg) > 1 {
|
|
||||||
method = arg[1]
|
|
||||||
}
|
|
||||||
switch method {
|
|
||||||
case "find":
|
|
||||||
m.target.Sessions[key] = m.Find(arg[0], root)
|
|
||||||
case "search":
|
|
||||||
m.target.Sessions[key] = m.Search(arg[0], root)[0]
|
|
||||||
}
|
|
||||||
return m.target.Sessions[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
for msg := m; msg != nil; msg = msg.message {
|
|
||||||
if x, ok := msg.target.Sessions[key]; ok {
|
|
||||||
return m.Spawn(x.target)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
func (m *Message) Sesss(key string, arg ...interface{}) *Message { // {{{
|
|
||||||
spawn := true
|
|
||||||
if _, ok := m.Sessions[key]; !ok && len(arg) > 0 {
|
|
||||||
switch value := arg[0].(type) {
|
|
||||||
case *Message:
|
|
||||||
m.Sessions[key] = value
|
|
||||||
return m.Sessions[key]
|
|
||||||
case *Context:
|
|
||||||
m.Sessions[key] = m.Spawn(value)
|
|
||||||
return m.Sessions[key]
|
|
||||||
case string:
|
|
||||||
root := true
|
|
||||||
if len(arg) > 2 {
|
|
||||||
root = Right(arg[2].(string))
|
|
||||||
}
|
|
||||||
method := "find"
|
|
||||||
if len(arg) > 1 {
|
|
||||||
method = arg[1].(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch method {
|
|
||||||
case "find":
|
|
||||||
m.Sessions[key] = m.Find(value, root)
|
|
||||||
case "search":
|
|
||||||
m.Sessions[key] = m.Search(value, root)[0]
|
|
||||||
}
|
|
||||||
return m.Sessions[key]
|
|
||||||
case bool:
|
|
||||||
spawn = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for msg := m; msg != nil; msg = msg.message {
|
|
||||||
if x, ok := msg.Sessions[key]; ok {
|
|
||||||
if spawn {
|
|
||||||
x = m.Spawn(x.target)
|
|
||||||
}
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
func (m *Message) Call(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
|
func (m *Message) Call(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
|
||||||
m.callback.hand = cb
|
m.callback = cb
|
||||||
m.Wait = nil
|
|
||||||
m.Cmd(arg...)
|
m.Cmd(arg...)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Back(msg *Message) *Message { // {{{
|
func (m *Message) Back(msg *Message) *Message { // {{{
|
||||||
if msg == nil || m.callback.hand == nil {
|
if msg == nil || m.callback == nil {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg.Hand {
|
if msg.Hand {
|
||||||
m.Log("cb", nil, "%d %v %v", msg.code, msg.Meta["result"], msg.Meta["append"])
|
m.Log("cb", "%d %v %v", msg.code, msg.Meta["result"], msg.Meta["append"])
|
||||||
} else {
|
} else {
|
||||||
m.Log("cb", nil, "%d %v %v", msg.code, msg.Meta["detail"], msg.Meta["option"])
|
m.Log("cb", "%d %v %v", msg.code, msg.Meta["detail"], msg.Meta["option"])
|
||||||
}
|
}
|
||||||
|
|
||||||
m.callback.ncall++
|
if sub := m.callback(msg); sub != nil && m.message != nil && m.message != m {
|
||||||
if sub := m.callback.hand(msg); sub != nil && m.message != nil && m.message != m {
|
|
||||||
m.message.Back(sub)
|
m.message.Back(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -720,15 +604,15 @@ func (m *Message) CallBack(sync bool, cb func(msg *Message) (sub *Message), arg
|
|||||||
|
|
||||||
go m.Call(func(sub *Message) *Message {
|
go m.Call(func(sub *Message) *Message {
|
||||||
msg := cb(sub)
|
msg := cb(sub)
|
||||||
m.Log("lock", nil, "before done %v", arg)
|
m.Log("lock", "before done %v", arg)
|
||||||
wait <- true
|
wait <- true
|
||||||
m.Log("lock", nil, "after done %v", arg)
|
m.Log("lock", "after done %v", arg)
|
||||||
return msg
|
return msg
|
||||||
}, arg...)
|
}, arg...)
|
||||||
|
|
||||||
m.Log("lock", nil, "before wait %v", arg)
|
m.Log("lock", "before wait %v", arg)
|
||||||
<-wait
|
<-wait
|
||||||
m.Log("lock", nil, "after wait %v", arg)
|
m.Log("lock", "after wait %v", arg)
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -761,7 +645,7 @@ func (m *Message) Add(meta string, key string, value ...interface{}) *Message {
|
|||||||
m.Meta[meta] = append(m.Meta[meta], key)
|
m.Meta[meta] = append(m.Meta[meta], key)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
m.Log("error", nil, "%s 消息参数错误", meta)
|
m.Log("error", "%s 消息参数错误", meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
@ -787,7 +671,7 @@ func (m *Message) Set(meta string, arg ...string) *Message { // {{{
|
|||||||
delete(m.Meta, meta)
|
delete(m.Meta, meta)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
m.Log("error", nil, "%s 消息参数错误", meta)
|
m.Log("error", "%s 消息参数错误", meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
@ -821,7 +705,7 @@ func (m *Message) Put(meta string, key string, value interface{}) *Message { //
|
|||||||
m.Meta[meta] = append(m.Meta[meta], key)
|
m.Meta[meta] = append(m.Meta[meta], key)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
m.Log("error", nil, "%s 消息参数错误", meta)
|
m.Log("error", "%s 消息参数错误", meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
@ -1326,7 +1210,7 @@ func (m *Message) Cmd(args ...interface{}) *Message { // {{{
|
|||||||
for s := c; s != nil; s = s.context {
|
for s := c; s != nil; s = s.context {
|
||||||
if x, ok := s.Commands[key]; ok && x.Hand != nil {
|
if x, ok := s.Commands[key]; ok && x.Hand != nil {
|
||||||
m.TryCatch(m, true, func(m *Message) {
|
m.TryCatch(m, true, func(m *Message) {
|
||||||
m.Log("cmd", s, "%s %v %v", s.Name, m.Meta["detail"], m.Meta["option"])
|
m.Log("cmd", "%s %v %v", s.Name, m.Meta["detail"], m.Meta["option"])
|
||||||
|
|
||||||
if args := []string{}; x.Form != nil {
|
if args := []string{}; x.Form != nil {
|
||||||
for i := 0; i < len(arg); i++ {
|
for i := 0; i < len(arg); i++ {
|
||||||
@ -1462,7 +1346,7 @@ func (m *Message) Conf(key string, arg ...string) string { // {{{
|
|||||||
} else {
|
} else {
|
||||||
x.Value = arg[0]
|
x.Value = arg[0]
|
||||||
}
|
}
|
||||||
// m.Log("conf", s, "%s %v", x.Name, x.Value)
|
// m.Log("conf", "%s %v", x.Name, x.Value)
|
||||||
return x.Value
|
return x.Value
|
||||||
case 0:
|
case 0:
|
||||||
if x.Hand != nil {
|
if x.Hand != nil {
|
||||||
@ -1480,11 +1364,11 @@ func (m *Message) Conf(key string, arg ...string) string { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.target.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
m.target.Configs[key] = &Config{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||||
m.Log("conf", nil, "%s %v", key, arg)
|
m.Log("conf", "%s %v", key, arg)
|
||||||
return m.Conf(key, arg[1])
|
return m.Conf(key, arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// m.Log("error", nil, "%s 配置项不存在", key)
|
// m.Log("error", "%s 配置项不存在", key)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1583,10 +1467,10 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
|
|||||||
} else {
|
} else {
|
||||||
x.Value = arg[0]
|
x.Value = arg[0]
|
||||||
}
|
}
|
||||||
// m.Log("debug", s, "%s %s", x.Name, x.Value)
|
// m.Log("debug", "%s %s", x.Name, x.Value)
|
||||||
return x.Value
|
return x.Value
|
||||||
case 0:
|
case 0:
|
||||||
// m.Log("debug", s, "%s %s", x.Name, x.Value)
|
// m.Log("debug", "%s %s", x.Name, x.Value)
|
||||||
if x.Hand != nil {
|
if x.Hand != nil {
|
||||||
return x.Hand(m, x)
|
return x.Hand(m, x)
|
||||||
}
|
}
|
||||||
@ -1602,11 +1486,11 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.target.Caches[key] = &Cache{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
m.target.Caches[key] = &Cache{Name: arg[0], Value: arg[1], Help: arg[2], Hand: hand}
|
||||||
m.Log("cap", nil, "%s %v", key, arg)
|
m.Log("cap", "%s %v", key, arg)
|
||||||
return m.Cap(key, arg[1])
|
return m.Cap(key, arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
// m.Log("error", nil, "%s 缓存项不存在", key)
|
// m.Log("error", "%s 缓存项不存在", key)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1717,7 +1601,7 @@ var CGI = template.FuncMap{
|
|||||||
case "messages":
|
case "messages":
|
||||||
case "sessions":
|
case "sessions":
|
||||||
msg := []string{}
|
msg := []string{}
|
||||||
for k, _ := range m.Sessions {
|
for k, _ := range m.sessions {
|
||||||
msg = append(msg, fmt.Sprintf("%s", k))
|
msg = append(msg, fmt.Sprintf("%s", k))
|
||||||
}
|
}
|
||||||
return strings.Join(msg, " ")
|
return strings.Join(msg, " ")
|
||||||
@ -1757,7 +1641,7 @@ var CGI = template.FuncMap{
|
|||||||
return strings.Join(msg, " ")
|
return strings.Join(msg, " ")
|
||||||
case "sessions":
|
case "sessions":
|
||||||
msg := []string{}
|
msg := []string{}
|
||||||
for k, _ := range m.Sessions {
|
for k, _ := range m.sessions {
|
||||||
msg = append(msg, fmt.Sprintf("%s", k))
|
msg = append(msg, fmt.Sprintf("%s", k))
|
||||||
}
|
}
|
||||||
return strings.Join(msg, " ")
|
return strings.Join(msg, " ")
|
||||||
@ -1915,11 +1799,9 @@ var CGI = template.FuncMap{
|
|||||||
var Pulse = &Message{
|
var Pulse = &Message{
|
||||||
code: 0,
|
code: 0,
|
||||||
time: time.Now(),
|
time: time.Now(),
|
||||||
Wait: make(chan bool),
|
|
||||||
source: Index,
|
source: Index,
|
||||||
master: Index,
|
|
||||||
target: Index,
|
target: Index,
|
||||||
Sessions: make(map[string]*Message),
|
sessions: make(map[string]*Message),
|
||||||
}
|
}
|
||||||
var Index = &Context{Name: "ctx", Help: "模块中心",
|
var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||||
Caches: map[string]*Cache{
|
Caches: map[string]*Cache{
|
||||||
@ -2065,9 +1947,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(msg.Sessions) > 0 {
|
if len(msg.sessions) > 0 {
|
||||||
m.Color(31, "sessions:\n")
|
m.Color(31, "sessions:\n")
|
||||||
for k, v := range msg.Sessions {
|
for k, v := range msg.sessions {
|
||||||
m.Echo(" %s %s\n", k, v.Format())
|
m.Echo(" %s %s\n", k, v.Format())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2188,7 +2070,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
msg := m.message // {{{
|
msg := m.message // {{{
|
||||||
if len(arg) == 0 {
|
if len(arg) == 0 {
|
||||||
for msg = msg; msg != nil; msg = msg.message {
|
for msg = msg; msg != nil; msg = msg.message {
|
||||||
for k, v := range msg.Sessions {
|
for k, v := range msg.sessions {
|
||||||
m.Echo("%s: %d(%s->%s) %v %v\n", k, v.code, v.source.Name, v.target.Name, v.Meta["detail"], v.Meta["option"])
|
m.Echo("%s: %d(%s->%s) %v %v\n", k, v.code, v.source.Name, v.target.Name, v.Meta["detail"], v.Meta["option"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2197,7 +2079,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
|
|
||||||
var sub *Message
|
var sub *Message
|
||||||
for m := msg; m != nil; m = m.message {
|
for m := msg; m != nil; m = m.message {
|
||||||
for k, v := range m.Sessions {
|
for k, v := range m.sessions {
|
||||||
if k == arg[0] {
|
if k == arg[0] {
|
||||||
sub = v
|
sub = v
|
||||||
}
|
}
|
||||||
@ -2217,7 +2099,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
|
|
||||||
cmd := msg.Spawn(sub.target).Cmd(arg[1:])
|
cmd := msg.Spawn(sub.target).Cmd(arg[1:])
|
||||||
m.Copy(cmd, "result").Copy(cmd, "append")
|
m.Copy(cmd, "result").Copy(cmd, "append")
|
||||||
msg.Sessions[arg[0]] = cmd
|
msg.sessions[arg[0]] = cmd
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"callback": &Command{Name: "callback index", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
"callback": &Command{Name: "callback index", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
@ -2226,25 +2108,25 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
index--
|
index--
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
case 0:
|
case 0:
|
||||||
if msg.callback.hand == nil {
|
if msg.callback == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Echo("%d: %v\n", index, msg.callback.hand)
|
m.Echo("%d: %v\n", index, msg.callback)
|
||||||
case 1:
|
case 1:
|
||||||
if msg.callback.hand == nil {
|
if msg.callback == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if i, e := strconv.Atoi(arg[0]); e == nil && i == index {
|
if i, e := strconv.Atoi(arg[0]); e == nil && i == index {
|
||||||
m.Echo("%v", msg.callback.hand)
|
m.Echo("%v", msg.callback)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if i, e := strconv.Atoi(arg[0]); e == nil && i == index {
|
if i, e := strconv.Atoi(arg[0]); e == nil && i == index {
|
||||||
msg.callback.hand = func(msg *Message) *Message {
|
msg.callback = func(msg *Message) *Message {
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
m.Echo("%v", msg.callback.hand)
|
m.Echo("%v", msg.callback)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -2252,16 +2134,16 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
|
|
||||||
if len(arg) == 0 {
|
if len(arg) == 0 {
|
||||||
m.Echo("msg(%s->%s): %d(%s) %v\n", msg.source.Name, msg.target.Name, msg.code, msg.time.Format("15:04:05"), msg.Meta["detail"])
|
m.Echo("msg(%s->%s): %d(%s) %v\n", msg.source.Name, msg.target.Name, msg.code, msg.time.Format("15:04:05"), msg.Meta["detail"])
|
||||||
if msg.callback.hand != nil {
|
if msg.callback != nil {
|
||||||
m.Echo(" hand(%d): %v\n", msg.callback.ncall, msg.callback.hand)
|
m.Echo(" hand: %v\n", msg.callback)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch arg[0] {
|
switch arg[0] {
|
||||||
case "del":
|
case "del":
|
||||||
msg.message.callback.hand = nil
|
msg.message.callback = nil
|
||||||
case "add":
|
case "add":
|
||||||
msg.message.callback.hand = func(msg *Message) *Message {
|
msg.message.callback = func(msg *Message) *Message {
|
||||||
msg.Log("info", nil, "callback default")
|
msg.Log("info", "callback default")
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -2387,24 +2269,23 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
m.Add("append", "help", v.Help)
|
m.Add("append", "help", v.Help)
|
||||||
}
|
}
|
||||||
case "module":
|
case "module":
|
||||||
m.Travel(msg.target, func(msg *Message) bool {
|
m.Travel(func(msg *Message, i int) bool {
|
||||||
m.Add("append", "name", msg.target.Name)
|
m.Add("append", "name", msg.target.Name)
|
||||||
m.Add("append", "help", msg.target.Help)
|
m.Add("append", "help", msg.target.Help)
|
||||||
m.Add("append", "module", msg.Cap("module"))
|
m.Add("append", "module", msg.Cap("module"))
|
||||||
m.Add("append", "status", msg.Cap("status"))
|
m.Add("append", "status", msg.Cap("status"))
|
||||||
m.Add("append", "stream", msg.Cap("stream"))
|
m.Add("append", "stream", msg.Cap("stream"))
|
||||||
return true
|
return true
|
||||||
})
|
}, msg.target)
|
||||||
case "domain":
|
case "domain":
|
||||||
msg := m.Find("ssh", true)
|
m.Find("ssh", true).Travel(func(msg *Message, i int) bool {
|
||||||
msg.Travel(msg.target, func(msg *Message) bool {
|
|
||||||
m.Add("append", "name", msg.target.Name)
|
m.Add("append", "name", msg.target.Name)
|
||||||
m.Add("append", "help", msg.target.Help)
|
m.Add("append", "help", msg.target.Help)
|
||||||
m.Add("append", "domain", msg.Cap("domain")+"."+msg.Conf("domains"))
|
m.Add("append", "domain", msg.Cap("domain")+"."+msg.Conf("domains"))
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
msg.Travel(msg.target, func(msg *Message) bool {
|
msg.Travel(func(msg *Message, i int) bool {
|
||||||
target := msg.target
|
target := msg.target
|
||||||
m.Echo("%s(", target.Name)
|
m.Echo("%s(", target.Name)
|
||||||
|
|
||||||
@ -2413,21 +2294,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
m.Echo(":")
|
m.Echo(":")
|
||||||
|
|
||||||
if target.master != nil {
|
|
||||||
m.Echo("%s", target.master.Name)
|
|
||||||
}
|
|
||||||
m.Echo(":")
|
|
||||||
|
|
||||||
if target.Owner != nil {
|
|
||||||
m.Echo("%s", target.Owner.Name)
|
|
||||||
}
|
|
||||||
m.Echo(":")
|
|
||||||
|
|
||||||
msg.target = msg.target.Owner
|
|
||||||
if msg.target != nil {
|
|
||||||
m.Echo("%s:%s", msg.Cap("username"), msg.Cap("group"))
|
|
||||||
}
|
|
||||||
m.Echo("): ")
|
|
||||||
msg.target = target
|
msg.target = target
|
||||||
|
|
||||||
m.Echo("%s(%s) ", msg.Cap("status"), msg.Cap("stream"))
|
m.Echo("%s(%s) ", msg.Cap("status"), msg.Cap("stream"))
|
||||||
@ -2450,12 +2316,12 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
switch len(arg) { // {{{
|
switch len(arg) { // {{{
|
||||||
case 0:
|
case 0:
|
||||||
m.Travel(m.target.root, func(m *Message) bool {
|
m.Travel(func(m *Message, i int) bool {
|
||||||
if m.Cap("status") == "start" {
|
if m.Cap("status") == "start" {
|
||||||
m.Echo("%s(%s): %s\n", m.target.Name, m.Cap("stream"), m.target.Help)
|
m.Echo("%s(%s): %s\n", m.target.Name, m.Cap("stream"), m.target.Help)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
}, m.target.root)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
switch arg[0] {
|
switch arg[0] {
|
||||||
@ -3007,20 +2873,18 @@ func Start(args ...string) {
|
|||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
Index.Owner = Index.contexts["aaa"]
|
|
||||||
Index.master = Index.contexts["cli"]
|
|
||||||
for _, m := range Pulse.Search("") {
|
for _, m := range Pulse.Search("") {
|
||||||
m.target.root = Index
|
m.target.root = Index
|
||||||
m.target.Begin(m)
|
m.target.Begin(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
Pulse.Options("log", true)
|
Pulse.Sesss("nfs", "nfs")
|
||||||
Pulse.Options("terminal_color", true)
|
Pulse.Sesss("lex", "lex")
|
||||||
|
Pulse.Sesss("yac", "yac")
|
||||||
Pulse.Sesss("log", "log").Conf("bench.log", Pulse.Conf("bench.log"))
|
Pulse.Sesss("log", "log").Conf("bench.log", Pulse.Conf("bench.log"))
|
||||||
|
|
||||||
Pulse.Sesss("nfs", "nfs")
|
Pulse.Options("log", true)
|
||||||
Pulse.Sesss("yac", "yac")
|
Pulse.Options("terminal_color", true)
|
||||||
Pulse.Sesss("lex", "lex")
|
|
||||||
cli := Pulse.Find("cli").Cmd("source", "stdio", "async")
|
cli := Pulse.Find("cli").Cmd("source", "stdio", "async")
|
||||||
<-cli.target.Exit
|
cli.target.Wait()
|
||||||
}
|
}
|
||||||
|
@ -129,8 +129,8 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
|
|||||||
cc = append(cc, seed[p])
|
cc = append(cc, seed[p])
|
||||||
}
|
}
|
||||||
|
|
||||||
lex.Log("debug", nil, "page: \033[31m%d %v\033[0m", len(ss), ss)
|
lex.Log("debug", "page: \033[31m%d %v\033[0m", len(ss), ss)
|
||||||
lex.Log("debug", nil, "cell: \033[32m%d %v\033[0m", len(cc), cc)
|
lex.Log("debug", "cell: \033[32m%d %v\033[0m", len(cc), cc)
|
||||||
|
|
||||||
flag := '\000'
|
flag := '\000'
|
||||||
if p+1 < len(seed) {
|
if p+1 < len(seed) {
|
||||||
@ -151,7 +151,7 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
|
|||||||
} else {
|
} else {
|
||||||
lex.Capi("nnode", 1)
|
lex.Capi("nnode", 1)
|
||||||
}
|
}
|
||||||
lex.Log("debug", nil, "GET(%d,%d): %v", s, c, state)
|
lex.Log("debug", "GET(%d,%d): %v", s, c, state)
|
||||||
|
|
||||||
switch flag {
|
switch flag {
|
||||||
case '+':
|
case '+':
|
||||||
@ -175,7 +175,7 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
|
|||||||
|
|
||||||
lex.mat[s][c] = state
|
lex.mat[s][c] = state
|
||||||
points = append(points, &Point{s, c})
|
points = append(points, &Point{s, c})
|
||||||
lex.Log("debug", nil, "SET(%d,%d): %v(%s,%s)", s, c, state, lex.Cap("nnode"), lex.Cap("nreal"))
|
lex.Log("debug", "SET(%d,%d): %v(%s,%s)", s, c, state, lex.Cap("nnode"), lex.Cap("nreal"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,7 +193,7 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(lex.mat[s]) == 0 {
|
if len(lex.mat[s]) == 0 {
|
||||||
lex.Log("debug", nil, "DEL: %d-%d", lex.Capi("nline")-1, lex.Capi("nline", 0, s))
|
lex.Log("debug", "DEL: %d-%d", lex.Capi("nline")-1, lex.Capi("nline", 0, s))
|
||||||
lex.mat = lex.mat[:s]
|
lex.mat = lex.mat[:s]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,12 +204,12 @@ func (lex *LEX) train(page int, hash int, seed []byte) int { // {{{
|
|||||||
*state = *lex.mat[p.s][p.c]
|
*state = *lex.mat[p.s][p.c]
|
||||||
|
|
||||||
if state.next == s {
|
if state.next == s {
|
||||||
lex.Log("debug", nil, "GET(%d, %d): %v", p.s, p.c, state)
|
lex.Log("debug", "GET(%d, %d): %v", p.s, p.c, state)
|
||||||
if state.hash = hash; state.next >= len(lex.mat) {
|
if state.hash = hash; state.next >= len(lex.mat) {
|
||||||
state.next = 0
|
state.next = 0
|
||||||
}
|
}
|
||||||
lex.mat[p.s][p.c] = state
|
lex.mat[p.s][p.c] = state
|
||||||
lex.Log("debug", nil, "SET(%d, %d): %v", p.s, p.c, state)
|
lex.Log("debug", "SET(%d, %d): %v", p.s, p.c, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
if x, ok := lex.state[*state]; !ok {
|
if x, ok := lex.state[*state]; !ok {
|
||||||
@ -241,7 +241,7 @@ func (lex *LEX) parse(m *ctx.Message, page int, line []byte) (hash int, rest []b
|
|||||||
}
|
}
|
||||||
|
|
||||||
state := lex.mat[s][c]
|
state := lex.mat[s][c]
|
||||||
lex.Log("debug", nil, "(%d,%d): %v", s, c, state)
|
lex.Log("debug", "(%d,%d): %v", s, c, state)
|
||||||
if state == nil {
|
if state == nil {
|
||||||
s, star, pos = star, 0, pos-1
|
s, star, pos = star, 0, pos-1
|
||||||
continue
|
continue
|
||||||
@ -438,11 +438,11 @@ var Index = &ctx.Context{Name: "lex", Help: "词法中心",
|
|||||||
nnode++
|
nnode++
|
||||||
|
|
||||||
if _, ok := set[x]; !ok {
|
if _, ok := set[x]; !ok {
|
||||||
m.Log("fuck", nil, "not in %d %d %v %p", i, j, x, x)
|
m.Log("fuck", "not in %d %d %v %p", i, j, x, x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.Log("fuck", nil, "node: %d real: %d", nnode, nreal)
|
m.Log("fuck", "node: %d real: %d", nnode, nreal)
|
||||||
} // }}}
|
} // }}}
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
@ -18,6 +18,8 @@ type LOG struct {
|
|||||||
color map[string]int
|
color map[string]int
|
||||||
*Log.Logger
|
*Log.Logger
|
||||||
|
|
||||||
|
nfs *ctx.Message
|
||||||
|
|
||||||
*ctx.Message
|
*ctx.Message
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
@ -44,9 +46,9 @@ func (log *LOG) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|||||||
|
|
||||||
log.Configs["bench.log"] = &ctx.Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
log.Configs["bench.log"] = &ctx.Config{Name: "日志文件", Value: "var/bench.log", Help: "模块日志输出的文件", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
if m.Sess("nfs") == nil {
|
if nfs := m.Sesss("nfs"); nfs != nil {
|
||||||
os.Create(arg[0])
|
os.Create(arg[0])
|
||||||
m.Sess("nfs", "nfs").Cmd("open", arg[0], "", "日志文件")
|
log.nfs = nfs.Cmd("open", arg[0], "", "日志文件")
|
||||||
}
|
}
|
||||||
return arg[0]
|
return arg[0]
|
||||||
}
|
}
|
||||||
@ -159,19 +161,16 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心",
|
|||||||
|
|
||||||
if m.Confs("flag_name") {
|
if m.Confs("flag_name") {
|
||||||
action = fmt.Sprintf("%s(%s->%s)", action, msg.Source().Name, msg.Target().Name)
|
action = fmt.Sprintf("%s(%s->%s)", action, msg.Source().Name, msg.Target().Name)
|
||||||
if msg.Name != "" {
|
|
||||||
action = fmt.Sprintf("%s(%s:%s->%s.%d)", action, msg.Source().Name, msg.Name, msg.Target().Name, m.Index)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := strings.Join(arg[1:], "")
|
cmd := strings.Join(arg[1:], "")
|
||||||
|
|
||||||
if nfs := m.Sess("nfs"); nfs != nil {
|
if log.nfs != nil {
|
||||||
if nfs.Options("log", false); color > 0 {
|
if log.nfs.Options("log", false); color > 0 {
|
||||||
nfs.Cmd("write", fmt.Sprintf("%s\033[%dm%s%s %s\033[0m\n", date, color, code, action, cmd))
|
log.nfs.Cmd("write", fmt.Sprintf("%s\033[%dm%s%s %s\033[0m\n", date, color, code, action, cmd))
|
||||||
} else {
|
} else {
|
||||||
nfs.Cmd("write", fmt.Sprintf("%s%s%s %s\n", date, code, action, cmd))
|
log.nfs.Cmd("write", fmt.Sprintf("%s%s%s %s\n", date, code, action, cmd))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
|
@ -51,7 +51,6 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
mdb.Context.Master(nil)
|
|
||||||
if mdb.Context == Index {
|
if mdb.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
@ -64,7 +63,7 @@ func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
m.Cap("username"), m.Cap("password"), m.Cap("protocol"), m.Cap("address"), m.Cap("database")))
|
m.Cap("username"), m.Cap("password"), m.Cap("protocol"), m.Cap("address"), m.Cap("database")))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
mdb.DB = db
|
mdb.DB = db
|
||||||
m.Log("info", nil, "mdb open %s", m.Cap("database"))
|
m.Log("info", "mdb open %s", m.Cap("database"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +72,7 @@ func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
switch mdb.Context {
|
switch mdb.Context {
|
||||||
case m.Target():
|
case m.Target():
|
||||||
if mdb.DB != nil {
|
if mdb.DB != nil {
|
||||||
m.Log("info", nil, "mdb close %s", m.Cap("database"))
|
m.Log("info", "mdb close %s", m.Cap("database"))
|
||||||
mdb.DB.Close()
|
mdb.DB.Close()
|
||||||
mdb.DB = nil
|
mdb.DB = nil
|
||||||
}
|
}
|
||||||
@ -132,7 +131,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
n, e := ret.RowsAffected()
|
n, e := ret.RowsAffected()
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
m.Log("info", nil, "last(%s) nrow(%s)", m.Append("last", id), m.Append("nrow", n))
|
m.Log("info", "last(%s) nrow(%s)", m.Append("last", id), m.Append("nrow", n))
|
||||||
m.Echo("%d", id).Echo("%d", n)
|
m.Echo("%d", id).Echo("%d", n)
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
@ -173,9 +172,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(m.Meta["append"]) > 0 {
|
if len(m.Meta["append"]) > 0 {
|
||||||
m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
m.Log("info", "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
||||||
} else {
|
} else {
|
||||||
m.Log("info", nil, "rows(0) cols(0)")
|
m.Log("info", "rows(0) cols(0)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
@ -239,10 +238,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
"show": &ctx.Command{
|
"show": &ctx.Command{
|
||||||
Name: "show table fields... [where conditions] [group fields] [order fields] [limit fields] [offset fields] [save filename] [other rest...]",
|
Name: "show table fields... [where conditions] [group fields] [order fields] [limit fields] [offset fields] [save filename] [other rest...]",
|
||||||
Help: "查询数据库, table: 表名, fields: 字段, where: 查询条件, group: 聚合字段, order: 排序字段",
|
Help: "查询数据库, table: 表名, fields: 字段, where: 查询条件, group: 聚合字段, order: 排序字段",
|
||||||
Form: map[string]int{
|
Form: map[string]int{"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1, "extras": 1, "extra_format": 1, "trans_field": 1, "trans_map": 2, "save": 1, "export": 1, "other": -1},
|
||||||
"where": 1, "group": 1, "order": 1, "limit": 1, "offset": 1,
|
|
||||||
"extras": 1, "extra_format": 1, "trans_field": 1, "trans_map": 2,
|
|
||||||
"save": 1, "save_format": 1, "csv_col_sep": 1, "export": 1, "other": -1},
|
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) { // {{{
|
if mdb, ok := m.Target().Server.(*MDB); m.Assert(ok) { // {{{
|
||||||
table := m.Confx("table", arg, 0)
|
table := m.Confx("table", arg, 0)
|
||||||
@ -285,15 +281,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.Table(func(maps map[string]string, lists []string, line int) bool {
|
m.Table(func(maps map[string]string, lists []string, line int) bool {
|
||||||
args := []interface{}{}
|
|
||||||
|
|
||||||
for i, v := range lists {
|
for i, v := range lists {
|
||||||
if m.Options("save_format") {
|
|
||||||
key := m.Meta["append"][i]
|
|
||||||
value := maps[key]
|
|
||||||
args = append(args, value)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if m.Options("save") {
|
if m.Options("save") {
|
||||||
key := m.Meta["append"][i]
|
key := m.Meta["append"][i]
|
||||||
value := maps[key]
|
value := maps[key]
|
||||||
@ -313,17 +301,10 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
m.Echo(v)
|
m.Echo(v)
|
||||||
}
|
}
|
||||||
if i < len(lists)-1 {
|
if i < len(lists)-1 {
|
||||||
m.Echo(m.Confx("csv_col_sep"))
|
m.Echo(m.Conf("csv_col_sep"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if m.Options("save_format") {
|
m.Echo(m.Conf("csv_row_sep"))
|
||||||
if line > -1 {
|
|
||||||
m.Echo(fmt.Sprintf(m.Option("save_format"), args...))
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
m.Echo(m.Confx("csv_row_sep"))
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -47,16 +47,16 @@ type NFS struct {
|
|||||||
|
|
||||||
func (nfs *NFS) open(name string) (*os.File, error) { // {{{
|
func (nfs *NFS) open(name string) (*os.File, error) { // {{{
|
||||||
if path.IsAbs(name) {
|
if path.IsAbs(name) {
|
||||||
nfs.Log("info", nil, "open %s", name)
|
nfs.Log("info", "open %s", name)
|
||||||
return os.Open(name)
|
return os.Open(name)
|
||||||
}
|
}
|
||||||
for i := len(nfs.paths) - 1; i >= 0; i-- {
|
for i := len(nfs.paths) - 1; i >= 0; i-- {
|
||||||
if f, e := os.Open(path.Join(nfs.paths[i], name)); e == nil {
|
if f, e := os.Open(path.Join(nfs.paths[i], name)); e == nil {
|
||||||
nfs.Log("info", nil, "open %s", path.Join(nfs.paths[i], name))
|
nfs.Log("info", "open %s", path.Join(nfs.paths[i], name))
|
||||||
return f, e
|
return f, e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nfs.Log("info", nil, "open %s", name)
|
nfs.Log("info", "open %s", name)
|
||||||
return os.Open(name)
|
return os.Open(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -487,8 +487,6 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
|
|||||||
if len(tab) == 0 {
|
if len(tab) == 0 {
|
||||||
tabi = 0
|
tabi = 0
|
||||||
prefix := string(buf)
|
prefix := string(buf)
|
||||||
target := nfs.Message.Target()
|
|
||||||
nfs.Message.Target(nfs.Optionv("ps_target").(*ctx.Context))
|
|
||||||
nfs.Message.BackTrace(func(m *ctx.Message) bool {
|
nfs.Message.BackTrace(func(m *ctx.Message) bool {
|
||||||
for k, _ := range m.Target().Commands {
|
for k, _ := range m.Target().Commands {
|
||||||
if strings.HasPrefix(k, prefix) {
|
if strings.HasPrefix(k, prefix) {
|
||||||
@ -496,8 +494,7 @@ func (nfs *NFS) Read(p []byte) (n int, err error) { // {{{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
}, nfs.Optionv("ps_target").(*ctx.Context))
|
||||||
nfs.Message.Target(target)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tabi >= 0 && tabi < len(tab) {
|
if tabi >= 0 && tabi < len(tab) {
|
||||||
@ -642,8 +639,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Target().Sessions["nfs"] = m
|
m.Sesss("nfs", m)
|
||||||
m.Sessions["nfs"] = m
|
|
||||||
|
|
||||||
nfs.Message = m
|
nfs.Message = m
|
||||||
if socket, ok := m.Data["io"]; ok {
|
if socket, ok := m.Data["io"]; ok {
|
||||||
@ -699,9 +695,9 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if msg.Has("detail") {
|
if msg.Has("detail") {
|
||||||
msg.Log("info", nil, "%d recv", m.Capi("nrecv", 1))
|
msg.Log("info", "%d recv", m.Capi("nrecv", 1))
|
||||||
msg.Log("info", nil, "detail: %v", msg.Meta["detail"])
|
msg.Log("info", "detail: %v", msg.Meta["detail"])
|
||||||
msg.Log("info", nil, "option: %v", msg.Meta["option"])
|
msg.Log("info", "option: %v", msg.Meta["option"])
|
||||||
msg.Options("stdio", false)
|
msg.Options("stdio", false)
|
||||||
msg.Option("nsend", nsend)
|
msg.Option("nsend", nsend)
|
||||||
|
|
||||||
@ -722,9 +718,9 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub.Log("info", nil, "%d recv", sub.Optioni("nsend"))
|
sub.Log("info", "%d recv", sub.Optioni("nsend"))
|
||||||
sub.Log("info", nil, "result: %v", sub.Meta["result"])
|
sub.Log("info", "result: %v", sub.Meta["result"])
|
||||||
sub.Log("info", nil, "append: %v", sub.Meta["append"])
|
sub.Log("info", "append: %v", sub.Meta["append"])
|
||||||
|
|
||||||
_, e := fmt.Fprintf(nfs.Writer, "\n")
|
_, e := fmt.Fprintf(nfs.Writer, "\n")
|
||||||
sub.Assert(e)
|
sub.Assert(e)
|
||||||
@ -746,9 +742,9 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
msg.Meta["append"] = msg.Meta["option"]
|
msg.Meta["append"] = msg.Meta["option"]
|
||||||
delete(msg.Meta, "option")
|
delete(msg.Meta, "option")
|
||||||
|
|
||||||
msg.Log("info", nil, "%s send", nsend)
|
msg.Log("info", "%s send", nsend)
|
||||||
msg.Log("info", nil, "result: %v", msg.Meta["result"])
|
msg.Log("info", "result: %v", msg.Meta["result"])
|
||||||
msg.Log("info", nil, "append: %v", msg.Meta["append"])
|
msg.Log("info", "append: %v", msg.Meta["append"])
|
||||||
|
|
||||||
n, e := strconv.Atoi(nsend)
|
n, e := strconv.Atoi(nsend)
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
@ -1038,7 +1034,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
buf := make([]byte, s)
|
buf := make([]byte, s)
|
||||||
|
|
||||||
if l, e := f.ReadAt(buf, int64(pos)); e == io.EOF || m.Assert(e) {
|
if l, e := f.ReadAt(buf, int64(pos)); e == io.EOF || m.Assert(e) {
|
||||||
m.Log("info", nil, "read %d", l)
|
m.Log("info", "read %d", l)
|
||||||
m.Echo(string(buf[:l]))
|
m.Echo(string(buf[:l]))
|
||||||
}
|
}
|
||||||
} // }}}
|
} // }}}
|
||||||
@ -1178,7 +1174,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
}
|
}
|
||||||
case "difftool":
|
case "difftool":
|
||||||
cmd := exec.Command("git", "difftool", "-y")
|
cmd := exec.Command("git", "difftool", "-y")
|
||||||
m.Log("info", nil, "cmd: %s %v", "git", "difftool", "-y")
|
m.Log("info", "cmd: %s %v", "git", "difftool", "-y")
|
||||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||||
if e := cmd.Start(); e != nil {
|
if e := cmd.Start(); e != nil {
|
||||||
m.Echo("error: ")
|
m.Echo("error: ")
|
||||||
@ -1257,7 +1253,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
args = append(args, arg[1:]...)
|
args = append(args, arg[1:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Log("info", nil, "cmd: %s %v", "git", ctx.Trans("-C", p, c, args))
|
m.Log("info", "cmd: %s %v", "git", ctx.Trans("-C", p, c, args))
|
||||||
cmd := exec.Command("git", ctx.Trans("-C", p, c, args)...)
|
cmd := exec.Command("git", ctx.Trans("-C", p, c, args)...)
|
||||||
if out, e := cmd.CombinedOutput(); e != nil {
|
if out, e := cmd.CombinedOutput(); e != nil {
|
||||||
m.Echo("error: ")
|
m.Echo("error: ")
|
||||||
@ -1342,9 +1338,9 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.Log("info", nil, "%d send", m.Optioni("nsend"))
|
m.Log("info", "%d send", m.Optioni("nsend"))
|
||||||
m.Log("info", nil, "detail: %v", m.Meta["detail"])
|
m.Log("info", "detail: %v", m.Meta["detail"])
|
||||||
m.Log("info", nil, "option: %v", m.Meta["option"])
|
m.Log("info", "option: %v", m.Meta["option"])
|
||||||
|
|
||||||
n, e := fmt.Fprintf(nfs.Writer, "\n")
|
n, e := fmt.Fprintf(nfs.Writer, "\n")
|
||||||
m.Capi("nbytes", n)
|
m.Capi("nbytes", n)
|
||||||
|
@ -30,7 +30,6 @@ func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
ssh.Context.Master(nil)
|
|
||||||
if ssh.Context == Index {
|
if ssh.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
@ -69,20 +68,20 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
// }}}
|
// }}}
|
||||||
|
|
||||||
func Done(m *ctx.Message, lock chan bool) { // {{{
|
func Done(m *ctx.Message, lock chan bool) { // {{{
|
||||||
m.Log("lock", nil, "done before %v", m.Meta["detail"])
|
m.Log("lock", "done before %v", m.Meta["detail"])
|
||||||
if m.Options("stdio") {
|
if m.Options("stdio") {
|
||||||
lock <- true
|
lock <- true
|
||||||
}
|
}
|
||||||
m.Log("lock", nil, "done after %v", m.Meta["detail"])
|
m.Log("lock", "done after %v", m.Meta["detail"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func Wait(m *ctx.Message, lock chan bool) { // {{{
|
func Wait(m *ctx.Message, lock chan bool) { // {{{
|
||||||
m.Log("lock", nil, "wait before %v", m.Meta["detail"])
|
m.Log("lock", "wait before %v", m.Meta["detail"])
|
||||||
if m.Options("stdio") {
|
if m.Options("stdio") {
|
||||||
<-lock
|
<-lock
|
||||||
}
|
}
|
||||||
m.Log("lock", nil, "wait after %v", m.Meta["detail"])
|
m.Log("lock", "wait after %v", m.Meta["detail"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
@ -118,7 +117,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
m.Find("nfs").Call(func(file *ctx.Message) *ctx.Message {
|
m.Find("nfs").Call(func(file *ctx.Message) *ctx.Message {
|
||||||
sub := file.Spawn(m.Target())
|
sub := file.Spawn(m.Target())
|
||||||
sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "远程主机")
|
sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "远程主机")
|
||||||
m.Sessions["ssh"] = sub
|
m.Sesss("ssh", sub)
|
||||||
return sub
|
return sub
|
||||||
}, m.Meta["detail"])
|
}, m.Meta["detail"])
|
||||||
m.Spawn(m.Target()).Cmd("save")
|
m.Spawn(m.Target()).Cmd("save")
|
||||||
@ -131,7 +130,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
sub := file.Spawn(m.Target())
|
sub := file.Spawn(m.Target())
|
||||||
sub.Copy(m, "detail")
|
sub.Copy(m, "detail")
|
||||||
sub.Target().Start(sub)
|
sub.Target().Start(sub)
|
||||||
m.Sessions["ssh"] = sub
|
m.Sesss("ssh", sub)
|
||||||
|
|
||||||
sub.Spawn(sub.Target()).Cmd("pwd", m.Conf("domain"))
|
sub.Spawn(sub.Target()).Cmd("pwd", m.Conf("domain"))
|
||||||
return sub
|
return sub
|
||||||
@ -179,7 +178,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
miss := true
|
miss := true
|
||||||
m.Travel(c, func(m *ctx.Message) bool { //向下搜索
|
m.Travel(func(m *ctx.Message, i int) bool { //向下搜索
|
||||||
if ssh, ok := m.Target().Server.(*SSH); ok && m.Conf("domains") == name {
|
if ssh, ok := m.Target().Server.(*SSH); ok && m.Conf("domains") == name {
|
||||||
msg := m.Spawn(ssh.nfs)
|
msg := m.Spawn(ssh.nfs)
|
||||||
msg.Option("domain", strings.Join(rest, "."))
|
msg.Option("domain", strings.Join(rest, "."))
|
||||||
@ -190,7 +189,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
miss = false
|
miss = false
|
||||||
}
|
}
|
||||||
return miss
|
return miss
|
||||||
})
|
}, c)
|
||||||
|
|
||||||
if miss {
|
if miss {
|
||||||
if name == m.Cap("domain") {
|
if name == m.Cap("domain") {
|
||||||
@ -217,13 +216,13 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
if m.Options("nsend") {
|
if m.Options("nsend") {
|
||||||
domain := arg[0]
|
domain := arg[0]
|
||||||
|
|
||||||
m.Travel(c, func(m *ctx.Message) bool {
|
m.Travel(func(m *ctx.Message, i int) bool {
|
||||||
if m.Conf("domains") == domain {
|
if m.Conf("domains") == domain {
|
||||||
domain = domain + m.Cap("nhost")
|
domain = domain + m.Cap("nhost")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
}, c)
|
||||||
m.Conf("domains", domain)
|
m.Conf("domains", domain)
|
||||||
|
|
||||||
mdb := m.Find(m.Conf("mdb"), true)
|
mdb := m.Find(m.Conf("mdb"), true)
|
||||||
@ -254,12 +253,12 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
}},
|
}},
|
||||||
"list": &ctx.Command{Name: "list", Help: "连接断开", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"list": &ctx.Command{Name: "list", Help: "连接断开", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
domain := m.Cap("domain")
|
domain := m.Cap("domain")
|
||||||
m.Travel(c, func(m *ctx.Message) bool {
|
m.Travel(func(m *ctx.Message, i int) bool {
|
||||||
if m.Confs("domains") {
|
if m.Confs("domains") {
|
||||||
m.Echo("%s: %s.%s\n", m.Target().Name, domain, m.Conf("domains"))
|
m.Echo("%s: %s.%s\n", m.Target().Name, domain, m.Conf("domains"))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
}, c)
|
||||||
}},
|
}},
|
||||||
"save": &ctx.Command{Name: "save", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"save": &ctx.Command{Name: "save", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
json := m.Sesss("nfs") // {{{
|
json := m.Sesss("nfs") // {{{
|
||||||
@ -295,7 +294,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
cmds = arg[1]
|
cmds = arg[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Travel(ctx.Index, func(msg *ctx.Message) bool {
|
m.Travel(func(msg *ctx.Message, i int) bool {
|
||||||
current := msg.Target()
|
current := msg.Target()
|
||||||
if _, ok := current.Index[cmds]; ok {
|
if _, ok := current.Index[cmds]; ok {
|
||||||
|
|
||||||
@ -306,14 +305,14 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
m.Add("append", "name", current.Name)
|
m.Add("append", "name", current.Name)
|
||||||
m.Add("append", "help", current.Help)
|
m.Add("append", "help", current.Help)
|
||||||
return true
|
return true
|
||||||
})
|
}, ctx.Index)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(arg) > 2 {
|
if len(arg) > 2 {
|
||||||
cmds = arg[2]
|
cmds = arg[2]
|
||||||
}
|
}
|
||||||
current := m.Sess(arg[1], arg[1], "search").Target()
|
current := m.Sesss(arg[1], arg[1], "search").Target()
|
||||||
if x, ok := current.Index[cmds]; ok {
|
if x, ok := current.Index[cmds]; ok {
|
||||||
current = x
|
current = x
|
||||||
} else if cmds != "" && cmds != "root" {
|
} else if cmds != "" && cmds != "root" {
|
||||||
|
@ -32,7 +32,6 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
tcp.Context.Master(nil)
|
|
||||||
if tcp.Context == Index {
|
if tcp.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
@ -67,7 +66,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
tcp.Conn = c
|
tcp.Conn = c
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"),
|
m.Log("info", "%s dial %s", Pulse.Cap("nclient"),
|
||||||
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
||||||
m.Put("append", "io", tcp.Conn).Back(m)
|
m.Put("append", "io", tcp.Conn).Back(m)
|
||||||
return false
|
return false
|
||||||
@ -76,7 +75,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
tcp.Conn = c
|
tcp.Conn = c
|
||||||
|
|
||||||
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"),
|
m.Log("info", "%s accept %s", Pulse.Cap("nclient"),
|
||||||
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
||||||
m.Put("append", "io", tcp.Conn).Back(m)
|
m.Put("append", "io", tcp.Conn).Back(m)
|
||||||
return false
|
return false
|
||||||
@ -95,7 +94,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
tcp.Listener = l
|
tcp.Listener = l
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", tcp.Addr())))
|
m.Log("info", "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", tcp.Addr())))
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@ -115,19 +114,19 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
switch tcp.Context {
|
switch tcp.Context {
|
||||||
case m.Target():
|
case m.Target():
|
||||||
if tcp.Listener != nil {
|
if tcp.Listener != nil {
|
||||||
m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream"))
|
m.Log("info", "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream"))
|
||||||
tcp.Listener.Close()
|
tcp.Listener.Close()
|
||||||
tcp.Listener = nil
|
tcp.Listener = nil
|
||||||
}
|
}
|
||||||
if tcp.Conn != nil {
|
if tcp.Conn != nil {
|
||||||
m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, m.Cap("stream"))
|
m.Log("info", "%d close %v", Pulse.Capi("nclient", -1)+1, m.Cap("stream"))
|
||||||
tcp.Conn.Close()
|
tcp.Conn.Close()
|
||||||
tcp.Conn = nil
|
tcp.Conn = nil
|
||||||
}
|
}
|
||||||
case m.Source():
|
case m.Source():
|
||||||
if tcp.Conn != nil {
|
if tcp.Conn != nil {
|
||||||
msg := m.Spawn(tcp.Context)
|
msg := m.Spawn(tcp.Context)
|
||||||
if msg.Master(tcp.Context); !tcp.Context.Close(msg, arg...) {
|
if !tcp.Context.Close(msg, arg...) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
|
|||||||
msg.Option(v.Name, v.Value)
|
msg.Option(v.Name, v.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.Log("cmd", nil, "%s [] %v", key, msg.Meta["option"])
|
msg.Log("cmd", "%s [] %v", key, msg.Meta["option"])
|
||||||
msg.Put("option", "request", r).Put("option", "response", w)
|
msg.Put("option", "request", r).Put("option", "response", w)
|
||||||
|
|
||||||
if hand(msg, msg.Target(), key); msg.Has("redirect") {
|
if hand(msg, msg.Target(), key); msg.Has("redirect") {
|
||||||
@ -126,20 +126,20 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
|
|||||||
// }}}
|
// }}}
|
||||||
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
||||||
if web.Message != nil {
|
if web.Message != nil {
|
||||||
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
web.Log("cmd", "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
||||||
|
|
||||||
if web.Confs("logheaders") {
|
if web.Confs("logheaders") {
|
||||||
for k, v := range r.Header {
|
for k, v := range r.Header {
|
||||||
web.Log("info", nil, "%s: %v", k, v)
|
web.Log("info", "%s: %v", k, v)
|
||||||
}
|
}
|
||||||
web.Log("info", nil, "")
|
web.Log("info", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.ParseForm(); len(r.PostForm) > 0 {
|
if r.ParseForm(); len(r.PostForm) > 0 {
|
||||||
for k, v := range r.PostForm {
|
for k, v := range r.PostForm {
|
||||||
web.Log("info", nil, "%s: %v", k, v)
|
web.Log("info", "%s: %v", k, v)
|
||||||
}
|
}
|
||||||
web.Log("info", nil, "")
|
web.Log("info", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,9 +147,9 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
|||||||
|
|
||||||
if web.Message != nil && web.Confs("logheaders") {
|
if web.Message != nil && web.Confs("logheaders") {
|
||||||
for k, v := range w.Header() {
|
for k, v := range w.Header() {
|
||||||
web.Log("info", nil, "%s: %v", k, v)
|
web.Log("info", "%s: %v", k, v)
|
||||||
}
|
}
|
||||||
web.Log("info", nil, "")
|
web.Log("info", "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,7 +167,6 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
web.Context.Master(nil)
|
|
||||||
web.Caches["route"] = &ctx.Cache{Name: "请求路径", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
web.Caches["route"] = &ctx.Cache{Name: "请求路径", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
||||||
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已初始化"}
|
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已初始化"}
|
||||||
web.Caches["master"] = &ctx.Cache{Name: "服务入口(yes/no)", Value: "no", Help: "服务入口"}
|
web.Caches["master"] = &ctx.Cache{Name: "服务入口(yes/no)", Value: "no", Help: "服务入口"}
|
||||||
@ -193,7 +192,7 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
m.Cap("directory", arg[0])
|
m.Cap("directory", arg[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Travel(m.Target(), func(m *ctx.Message) bool {
|
m.Travel(func(m *ctx.Message, i int) bool {
|
||||||
if h, ok := m.Target().Server.(http.Handler); ok && m.Cap("register") == "no" {
|
if h, ok := m.Target().Server.(http.Handler); ok && m.Cap("register") == "no" {
|
||||||
m.Cap("register", "yes")
|
m.Cap("register", "yes")
|
||||||
m.Capi("nroute", 1)
|
m.Capi("nroute", 1)
|
||||||
@ -208,12 +207,12 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
})
|
})
|
||||||
|
|
||||||
if s, ok := p.Server.(MUX); ok {
|
if s, ok := p.Server.(MUX); ok {
|
||||||
m.Log("info", p, "route %s -> %s", m.Cap("route"), m.Target().Name)
|
m.Log("info", "route %s -> %s", m.Cap("route"), m.Target().Name)
|
||||||
s.Handle(m.Cap("route"), http.StripPrefix(path.Dir(m.Cap("route")), h))
|
s.Handle(m.Cap("route"), http.StripPrefix(path.Dir(m.Cap("route")), h))
|
||||||
}
|
}
|
||||||
|
|
||||||
if s, ok := m.Target().Server.(MUX); ok && m.Cap("directory") != "" {
|
if s, ok := m.Target().Server.(MUX); ok && m.Cap("directory") != "" {
|
||||||
m.Log("info", nil, "dir / -> [%s]", m.Cap("directory"))
|
m.Log("info", "dir / -> [%s]", m.Cap("directory"))
|
||||||
s.Handle("/", http.FileServer(http.Dir(m.Cap("directory"))))
|
s.Handle("/", http.FileServer(http.Dir(m.Cap("directory"))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,8 +238,8 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
|
|
||||||
m.Cap("master", "yes")
|
m.Cap("master", "yes")
|
||||||
m.Cap("stream", m.Cap("address"))
|
m.Cap("stream", m.Cap("address"))
|
||||||
m.Log("info", nil, "address [%s]", m.Cap("address"))
|
m.Log("info", "address [%s]", m.Cap("address"))
|
||||||
m.Log("info", nil, "protocol [%s]", m.Cap("protocol"))
|
m.Log("info", "protocol [%s]", m.Cap("protocol"))
|
||||||
web.Server = &http.Server{Addr: m.Cap("address"), Handler: web}
|
web.Server = &http.Server{Addr: m.Cap("address"), Handler: web}
|
||||||
|
|
||||||
web.Configs["logheaders"] = &ctx.Config{Name: "日志输出报文头(yes/no)", Value: "no", Help: "日志输出报文头"}
|
web.Configs["logheaders"] = &ctx.Config{Name: "日志输出报文头(yes/no)", Value: "no", Help: "日志输出报文头"}
|
||||||
@ -249,8 +248,8 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
if web.Message = m; m.Cap("protocol") == "https" {
|
if web.Message = m; m.Cap("protocol") == "https" {
|
||||||
web.Caches["cert"] = &ctx.Cache{Name: "服务证书", Value: m.Conf("cert"), Help: "服务证书"}
|
web.Caches["cert"] = &ctx.Cache{Name: "服务证书", Value: m.Conf("cert"), Help: "服务证书"}
|
||||||
web.Caches["key"] = &ctx.Cache{Name: "服务密钥", Value: m.Conf("key"), Help: "服务密钥"}
|
web.Caches["key"] = &ctx.Cache{Name: "服务密钥", Value: m.Conf("key"), Help: "服务密钥"}
|
||||||
m.Log("info", nil, "cert [%s]", m.Cap("cert"))
|
m.Log("info", "cert [%s]", m.Cap("cert"))
|
||||||
m.Log("info", nil, "key [%s]", m.Cap("key"))
|
m.Log("info", "key [%s]", m.Cap("key"))
|
||||||
|
|
||||||
web.Server.ListenAndServeTLS(m.Cap("cert"), m.Cap("key"))
|
web.Server.ListenAndServeTLS(m.Cap("cert"), m.Cap("key"))
|
||||||
} else {
|
} else {
|
||||||
@ -348,7 +347,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
|
|
||||||
method := m.Confx("method")
|
method := m.Confx("method")
|
||||||
uri := web.Merge(m, arg[0], arg[1:]...)
|
uri := web.Merge(m, arg[0], arg[1:]...)
|
||||||
m.Log("info", nil, "GET %s", uri)
|
m.Log("info", "GET %s", uri)
|
||||||
m.Echo("%s: %s\n", method, uri)
|
m.Echo("%s: %s\n", method, uri)
|
||||||
|
|
||||||
var body io.Reader
|
var body io.Reader
|
||||||
@ -414,7 +413,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range res.Header {
|
for k, v := range res.Header {
|
||||||
m.Log("info", nil, "%s: %v", k, v)
|
m.Log("info", "%s: %v", k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Confs("output") {
|
if m.Confs("output") {
|
||||||
|
@ -79,7 +79,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
if word[i] == "rep{" {
|
if word[i] == "rep{" {
|
||||||
state.star = s
|
state.star = s
|
||||||
yac.mat[x.s][x.c] = state
|
yac.mat[x.s][x.c] = state
|
||||||
yac.Log("debug", nil, "REP(%d, %d): %v", x.s, x.c, state)
|
yac.Log("debug", "REP(%d, %d): %v", x.s, x.c, state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "mul{":
|
case "mul{":
|
||||||
@ -106,7 +106,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
} else {
|
} else {
|
||||||
yac.Capi("nnode", 1)
|
yac.Capi("nnode", 1)
|
||||||
}
|
}
|
||||||
yac.Log("debug", nil, "GET(%d, %d): %v \033[31m(%s)\033[0m", s, c, state, word[i])
|
yac.Log("debug", "GET(%d, %d): %v \033[31m(%s)\033[0m", s, c, state, word[i])
|
||||||
|
|
||||||
if state.next == 0 {
|
if state.next == 0 {
|
||||||
state.next = yac.Capi("nline", 1) - 1
|
state.next = yac.Capi("nline", 1) - 1
|
||||||
@ -121,7 +121,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
|
|
||||||
ends = append(ends, &Point{s, c})
|
ends = append(ends, &Point{s, c})
|
||||||
points = append(points, &Point{s, c})
|
points = append(points, &Point{s, c})
|
||||||
yac.Log("debug", nil, "SET(%d, %d): %v", s, c, state)
|
yac.Log("debug", "SET(%d, %d): %v", s, c, state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,7 +150,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
}
|
}
|
||||||
|
|
||||||
if void {
|
if void {
|
||||||
yac.Log("debug", nil, "DEL: %d-%d", yac.Capi("nline")-1, yac.Capi("nline", 0, s))
|
yac.Log("debug", "DEL: %d-%d", yac.Capi("nline")-1, yac.Capi("nline", 0, s))
|
||||||
yac.mat = yac.mat[:s]
|
yac.mat = yac.mat[:s]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -161,7 +161,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
*state = *yac.mat[p.s][p.c]
|
*state = *yac.mat[p.s][p.c]
|
||||||
|
|
||||||
if state.next == s {
|
if state.next == s {
|
||||||
yac.Log("debug", nil, "GET(%d, %d): %v", p.s, p.c, state)
|
yac.Log("debug", "GET(%d, %d): %v", p.s, p.c, state)
|
||||||
if state.next >= len(yac.mat) {
|
if state.next >= len(yac.mat) {
|
||||||
state.next = 0
|
state.next = 0
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
state.hash = hash
|
state.hash = hash
|
||||||
}
|
}
|
||||||
yac.mat[p.s][p.c] = state
|
yac.mat[p.s][p.c] = state
|
||||||
yac.Log("debug", nil, "SET(%d, %d): %v", p.s, p.c, state)
|
yac.Log("debug", "SET(%d, %d): %v", p.s, p.c, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
if x, ok := yac.state[*state]; !ok {
|
if x, ok := yac.state[*state]; !ok {
|
||||||
@ -187,7 +187,7 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
|
|||||||
// }}}
|
// }}}
|
||||||
func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line string, level int) (string, []string) { // {{{
|
func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line string, level int) (string, []string) { // {{{
|
||||||
if m.Confs("debug") {
|
if m.Confs("debug") {
|
||||||
m.Log("debug", nil, "%s\\%d %s(%d): %s", m.Conf("label")[0:level], level, yac.name(page), page, line)
|
m.Log("debug", "%s\\%d %s(%d): %s", m.Conf("label")[0:level], level, yac.name(page), page, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
hash, word := 0, []string{}
|
hash, word := 0, []string{}
|
||||||
@ -252,7 +252,7 @@ func (yac *YAC) parse(m *ctx.Message, out *ctx.Message, page int, void int, line
|
|||||||
}
|
}
|
||||||
|
|
||||||
if m.Confs("debug") {
|
if m.Confs("debug") {
|
||||||
m.Log("debug", nil, "%s/%d %s(%d): %v", m.Conf("label")[0:level], level, yac.name(page), page, word)
|
m.Log("debug", "%s/%d %s(%d): %v", m.Conf("label")[0:level], level, yac.name(page), page, word)
|
||||||
}
|
}
|
||||||
|
|
||||||
return line, word
|
return line, word
|
||||||
@ -373,7 +373,7 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
|
|||||||
"nparse": &ctx.Cache{Name: "nparse", Value: "0", Help: "解析器数量"},
|
"nparse": &ctx.Cache{Name: "nparse", Value: "0", Help: "解析器数量"},
|
||||||
},
|
},
|
||||||
Configs: map[string]*ctx.Config{
|
Configs: map[string]*ctx.Config{
|
||||||
"debug": &ctx.Config{Name: "debug", Value: "true", Help: "词法集合的最大数量"},
|
"debug": &ctx.Config{Name: "debug", Value: "false", Help: "词法集合的最大数量"},
|
||||||
"ncell": &ctx.Config{Name: "词法上限", Value: "128", Help: "词法集合的最大数量"},
|
"ncell": &ctx.Config{Name: "词法上限", Value: "128", Help: "词法集合的最大数量"},
|
||||||
"nlang": &ctx.Config{Name: "语法上限", Value: "32", Help: "语法集合的最大数量"},
|
"nlang": &ctx.Config{Name: "语法上限", Value: "32", Help: "语法集合的最大数量"},
|
||||||
"label": &ctx.Config{Name: "嵌套标记", Value: "####################", Help: "嵌套层级日志的标记"},
|
"label": &ctx.Config{Name: "嵌套标记", Value: "####################", Help: "嵌套层级日志的标记"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user