1
0
forked from x/ContextOS

vps add ssh.send 添加主机路由

This commit is contained in:
shylinux 2018-04-22 11:03:18 +08:00
parent e463fe87cd
commit 1a66c4c170
7 changed files with 307 additions and 208 deletions

View File

@ -261,8 +261,8 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
switch cli.Context {
case m.Target():
m.Echo(cli.nfs.Cap("return"))
if p, ok := m.Source().Server.(*CLI); ok {
p.target = cli.target
if _, ok := m.Source().Server.(*CLI); ok {
// p.target = cli.target
}
case m.Source():
if m.Name == "aaa" {

View File

@ -295,7 +295,7 @@ type Message struct {
Index int
ncallback int
callback func(ok bool, sub *Message) (done bool, up *Message)
callback func(msg *Message) (sub *Message)
Template *Message
}
@ -1042,8 +1042,6 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
m.target.Historys = make([]*Message, 0, 10)
}
m.target.Historys = append(m.target.Historys, m)
m.Back(false, nil)
})
return m.Get("result")
@ -1126,34 +1124,43 @@ func (m *Message) Cmd(arg ...interface{}) *Message { // {{{
}
// }}}
func (m *Message) Call(cb func(ok bool, msg *Message) (done bool, sub *Message), cmd bool) *Message { // {{{
func (m *Message) Call(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
m.callback = cb
m.message.ncallback++
m.Wait = nil
if cmd {
m.Cmd()
m.Cmd(arg...)
return m
}
// }}}
func (m *Message) Back(msg *Message) *Message { // {{{
if msg == nil || m.callback == nil {
return m
}
m.Log("callback", nil, "%v %v", msg.Meta["result"], msg.Meta["append"])
if sub := m.callback(msg); sub != nil && m.message != nil && m.message != m {
m.Log("fuck", nil, "31")
m.message.Back(sub)
}
m.Log("fuck", nil, "41")
return m
}
// }}}
func (m *Message) Back(ok bool, msg *Message) *Message { // {{{
if m.callback == nil {
return m
}
m.Log("info", nil, "back %v %v", m.Meta["result"], m.Meta["append"])
done, sub := m.callback(ok, msg)
if done {
m.callback = nil
m.message.ncallback--
}
if sub != nil || m.message.ncallback == 0 {
m.message.Back(ok, sub)
}
func (m *Message) CallBack(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
wait := make(chan bool)
m.Log("fuck", nil, "callback 1")
go m.Call(func(sub *Message) *Message {
m.Log("fuck", nil, "callback 4")
wait <- true
m.Log("fuck", nil, "callback 5")
return cb(sub)
}, arg...)
m.Log("fuck", nil, "callback 2")
<-wait
m.Log("fuck", nil, "callback 3")
return m
}
@ -1388,9 +1395,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
for i, v := range m.target.Requests {
m.Echo("%d %s\n", i, v.Format())
for i, v := range v.messages {
if v.Detail(0) != "log" {
m.Echo(" %d %s\n", i, v.Format())
}
}
}
m.Echo("\033[32msessions:\033[0m\n")
for k, v := range m.target.Sessions {
@ -1401,9 +1410,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
for i, v := range m.target.Historys {
m.Echo("%d %s\n", i, v.Format())
for i, v := range v.messages {
if v.Detail(0) != "log" {
m.Echo(" %d %s\n", i, v.Format())
}
}
}
case 1:
n, e := strconv.Atoi(arg[0])
m.Assert(e)

View File

@ -193,6 +193,8 @@ func init() {
"conf": 33,
"search": 35,
"find": 35,
"callback": 35,
"lock": 35,
"spawn": 35,
"begin": 36,
"start": 36,

View File

@ -4,6 +4,7 @@ import ( // {{{
"contexts"
"bufio"
"encoding/json"
"fmt"
"github.com/skip2/go-qrcode"
"io"
@ -20,6 +21,7 @@ type NFS struct {
*bufio.Reader
*bufio.Writer
send map[int]*ctx.Message
target *ctx.Context
in *os.File
out *os.File
@ -87,15 +89,17 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.Reader = bufio.NewReader(nfs.io)
nfs.Writer = bufio.NewWriter(nfs.io)
nfs.send = make(map[int]*ctx.Message)
nfs.target = m.Target()
if target, ok := m.Data["target"]; ok {
nfs.target = target.(*ctx.Context)
}
target, msg := m.Target(), m.Spawn(m.Target())
nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: target.Name, Help: "文件名"}
msg := m.Spawn(nfs.target)
nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: nfs.target.Name, Help: "文件名"}
nsend := 0
for {
line, e := nfs.Reader.ReadString('\n')
m.Assert(e)
// m.Log("debug", nil, "recv(%d): %s", len(line), line)
if line = strings.TrimSpace(line); len(line) > 0 {
ls := strings.SplitN(line, ":", 2)
@ -104,61 +108,72 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Assert(e)
switch ls[0] {
case "nsend":
n, e := strconv.Atoi(ls[1])
m.Assert(e)
nsend = n
case "detail":
msg.Add("detail", ls[1])
case "result":
msg.Add("result", ls[1])
default:
msg.Add("option", ls[0], ls[1])
}
continue
}
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") {
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
if msg.Has("detail") {
msg.Log("info", nil, "%d recv", m.Capi("nrecv", 1))
msg.Log("info", nil, "detail: %v", msg.Meta["detail"])
msg.Log("info", nil, "option: %v", msg.Meta["option"])
msg.Options("stdio", false)
func() {
fuck := msg
fuck.Call(func(ok bool, cmd *ctx.Message) (bool, *ctx.Message) {
if ok {
target = fuck.Target()
m.Cap("target", target.Name)
for _, v := range fuck.Meta["result"] {
fmt.Fprintf(nfs.Writer, "result: %s\n", url.QueryEscape(v))
cmd := msg
cmd.Call(func(sub *ctx.Message) *ctx.Message {
for _, v := range sub.Meta["result"] {
n, e := fmt.Fprintf(nfs.Writer, "result: %s\n", url.QueryEscape(v))
sub.Assert(e)
sub.Log("fuck", nil, "write %d", n)
}
fmt.Fprintf(nfs.Writer, "nsend: %s\n", fuck.Get("nrecv"))
for _, k := range fuck.Meta["append"] {
for _, v := range fuck.Meta[k] {
fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
sub.Append("nsend", sub.Option("nsend"))
for _, k := range sub.Meta["append"] {
for _, v := range sub.Meta[k] {
n, e := fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
sub.Assert(e)
sub.Log("fuck", nil, "write %d", n)
}
}
fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush()
if fuck.Has("io") {
if f, ok := fuck.Data["io"].(io.ReadCloser); ok {
sub.Log("info", nil, "%d send", sub.Optioni("nsend"))
sub.Log("info", nil, "result: %v", sub.Meta["result"])
sub.Log("info", nil, "append: %v", sub.Meta["append"])
n, e := fmt.Fprintf(nfs.Writer, "\n")
sub.Assert(e)
sub.Log("sub", nil, "write %d", n)
e = nfs.Writer.Flush()
sub.Assert(e)
if sub.Has("io") {
if f, ok := sub.Data["io"].(io.ReadCloser); ok {
io.Copy(nfs.Writer, f)
nfs.Writer.Flush()
f.Close()
}
}
return true, fuck
}
return false, nil
}, false).Cmd(fuck.Meta["detail"])
return sub
})
}()
} else {
msg.Log("info", nil, "%d echo: %v", nsend, msg.Meta["result"])
m.Cap("result", msg.Get("result"))
msg.Meta["append"] = msg.Meta["option"]
delete(msg.Meta, "option")
send := nfs.send[nsend]
send.Meta = msg.Meta
msg.Log("info", nil, "%s send", msg.Meta["nsend"])
msg.Log("info", nil, "result: %v", msg.Meta["result"])
msg.Log("info", nil, "append: %v", msg.Meta["append"])
send := nfs.send[msg.Appendi("nsend")]
send.Copy(msg, "result")
send.Copy(msg, "append")
if send.Has("io") {
if f, ok := send.Data["io"].(io.WriteCloser); ok {
@ -167,11 +182,11 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
}
}
send.Recv <- true
send.Back(send)
}
msg = m.Spawn(target)
m.Cap("target", target.Name)
msg = m.Spawn(nfs.target)
m.Cap("target", nfs.target.Name)
}
return true
}
@ -225,6 +240,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
for text = nfs.buf[pos] + "\n"; text != ""; {
line := m.Spawn(yac.Target())
line.Optioni("pos", pos)
line.Options("stdio", true)
line.Put("option", "cli", cli.Target())
text = line.Cmd("parse", "line", "void", text).Get("result")
cli.Target(line.Data["cli"].(*ctx.Context))
@ -318,34 +334,31 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}},
"listen": &ctx.Command{Name: "listen args...", Help: "启动文件服务, args: 参考tcp模块, listen命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("pub", "tcp") // {{{
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) {
if ok {
sub := msg.Spawn(m.Target())
sub.Put("option", "io", msg.Data["io"])
sub.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件")
msg := m.Find("tcp") // {{{
msg.Call(func(com *ctx.Message) *ctx.Message {
sub := com.Spawn(m.Target())
sub.Put("option", "target", m.Source())
sub.Put("option", "io", com.Data["io"])
sub.Cmd(com.Meta["detail"])
sub.Cap("stream", com.Target().Name)
return false, sub
}
return false, nil
}, false).Cmd(m.Meta["detail"])
return sub
}, m.Meta["detail"])
// }}}
}},
"accept": &ctx.Command{Name: "accept args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件")
}},
"dial": &ctx.Command{Name: "dial args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("com", "tcp") // {{{
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) {
if ok {
sub := msg.Spawn(m.Target())
sub.Put("option", "io", msg.Data["io"])
msg.CallBack(func(com *ctx.Message) *ctx.Message {
sub := com.Spawn(m.Target())
sub.Put("option", "target", m.Source())
sub.Put("option", "io", com.Data["io"])
sub.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件")
sub.Cap("stream", msg.Target().Name)
sub.Cap("stream", com.Target().Name)
m.Target(sub.Target())
return true, sub
}
return false, nil
}, false).Cmd(m.Meta["detail"])
return sub
}, m.Meta["detail"])
// }}}
}},
"send": &ctx.Command{Name: "send [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -365,7 +378,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}
} else {
nfs.send[m.Optioni("nrecv", m.Capi("nsend", 1))] = m
nfs.send[m.Optioni("nsend", m.Capi("nsend", 1))] = m
if len(arg) > 1 && arg[0] == "file" {
info, e := os.Stat(arg[1])
@ -389,11 +402,13 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
}
}
m.Log("info", nil, "%d send", m.Optioni("nsend"))
m.Log("info", nil, "detail: %v", m.Meta["detail"])
m.Log("info", nil, "option: %v", m.Meta["option"])
fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush()
if true {
if len(arg) > 1 && arg[0] == "file" {
f, e := os.Open(arg[1])
m.Assert(e)
@ -401,10 +416,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
_, e = io.Copy(nfs.Writer, f)
}
}
m.Recv = make(chan bool)
<-m.Recv
}
} // }}}
}},
"recv": &ctx.Command{Name: "recv [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -445,9 +456,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush()
m.Recv = make(chan bool)
<-m.Recv
} // }}}
}},
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -542,6 +550,18 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}
qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0]) // }}}
}},
"json": &ctx.Command{Name: "json file", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
buf, e := json.Marshal(m.Data["data"]) // {{{
m.Assert(e)
f, e := os.Create(arg[0])
m.Assert(e)
f.Write(buf)
f.Close()
m.Echo(string(buf))
// }}}
}},
"pwd": &ctx.Command{Name: "pwd", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
wd, e := os.Getwd() // {{{

View File

@ -53,76 +53,110 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{
// }}}
func Done(m *ctx.Message, lock chan bool) {
m.Log("lock", nil, "done before %v", m.Meta["detail"])
if m.Options("stdio") {
lock <- true
}
m.Log("lock", nil, "done after %v", m.Meta["detail"])
}
func Wait(m *ctx.Message, lock chan bool) {
m.Log("lock", nil, "wait before %v", m.Meta["detail"])
if m.Options("stdio") {
<-lock
}
m.Log("lock", nil, "wait after %v", m.Meta["detail"])
}
var Pulse *ctx.Message
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
Caches: map[string]*ctx.Cache{
"nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"},
"route": &ctx.Cache{Name: "route", Value: "ssh", Help: "主机数量"},
"route": &ctx.Cache{Name: "route", Value: "com", Help: "主机数量"},
"count": &ctx.Cache{Name: "count", Value: "3", Help: "主机数量"},
"share": &ctx.Cache{Name: "share", Value: "root", Help: "主机数量"},
"level": &ctx.Cache{Name: "level", Value: "root", Help: "主机数量"},
},
Configs: map[string]*ctx.Config{
"route": &ctx.Config{Name: "route", Value: "com", Help: "主机数量"},
"route.json": &ctx.Config{Name: "route.json", Value: "var/route.json", Help: "主机数量"},
"route.png": &ctx.Config{Name: "route.png", Value: "var/route.png", Help: "主机数量"},
"type": &ctx.Config{Name: "type", Value: "terminal", Help: "主机数量"},
"kind": &ctx.Config{Name: "kind", Value: "terminal", Help: "主机数量"},
"name": &ctx.Config{Name: "name", Value: "vps", Help: "主机数量"},
"mark": &ctx.Config{Name: "mark", Value: "com", Help: "主机数量"},
},
Commands: map[string]*ctx.Command{
"listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Find("nfs")
msg.Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) {
if ok {
sub := msg.Spawn(m.Target())
msg := m.Find("nfs") // {{{
msg.Call(func(file *ctx.Message) *ctx.Message {
sub := file.Spawn(m.Target())
sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "远程主机")
sub.Cap("stream", file.Target().Name)
sub.Sess("file", "nfs."+file.Target().Name)
sub.Sess("file").Cmd("send", "context", "ssh")
sub.Sess("file").Cmd("send", "route", sub.Target().Name, msg.Cap("route"))
return false, sub
}
return false, nil
}, false).Cmd(m.Meta["detail"])
sub.Sess("file", "nfs."+file.Target().Name)
sub.Sess("file").Cmd("send", "route", sub.Target().Name, msg.Cap("route"))
return sub
}, m.Meta["detail"])
// }}}
}},
"dial": &ctx.Command{Name: "dial address protocol", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("file", "nfs")
msg.Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) {
if ok {
m.Cap("stream", msg.Target().Name)
m.Sess("file").Cmd("send", "context", "ssh")
return true, m
}
return false, nil
}, false).Cmd(m.Meta["detail"])
}},
"send": &ctx.Command{Name: "send arg...", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("file")
msg.Copy(m, "detail").Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) {
return ok, file
}, false).Cmd()
m.Copy(msg, "result")
}},
"pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Echo(m.Cap("route"))
msg := m.Sess("file", "nfs") // {{{
msg.Call(func(file *ctx.Message) *ctx.Message {
m.Cap("stream", file.Target().Name)
return m
}, m.Meta["detail"])
// }}}
}},
"route": &ctx.Command{Name: "route", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Conf("route", arg[0])
m.Conf("route", arg[0]) // {{{
m.Cap("route", arg[1]+"."+arg[0])
info := map[string]string{"route": m.Cap("route")}
msg := m.Sess("file")
msg.Put("option", "data", info)
msg.Cmd("json", m.Conf("route.json"))
png := m.Sess("file")
png.Cmd("genqr", m.Conf("route.png"), msg.Result(0))
m.Back(m)
// }}}
}},
"search": &ctx.Command{Name: "search route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*SSH); m.Assert(ok) {
"pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Echo(m.Cap("route")) // {{{
m.Back(m)
// }}}
}},
"send": &ctx.Command{Name: "send route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*SSH); m.Assert(ok) { // {{{
lock := make(chan bool)
if len(arg[0]) == 0 {
msg := m.Spawn(m.Target()).Cmd(arg[1:])
m.Copy(msg, "result")
m.Copy(msg, "append")
m.Back(m)
return
}
miss := true
self := true
target := strings.Split(arg[0], ".")
m.Travel(m.Target(), func(m *ctx.Message) bool {
if self {
self = false
return true
}
if m.Target().Name == target[0] {
msg := m.Spawn(m.Target())
msg.Call(func(ok bool, host *ctx.Message) (bool, *ctx.Message) {
m.Copy(msg, "result")
return ok, host
}, false).Cmd("send", "search", strings.Join(target[1:], "."), arg[1:])
msg := m.Sess("file")
msg.Call(func(host *ctx.Message) *ctx.Message {
m.Copy(host, "result")
m.Copy(host, "append")
Done(m, lock)
return m
}, "send", "send", strings.Join(target[1:], "."), arg[1:])
miss = false
}
@ -130,21 +164,42 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
})
if miss {
msg := m.Spawn(c)
msg.Call(func(ok bool, host *ctx.Message) (bool, *ctx.Message) {
m.Copy(msg, "result")
return ok, host
}, false).Cmd("send", "search", arg)
if target[0] == m.Conf("route") {
m.Spawn(m.Target()).Call(func(host *ctx.Message) *ctx.Message {
m.Copy(host, "result")
m.Copy(host, "append")
Done(m, lock)
return m
}, "send", strings.Join(target[1:], "."), arg[1:])
} else if m.Cap("route") != "ssh" {
msg := m.Sess("file")
msg.Call(func(host *ctx.Message) *ctx.Message {
m.Copy(host, "result")
m.Copy(host, "append")
m.Back(m)
Done(m, lock)
return nil
}, "send", "send", arg)
} else {
m.Back(m)
return
}
}
Wait(m, lock)
}
// }}}
}},
"dispatch": &ctx.Command{Name: "dispatch route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Travel(m.Target(), func(m *ctx.Message) bool {
msg := m.Spawn(m.Target())
msg.Cmd("send", arg)
return true
})
"good": &ctx.Command{Name: "good", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Append("share", m.Cap("share")) // {{{
m.Append("level", m.Cap("level"))
m.Append("type", m.Conf("type"))
m.Append("value", m.Cap("route"))
m.Append("kind", m.Conf("kind"))
m.Append("name", m.Conf("name"))
m.Append("mark", m.Conf("mark"))
m.Append("count", m.Cap("count"))
m.Back(m)
// }}}
}},
"register": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
ssh, ok := m.Target().Server.(*SSH)

