1
0
forked from x/ContextOS

mac mod context.close 完善了模块的退出机制

This commit is contained in:
shaoying 2017-12-14 23:48:24 +08:00
parent 7fffcde987
commit debceb6c0f
5 changed files with 65 additions and 36 deletions

View File

@ -44,7 +44,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
if len(arg) > 0 { // {{{
bs := md5.Sum([]byte(fmt.Sprintln("用户密码:%s", arg[0])))
m.Assert(x.Value == "" || x.Value == hex.EncodeToString(bs[:]), "密码错误")
m.Cap("expire", fmt.Sprintf("%d", time.Now().Unix()+int64(m.Confi("expire"))))
m.Cap("expire", fmt.Sprintf("%d", time.Now().Unix()+int64(Pulse.Confi("expire"))))
return hex.EncodeToString(bs[:])
}
return x.Value
@ -77,11 +77,12 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{
if len(arg) > 1 {
if m.Cap("sessid") == "" {
m.Cap("sessid", aaa.session(arg[1]))
m.Capi("nuser", 1)
Pulse.Capi("nuser", 1)
}
m.Log("info", m.Source, "login %s %s", m.Cap("group", arg[0]), m.Cap("username", arg[1]))
m.Log("info", m.Source, "create %s %s", m.Cap("group", arg[0]), m.Cap("username", arg[1]))
m.Cap("stream", m.Cap("username"))
}
m.Log("info", m.Source, "login %s %s", m.Cap("group"), m.Cap("username"))
return false
}
@ -94,15 +95,18 @@ func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { // {{{
switch aaa.Context {
case m.Target:
if len(aaa.Context.Requests) == 0 {
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
}
case m.Source:
}
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
return true
}
// }}}
var Pulse *ctx.Message
var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
Caches: map[string]*ctx.Cache{
"nuser": &ctx.Cache{Name: "用户数量", Value: "0", Help: "用户数量"},
@ -131,6 +135,10 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
m.Log("info", m.Source, "logon %s", m.Cap("group"), m.Cap("username"))
if m.Name != "" {
c.Requests = append(c.Requests, m)
m.Index = len(m.Target.Requests)
}
return m.Cap("username")
}
case 2, 3:
@ -139,12 +147,17 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
username, password = arg[1], arg[2]
}
if username == m.Conf("rootname") {
if username == Pulse.Conf("rootname") {
m.Set("detail", group, username).Target.Start(m)
} else if msg := m.Find(username); msg == nil {
m.Start(username, "认证用户", group, username)
} else {
m.Target = msg.Target
msg.Target.Start(msg)
if m.Name != "" {
m.Target.Requests = append(m.Target.Requests, m)
m.Index = len(m.Target.Requests)
}
}
m.Cap("password", password)
@ -162,7 +175,6 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
},
},
}
var Pulse *ctx.Message
func init() {
aaa := &AAA{}

View File

@ -239,6 +239,9 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
if cli.Context != Index {
cli.Owner = nil
}
if cli.Context == Index {
Pulse = m
}
cli.target = cli.Context
cli.alias = map[string]string{
@ -286,6 +289,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
cli.echo("password>")
fmt.Fscanln(cli.in, &password)
msg.Name = "aaa"
msg.Wait = make(chan bool)
if msg.Cmd("login", username, password) == "" {
cli.echo("登录失败")
@ -299,7 +303,6 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
cli.Sessions = make(map[string]*ctx.Message)
}
cli.Sessions["aaa"] = msg
msg.Name = "aaa"
}
} else {
m.Cap("stream", "stdout")
@ -358,7 +361,17 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
switch cli.Context {
case m.Target:
if len(cli.Context.Requests) == 0 {
m.Log("info", nil, "%s close %v", Pulse.Cap("nterm"), arg)
}
case m.Source:
if m.Name == "aaa" {
msg := m.Spawn(cli.Context)
msg.Master = cli.Context
if !cli.Context.Close(msg, arg...) {
return false
}
}
}
return true
@ -366,6 +379,7 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
// }}}
var Pulse *ctx.Message
var Index = &ctx.Context{Name: "cli", Help: "管理中心",
Caches: map[string]*ctx.Cache{
"nterm": &ctx.Cache{Name: "终端数量", Value: "0", Help: "已经运行的终端数量"},

View File

@ -72,7 +72,6 @@ type Context struct {
Owner *Context
Group string
Pulse *Message
Server
}
@ -127,8 +126,9 @@ func (c *Context) Begin(m *Message) *Context { // {{{
}
}
c.Requests = []*Message{m}
m.Index = 1
c.Historys = []*Message{m}
c.Requests = []*Message{m}
if c.Server != nil {
c.Server.Begin(m, m.Meta["detail"]...)
@ -161,12 +161,24 @@ func (c *Context) Start(m *Message) bool { // {{{
// }}}
func (c *Context) Close(m *Message, arg ...string) bool { // {{{
m.Log("close", c, "close %v", arg)
if m.Target == c {
for k, v := range c.Sessions {
delete(c.Sessions, k)
if v.Target != c && !v.Target.Close(v, arg...) {
return false
switch {
case m.Index == 0:
for i := len(c.Requests) - 1; i >= 0; i-- {
v := c.Requests[i]
v.Index = -1
if v.Source != c && !v.Source.Close(v, arg...) {
v.Index = i
return false
}
c.Requests = c.Requests[:i]
}
case 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]
}
}
@ -174,34 +186,26 @@ func (c *Context) Close(m *Message, arg ...string) bool { // {{{
return false
}
if m.Source == c {
if m.Source == c && m.Target != c {
if _, ok := c.Sessions[m.Name]; ok {
delete(c.Sessions, m.Name)
m.Target.Server.Close(m, arg...)
}
} else {
if m.Index == -1 {
return true
}
}
if len(c.Sessions) > 0 {
return false
}
if m.Cap("status") == "close" {
return true
}
if len(c.Requests) > 0 {
return false
}
m.Log(m.Cap("status", "close"), c, "%d server %v", m.root.Capi("nserver", -1)+1, arg)
if c.context != nil && len(c.contexts) == 0 {
m.Log("close", c, "%d context %v", m.root.Capi("ncontext", -1)+1, arg)
delete(c.context.contexts, c.Name)
}
for _, v := range c.Requests {
if v.Source != c && v.Index != -1 {
v.Index = -1
v.Source.Close(v, arg...)
for _, v := range c.Sessions {
if v.Target != c {
v.Target.Close(v, arg...)
}
}
@ -1077,7 +1081,8 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
// }}}
var Index = &Context{Name: "ctx", Help: "元始模块",
var Pulse = &Message{code: 0, time: time.Now(), Wait: make(chan bool), Source: Index, Master: Index, Target: Index}
var Index = &Context{Name: "ctx", Help: "模块中心",
Caches: map[string]*Cache{
"nserver": &Cache{Name: "服务数量", Value: "0", Help: "显示已经启动运行模块的数量"},
"ncontext": &Cache{Name: "模块数量", Value: "0", Help: "显示功能树已经注册模块的数量"},
@ -1562,8 +1567,6 @@ var Index = &Context{Name: "ctx", Help: "元始模块",
},
}
var Pulse = &Message{code: 0, time: time.Now(), Wait: make(chan bool), Source: Index, Master: Index, Target: Index}
func init() {
Pulse.root = Pulse
Index.root = Index

View File

@ -30,7 +30,7 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
return false
}
var Index = &ctx.Context{Name: "ssh", Help: "加密终端",
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{},

View File

@ -4,13 +4,13 @@ import (
"context"
_ "context/aaa"
_ "context/cli"
_ "context/ssh"
_ "context/mdb"
_ "context/nfs"
_ "context/tcp"
_ "context/web"
_ "context/ssh"
_ "context/lex"
"os"