forked from x/ContextOS
mod ctx&msg 私有化了某些接口
This commit is contained in:
parent
4cb2b90da5
commit
46c602586e
@ -56,13 +56,13 @@ func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
|
|
||||||
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool {
|
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, "%d close %s %s", Pulse.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source"))
|
m.Log("info", nil, "%d close %s %s", Pulse.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source"))
|
||||||
mdb.DB.Close()
|
mdb.DB.Close()
|
||||||
mdb.DB = nil
|
mdb.DB = nil
|
||||||
}
|
}
|
||||||
case m.Source:
|
case m.Source():
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -80,12 +80,12 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
m.Assert(len(arg) > 0, "缺少参数")
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...)
|
m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...)
|
||||||
Pulse.Cap("stream", Pulse.Cap("nsource"))
|
Pulse.Cap("stream", Pulse.Cap("nsource"))
|
||||||
m.Echo(m.Target.Name)
|
m.Echo(m.Target().Name)
|
||||||
}},
|
}},
|
||||||
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
||||||
Appends: map[string]string{"last": "最后插入元组的标识", "nrow": "修改元组的数量"},
|
Appends: map[string]string{"last": "最后插入元组的标识", "nrow": "修改元组的数量"},
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
mdb, ok := m.Target.Server.(*MDB)
|
mdb, ok := m.Target().Server.(*MDB)
|
||||||
m.Assert(ok, "目标模块类型错误")
|
m.Assert(ok, "目标模块类型错误")
|
||||||
m.Assert(len(arg) > 0, "缺少参数")
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Assert(mdb.DB != nil, "数据库未打开")
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
@ -108,7 +108,7 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
m.Log("info", nil, "last(%d) nrow(%d)", id, n)
|
m.Log("info", nil, "last(%d) nrow(%d)", id, n)
|
||||||
}},
|
}},
|
||||||
"query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
mdb, ok := m.Target.Server.(*MDB)
|
mdb, ok := m.Target().Server.(*MDB)
|
||||||
m.Assert(ok, "目标模块类型错误")
|
m.Assert(ok, "目标模块类型错误")
|
||||||
m.Assert(len(arg) > 0, "缺少参数")
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Assert(mdb.DB != nil, "数据库未打开")
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
|
@ -53,13 +53,13 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
|
|
||||||
func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool {
|
func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool {
|
||||||
switch nfs.Context {
|
switch nfs.Context {
|
||||||
case m.Target:
|
case m.Target():
|
||||||
if nfs.file != nil {
|
if nfs.file != nil {
|
||||||
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
|
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
|
||||||
nfs.file.Close()
|
nfs.file.Close()
|
||||||
nfs.file = nil
|
nfs.file = nil
|
||||||
}
|
}
|
||||||
case m.Source:
|
case m.Source():
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -75,10 +75,10 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
|
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
|
||||||
m.Echo(m.Target.Name)
|
m.Echo(m.Target().Name)
|
||||||
}},
|
}},
|
||||||
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
nfs, ok := m.Target.Server.(*NFS)
|
nfs, ok := m.Target().Server.(*NFS)
|
||||||
m.Assert(ok)
|
m.Assert(ok)
|
||||||
|
|
||||||
var e error
|
var e error
|
||||||
@ -102,7 +102,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
}
|
}
|
||||||
}},
|
}},
|
||||||
"write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
nfs, ok := m.Target.Server.(*NFS)
|
nfs, ok := m.Target().Server.(*NFS)
|
||||||
if m.Assert(ok); len(arg) > 1 {
|
if m.Assert(ok); len(arg) > 1 {
|
||||||
m.Cap("pos", arg[1])
|
m.Cap("pos", arg[1])
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
for {
|
for {
|
||||||
c, e := tcp.Accept()
|
c, e := tcp.Accept()
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String())
|
m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source()).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@ -104,7 +104,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
|
|
||||||
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
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", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream"))
|
||||||
tcp.Listener.Close()
|
tcp.Listener.Close()
|
||||||
@ -115,10 +115,10 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
|||||||
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 msg.Master(tcp.Context); !tcp.Context.Close(msg, arg...) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -145,12 +145,12 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
|||||||
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
|
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
|
||||||
}},
|
}},
|
||||||
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
tcp, ok := m.Target.Server.(*TCP)
|
tcp, ok := m.Target().Server.(*TCP)
|
||||||
m.Assert(ok && tcp.Conn != nil)
|
m.Assert(ok && tcp.Conn != nil)
|
||||||
tcp.Conn.Write([]byte(arg[0]))
|
tcp.Conn.Write([]byte(arg[0]))
|
||||||
}},
|
}},
|
||||||
"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
tcp, ok := m.Target.Server.(*TCP)
|
tcp, ok := m.Target().Server.(*TCP)
|
||||||
m.Assert(ok && tcp.Conn != nil)
|
m.Assert(ok && tcp.Conn != nil)
|
||||||
size, e := strconv.Atoi(arg[0])
|
size, e := strconv.Atoi(arg[0])
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
@ -49,7 +49,7 @@ func (web *WEB) AppendJson(msg *ctx.Message) string {
|
|||||||
|
|
||||||
func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string)) {
|
func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string)) {
|
||||||
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
msg := m.Spawn(m.Target).Set("detail", key)
|
msg := m.Spawn(m.Target()).Set("detail", key)
|
||||||
for k, v := range r.Form {
|
for k, v := range r.Form {
|
||||||
msg.Add("option", k, v...)
|
msg.Add("option", k, v...)
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
|
|||||||
|
|
||||||
msg.Log("cmd", nil, "%s [] %v", key, msg.Meta["option"])
|
msg.Log("cmd", nil, "%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); len(msg.Meta["append"]) > 0 {
|
if hand(msg, msg.Target(), key); len(msg.Meta["append"]) > 0 {
|
||||||
msg.Set("result", web.AppendJson(msg))
|
msg.Set("result", web.AppendJson(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
web.ServeMux = http.NewServeMux()
|
web.ServeMux = http.NewServeMux()
|
||||||
if mux, ok := m.Target.Server.(MUX); ok {
|
if mux, ok := m.Target().Server.(MUX); ok {
|
||||||
for k, x := range web.Commands {
|
for k, x := range web.Commands {
|
||||||
if k[0] == '/' {
|
if k[0] == '/' {
|
||||||
mux.Trans(m, k, x.Hand)
|
mux.Trans(m, k, x.Hand)
|
||||||
@ -135,13 +135,13 @@ 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(m.Target(), func(m *ctx.Message) 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")
|
||||||
|
|
||||||
p, i := m.Target, 0
|
p, i := m.Target(), 0
|
||||||
m.BackTrace(func(m *ctx.Message) bool {
|
m.BackTrace(func(m *ctx.Message) bool {
|
||||||
p = m.Target
|
p = m.Target()
|
||||||
if i++; i == 2 {
|
if i++; i == 2 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -149,11 +149,11 @@ 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", p, "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", nil, "dir / -> [%s]", m.Cap("directory"))
|
||||||
s.Handle("/", http.FileServer(http.Dir(m.Cap("directory"))))
|
s.Handle("/", http.FileServer(http.Dir(m.Cap("directory"))))
|
||||||
}
|
}
|
||||||
@ -194,8 +194,8 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
|
|
||||||
func (web *WEB) Close(m *ctx.Message, arg ...string) bool {
|
func (web *WEB) Close(m *ctx.Message, arg ...string) bool {
|
||||||
switch web.Context {
|
switch web.Context {
|
||||||
case m.Target:
|
case m.Target():
|
||||||
case m.Source:
|
case m.Source():
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -205,10 +205,10 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
Configs: map[string]*ctx.Config{},
|
Configs: map[string]*ctx.Config{},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Set("detail", arg...).Target.Start(m)
|
m.Set("detail", arg...).Target().Start(m)
|
||||||
}},
|
}},
|
||||||
"route": &ctx.Command{Name: "route directory|template|script route content", Help: "添加应用内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"route": &ctx.Command{Name: "route directory|template|script route content", Help: "添加应用内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
mux, ok := m.Target.Server.(MUX)
|
mux, ok := m.Target().Server.(MUX)
|
||||||
m.Assert(ok, "模块类型错误")
|
m.Assert(ok, "模块类型错误")
|
||||||
m.Assert(len(arg) == 3, "缺少参数")
|
m.Assert(len(arg) == 3, "缺少参数")
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user