View File

@ -69,7 +69,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"),
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
m.Put("append", "io", tcp.Conn).Back(true, m)
m.Put("append", "io", tcp.Conn).Back(m)
return false
case "accept":
c, e := m.Data["io"].(net.Conn)
@ -78,8 +78,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"),
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
m.Put("append", "io", tcp.Conn).Back(true, m)
m.Log("fuck", nil, "accept")
m.Put("append", "io", tcp.Conn).Back(m)
return false
default:
if m.Cap("security") != "false" {
@ -103,16 +102,9 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
c, e := tcp.Accept()
m.Assert(e)
msg := m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source())
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) {
if ok {
m.Append("stream", msg.Append("stream"))
m.Put("append", "io", msg.Data["io"])
com.Log("fuck", nil, "listen")
}
return ok, com
}, false).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接",
"accept", c.RemoteAddr().String(), m.Cap("security"), m.Cap("protocol"))
msg.Call(func(com *ctx.Message) *ctx.Message {
return com
}, "accept", c.RemoteAddr().String(), m.Cap("security"), m.Cap("protocol"))
}
return true
@ -161,6 +153,10 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
}},
"accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
}},
"dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)

View File

@ -1,12 +1,14 @@
package yac
import (
package yac // {{{
// }}}
import ( // {{{
"contexts"
"fmt"
"strconv"
"strings"
)
// }}}
type Seed struct {
page int
hash int
@ -37,14 +39,15 @@ type YAC struct {
*ctx.Context
}
func (yac *YAC) name(page int) string {
func (yac *YAC) name(page int) string { // {{{
if name, ok := yac.word[page]; ok {
return name
}
return fmt.Sprintf("yac%d", page)
}
func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Point, []*Point) {
// }}}
func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Point, []*Point) { // {{{
ss := []int{page}
sn := make([]bool, yac.Capi("nline"))
@ -183,7 +186,8 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
return len(word), points, ends
}
func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line string) (*ctx.Context, string, []string) {
// }}}
func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line string) (*ctx.Context, string, []string) { // {{{
level := m.Capi("level", 1)
yac.Log("debug", nil, fmt.Sprintf("%s\\%d %s(%d):", m.Cap("label")[0:level], level, yac.word[page], page))
@ -252,7 +256,9 @@ func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line str
return cli, line, word
}
func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
// }}}
func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{}
@ -261,7 +267,8 @@ func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s
}
func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server {
// }}}
func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
if yac.Message = m; yac.Context == Index {
Pulse = m
}
@ -299,12 +306,14 @@ func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server {
return yac
}
func (yac *YAC) Start(m *ctx.Message, arg ...string) bool {
// }}}
func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // {{{
yac.Message = m
return false
}
func (yac *YAC) Close(m *ctx.Message, arg ...string) bool {
// }}}
func (yac *YAC) Close(m *ctx.Message, arg ...string) bool { // {{{
switch yac.Context {
case m.Target():
case m.Source():
@ -312,13 +321,15 @@ func (yac *YAC) Close(m *ctx.Message, arg ...string) bool {
return true
}
// }}}
var Pulse *ctx.Message
var Index = &ctx.Context{Name: "yac", Help: "语法中心",
Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{
"train": &ctx.Command{Name: "train page hash word...", Help: "添加语法规则, page: 语法集合, hash: 语句类型, word: 语法模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
page, ok := yac.page[arg[0]]
if !ok {
page = m.Capi("npage", 1)
@ -352,9 +363,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
yac.seed = append(yac.seed, &Seed{page, hash, arg[2:]})
yac.Cap("stream", fmt.Sprintf("%d,%s,%s", yac.Capi("nseed", 1), yac.Cap("npage"), yac.Cap("nhash")))
}
// }}}
}},
"parse": &ctx.Command{Name: "parse page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
page, ok := yac.page[arg[0]]
m.Assert(ok)
void, ok := yac.page[arg[1]]
@ -366,9 +378,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
m.Result(0, rest, word)
}
}
// }}}
}},
"info": &ctx.Command{Name: "info", Help: "显示缓存", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
for i, v := range yac.seed {
m.Echo("seed: %d %v\n", i, v)
}
@ -389,9 +402,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
}
}
}
// }}}
}},
"check": &ctx.Command{Name: "check page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
set := map[*State]bool{}
nreal := 0
for _, v := range yac.state {
@ -414,6 +428,7 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
}
m.Log("fuck", nil, "node: %d real: %d", nnode, nreal)
}
// }}}
}},
},
}