From 4084e0ef74e27941c25c066fbaaa564ac50aac7f Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 19 Dec 2017 20:53:30 +0800 Subject: [PATCH 1/2] mac pro code --- etc/init.sh | 19 ++++++++----- src/context/aaa/aaa.go | 51 +++++++++++------------------------ src/context/cli/cli.go | 23 ++++++---------- src/context/ctx.go | 39 ++++++++++----------------- src/context/ssh/ssh.go | 60 ++++++++++++++++-------------------------- 5 files changed, 74 insertions(+), 118 deletions(-) diff --git a/etc/init.sh b/etc/init.sh index e2ca999f..74948021 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -1,19 +1,26 @@ ~cli @lex lex - +# ~cli +# remote slaver listen ":9393" tcp ~aaa login root root - +# ~ssh +# listen :9191 ~tcp listen :9393 +~web + listen # ~tcp dial ":9393" -# ~cli remote slaver listen ":9393" tcp # @debug on - -# ~mdb open chat chat "chat:chat@/chat" mysql +# ~aaa +# login shy shy +# ~mdb +# open chat:chat@/chat mysql # ~web listen -# ~ssh listen :9898 +# @debug on +# ~nfs +# open hi.txt return diff --git a/src/context/aaa/aaa.go b/src/context/aaa/aaa.go index ba9f5a7b..114b37d2 100644 --- a/src/context/aaa/aaa.go +++ b/src/context/aaa/aaa.go @@ -1,6 +1,6 @@ -package aaa // {{{ -// }}} -import ( // {{{ +package aaa + +import ( "context" "crypto/md5" @@ -12,22 +12,18 @@ import ( // {{{ "time" ) -// }}} - type AAA struct { sessions map[string]*ctx.Context *ctx.Context } -func (aaa *AAA) session(meta string) string { // {{{ +func (aaa *AAA) session(meta string) string { bs := md5.Sum([]byte(fmt.Sprintln("%d%d%s", time.Now().Unix(), rand.Int(), meta))) sessid := hex.EncodeToString(bs[:]) return sessid } -// }}} - -func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{} c.Configs = map[string]*ctx.Config{} @@ -36,32 +32,29 @@ func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -// }}} -func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { aaa.Caches["group"] = &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 { - if len(arg) > 0 { // {{{ + 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(Pulse.Confi("expire")))) return hex.EncodeToString(bs[:]) } return x.Value - // }}} }} aaa.Caches["sessid"] = &ctx.Cache{Name: "会话标识", Value: "", Help: "用户的会话标识"} aaa.Caches["expire"] = &ctx.Cache{Name: "会话超时", Value: "", Help: "用户的会话标识"} aaa.Caches["time"] = &ctx.Cache{Name: "登录时间", Value: fmt.Sprintf("%d", time.Now().Unix()), Help: "用户登录时间", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string { - if len(arg) > 0 { // {{{ + if len(arg) > 0 { return arg[0] } n, e := strconv.Atoi(x.Value) m.Assert(e) return time.Unix(int64(n), 0).Format("15:03:04") - // }}} }} if m.Target == Index { @@ -72,8 +65,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ return aaa } -// }}} -func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{ +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])) @@ -87,25 +79,16 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{ return false } -// }}} -func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool { switch aaa.Context { case m.Target: - if aaa.Context == Index { - return false - } - - if len(aaa.Context.Requests) == 0 { - m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username")) - } + m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username")) case m.Source: } return true } -// }}} - var Pulse *ctx.Message var Index = &ctx.Context{Name: "aaa", Help: "认证中心", Caches: map[string]*ctx.Cache{ @@ -116,8 +99,8 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", "expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"}, }, 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) string { - m.Target, m.Master = c, c // {{{ + "login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Target, m.Master = c, c aaa := c.Server.(*AAA) switch len(arg) { @@ -130,7 +113,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", if s, ok := aaa.sessions[arg[0]]; ok { if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() { s.Close(m) - return "" + return } m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target @@ -139,7 +122,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", c.Requests = append(c.Requests, m) m.Index = len(m.Target.Requests) } - return m.Cap("username") + m.Echo(m.Cap("username")) } case 2, 3: group, username, password := arg[0], arg[0], arg[1] @@ -163,10 +146,8 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", m.Cap("password", password) m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target aaa.sessions[m.Cap("sessid")] = m.Target - return m.Cap("sessid") + m.Echo(m.Cap("sessid")) } - return "" - // }}} }}, }, Index: map[string]*ctx.Context{ diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index b4695538..8c0f5664 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -386,7 +386,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", }, Configs: map[string]*ctx.Config{}, Commands: map[string]*ctx.Command{ - "source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { cli := c.Server.(*CLI) // {{{ switch len(arg) { case 1: @@ -395,16 +395,14 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", cli.push(f) } - return "" // }}} }}, - "return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { cli := c.Server.(*CLI) // {{{ cli.bio.Discard(cli.bio.Buffered()) - return "" // }}} }}, - "alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { cli := c.Server.(*CLI) // {{{ switch len(arg) { case 0: @@ -423,12 +421,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", cli.alias[arg[0]] = strings.Join(arg[1:], " ") m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]]) } - return "" // }}} }}, "remote": &ctx.Command{Name: "remote [send args...]|[[master|slaver] listen|dial address protocol]", Help: "建立远程连接", Formats: map[string]int{"send": -1, "master": 0, "slaver": 0, "listen": 1, "dial": 1}, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { if m.Has("send") { // {{{ cli := m.Target.Server.(*CLI) @@ -441,7 +438,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", cli.bufs = cli.bufs[0:0] m.Echo("\n~~~remote~~~\n") - return "" + return } action := "dial" @@ -456,19 +453,17 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } msg.Cmd(action, m.Get(action)) - return "" }}, // }}} "open": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接", Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"}, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { m.Start(fmt.Sprintf("PTS%d", m.Capi("nterm")), "管理终端", "void.sh") // {{{ - return "" // }}} }}, "master": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接", Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"}, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { cli, ok := c.Server.(*CLI) // {{{ m.Assert(ok, "模块类型错误") m.Assert(m.Target != c, "模块是主控模块") @@ -476,15 +471,13 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", msg := m.Spawn(c) msg.Start(fmt.Sprintf("PTS%d", cli.Capi("nterm")), arg[0], arg[1:]...) m.Target.Master = msg.Target - return "" // }}} }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{Name: "void", Commands: map[string]*ctx.Command{ - "context": &ctx.Command{}, - "open": &ctx.Command{}, + "open": &ctx.Command{}, }, }, }, diff --git a/src/context/ctx.go b/src/context/ctx.go index 0b14dca8..b7d66fc2 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -37,7 +37,7 @@ type Command struct { Formats map[string]int Options map[string]string Appends map[string]string - Hand func(m *Message, c *Context, key string, arg ...string) string + Hand func(m *Message, c *Context, key string, arg ...string) } type Server interface { @@ -120,7 +120,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{ c.Owner = m.Master.Owner c.Group = m.Master.Group - m.Log("begin", nil, "%d context %v", m.Capi("ncontext", 1), m.Meta["detail"]) + m.Log("begin", nil, "%d context %v", m.root.Capi("ncontext", 1), m.Meta["detail"]) for k, x := range c.Configs { if x.Hand != nil { m.Conf(k, x.Value) @@ -139,7 +139,7 @@ func (c *Context) Start(m *Message) bool { // {{{ if c.Requests = append(c.Requests, m); m.Cap("status") != "start" { running := make(chan bool) go m.AssertOne(m, true, func(m *Message) { - m.Log(m.Cap("status", "start"), nil, "%d server %v", m.Capi("nserver", 1), m.Meta["detail"]) + m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"]) if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) { c.Close(m, m.Meta["detail"]...) @@ -828,7 +828,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{ for s := c; s != nil; s = s.context { m.Master = m.Source - if x, ok := s.Commands[key]; ok && m.Check(s, "commands", key) && x.Hand != nil { + if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) { m.AssertOne(m, true, func(m *Message) { m.Log("cmd", s, "%s %v", key, arg) @@ -863,11 +863,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{ arg = m.Meta["args"] } - m.Set("result") - m.Set("append") - if ret := x.Hand(m, s, key, arg...); ret != "" { - m.Echo(ret) - } + x.Hand(m.Set("result").Set("append"), s, key, arg...) if x.Appends != nil { for _, v := range m.Meta["append"] { @@ -1007,7 +1003,7 @@ func (m *Message) Conf(key string, arg ...string) string { // {{{ return m.Conf(key, arg[1]) } - m.Assert(true, "配置项操作错误") + m.Assert(false, key+"配置项操作错误") return "" } @@ -1066,7 +1062,7 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{ return m.Cap(key, arg[1]) } - m.Assert(true, "缓存项操作错误") + m.Assert(false, key+"缓存项操作错误") return "" } @@ -1131,7 +1127,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Commands: map[string]*Command{ "userinfo": &Command{Name: "userinfo [add|del [context key name help]|[command|config|cache group name]]", Help: "查看模块的用户信息", Formats: map[string]int{"add": -1, "del": -1}, - Hand: func(m *Message, c *Context, key string, arg ...string) string { + Hand: func(m *Message, c *Context, key string, arg ...string) { switch { // {{{ case m.Has("add"): m.Target.Add(m.Source.Group, m.Meta["add"]...) @@ -1164,10 +1160,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心", } } } - return "" // }}} }}, - "server": &Command{Name: "server [start|exit|switch][args]", Help: "服务启动停止切换", Hand: func(m *Message, c *Context, key string, arg ...string) string { + "server": &Command{Name: "server [start|exit|switch][args]", Help: "服务启动停止切换", Hand: func(m *Message, c *Context, key string, arg ...string) { switch len(arg) { // {{{ case 0: m.Travel(m.Target.root, func(m *Message) bool { @@ -1187,10 +1182,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心", case "switch": } } - return "" // }}} }}, - "message": &Command{Name: "message code", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) string { + "message": &Command{Name: "message code", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) { switch len(arg) { // {{{ case 0: m.Echo("\033[31mrequests:\033[0m\n") @@ -1254,12 +1248,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心", } } - return "" // }}} }}, "context": &Command{Name: "context [root] [[find|search] name] [list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数", Formats: map[string]int{"root": 0, "back": 0, "home": 0, "find": 1, "search": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0}, - Hand: func(m *Message, c *Context, key string, arg ...string) string { + Hand: func(m *Message, c *Context, key string, arg ...string) { root := true || m.Has("root") // {{{ ms := []*Message{} @@ -1348,12 +1341,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心", v.Set("detail", arg...).Cmd() } } - return "" // }}} }}, "command": &Command{Name: "command [all] [key [name help]]", Help: "查看或修改命令", Formats: map[string]int{"all": 0, "delete": 0, "void": 0}, - Hand: func(m *Message, c *Context, key string, arg ...string) string { + Hand: func(m *Message, c *Context, key string, arg ...string) { all := m.Has("all") // {{{ switch len(arg) { @@ -1428,12 +1420,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心", } } } - return "" // }}} }}, "config": &Command{Name: "config [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加配置", Formats: map[string]int{"all": 0, "delete": 0, "void": 0}, - Hand: func(m *Message, c *Context, key string, arg ...string) string { + Hand: func(m *Message, c *Context, key string, arg ...string) { all := m.Has("all") // {{{ switch len(arg) { @@ -1484,12 +1475,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心", case 4: m.Conf(arg[0], arg[1:]...) } - return "" // }}} }}, "cache": &Command{Name: "cache [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加缓存", Formats: map[string]int{"all": 0, "delete": 0, "void": 0}, - Hand: func(m *Message, c *Context, key string, arg ...string) string { + Hand: func(m *Message, c *Context, key string, arg ...string) { all := m.Has("all") // {{{ switch len(arg) { @@ -1540,7 +1530,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心", case 4: m.Cap(arg[0], arg[1:]...) } - return "" // }}} }}, }, diff --git a/src/context/ssh/ssh.go b/src/context/ssh/ssh.go index 8d1666a3..f28e59b2 100644 --- a/src/context/ssh/ssh.go +++ b/src/context/ssh/ssh.go @@ -1,6 +1,6 @@ -package ssh // {{{ -// }}} -import ( // {{{ +package ssh + +import ( "bufio" "context" "fmt" @@ -9,8 +9,6 @@ import ( // {{{ "strings" ) -// }}} - type SSH struct { send map[string]*ctx.Message *bufio.Reader @@ -18,37 +16,32 @@ type SSH struct { *ctx.Context } -func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{ "nsend": &ctx.Cache{Name: "nsend", Value: "0", Help: "消息发送数量"}, } c.Configs = map[string]*ctx.Config{} - c.Commands = map[string]*ctx.Command{} s := new(SSH) s.Context = c - s.send = make(map[string]*ctx.Message) return s } -// }}} -func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { if ssh.Context == Index { Pulse = m } return ssh } -// }}} -func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { ssh.Owner = nil ssh.Conn = m.Data["io"].(net.Conn) ssh.Reader = bufio.NewReader(ssh.Conn) m.Log("info", nil, "%d remote %v", 0, ssh.Conn.RemoteAddr()) - target := m.Target - msg := m.Spawn(target) + target, msg := m.Target, m.Spawn(m.Target) for { line, e := ssh.Reader.ReadString('\n') @@ -58,6 +51,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{ if msg.Has("detail") { msg.Log("info", nil, "remote: %v", msg.Meta["detail"]) msg.Log("info", nil, "remote: %v", msg.Meta["option"]) + msg.Cmd(msg.Meta["detail"]...) target = msg.Target @@ -78,6 +72,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{ } else if msg.Has("result") { msg.Log("info", nil, "remote: %v", msg.Meta["result"]) msg.Log("info", nil, "remote: %v", msg.Meta["append"]) + send := ssh.send[msg.Get("nsend")] send.Meta = msg.Meta send.Recv <- true @@ -95,13 +90,14 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{ return false } -// }}} -func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{ - return false +func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { + switch ssh.Context { + case m.Target: + case m.Source: + } + return true } -// }}} - var Pulse *ctx.Message var Index = &ctx.Context{Name: "ssh", Help: "集群中心", Caches: map[string]*ctx.Cache{ @@ -109,25 +105,17 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", }, Configs: map[string]*ctx.Config{}, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - tcp := m.Find("tcp", true) // {{{ - tcp.Cmd(m.Meta["detail"]...) - return "" - // }}} + "listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Find("tcp", true).Cmd(m.Meta["detail"]...) }}, - "dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - tcp := m.Find("tcp", true) // {{{ - tcp.Cmd(m.Meta["detail"]...) - return "" - // }}} + "dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Find("tcp", true).Cmd(m.Meta["detail"]...) }}, - "open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - m.Start("host"+Pulse.Cap("nhost"), "主机连接") // {{{ - return "" - // }}} + "open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Start(fmt.Sprintf("host%s", Pulse.Capi("nhost", 1)), "主机连接") }}, - "remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - ssh, ok := m.Target.Server.(*SSH) // {{{ + "remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + ssh, ok := m.Target.Server.(*SSH) m.Assert(ok) m.Capi("nsend", 1) m.Recv = make(chan bool) @@ -144,8 +132,6 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", } fmt.Fprintf(ssh.Conn, "\n") <-m.Recv - return "" - // }}} }}, }, } From dbf50a1cd4010a65225db30b8d38225b69e8136e Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 19 Dec 2017 20:54:35 +0800 Subject: [PATCH 2/2] mac pro code --- src/context/mdb/mdb.go | 68 +++++++++----------- src/context/nfs/nfs.go | 101 ++++++++++++----------------- src/context/tcp/tcp.go | 141 ++++++++++++----------------------------- src/context/web/web.go | 102 ++++++++++------------------- 4 files changed, 144 insertions(+), 268 deletions(-) diff --git a/src/context/mdb/mdb.go b/src/context/mdb/mdb.go index 9cfd4c31..3ff097d7 100644 --- a/src/context/mdb/mdb.go +++ b/src/context/mdb/mdb.go @@ -1,6 +1,6 @@ -package mdb // {{{ -// }}} -import ( // {{{ +package mdb + +import ( "context" "database/sql" @@ -9,14 +9,12 @@ import ( // {{{ "fmt" ) -// }}} - type MDB struct { *sql.DB *ctx.Context } -func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{ "source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"}, "driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"}, @@ -28,56 +26,48 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -// }}} -func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { + if mdb.Context == Index { + Pulse = m + } return mdb } -// }}} -func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 { m.Cap("source", arg[0]) } if len(arg) > 1 { m.Cap("driver", arg[1]) } else { - m.Cap("driver", m.Conf("driver")) + m.Cap("driver", Pulse.Conf("driver")) } - if m.Cap("source") == "" || m.Cap("driver") == "" { return false } - m.Cap("stream", m.Cap("source")) db, e := sql.Open(m.Cap("driver"), m.Cap("source")) m.Assert(e) mdb.DB = db - m.Log("info", nil, "%d open %s %s", m.Capi("nsource", 1), m.Cap("driver"), m.Cap("source")) + m.Log("info", nil, "%d open %s %s", Pulse.Capi("nsource"), m.Cap("driver"), m.Cap("stream", m.Cap("source"))) return false } -// }}} -func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { switch mdb.Context { case m.Target: - if mdb.Context == Index { - return false - } - if mdb.DB != nil { - m.Log("info", nil, "%d close %s %s", m.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 = nil } case m.Source: } - return true } -// }}} - +var Pulse *ctx.Message var Index = &ctx.Context{Name: "mdb", Help: "数据中心", Caches: map[string]*ctx.Cache{ "nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"}, @@ -86,18 +76,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", "driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"}, }, Commands: map[string]*ctx.Command{ - "open": &ctx.Command{Name: "open name help source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - m.Assert(len(arg) > 2, "缺少参数") // {{{ - m.Master, m.Target = c, c - m.Cap("stream", m.Cap("nsource")) - m.Start(arg[0], "数据存储", arg[2:]...) - return "" - // }}} + "open": &ctx.Command{Name: "open source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Assert(len(arg) > 0, "缺少参数") + m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...) + Pulse.Cap("stream", Pulse.Cap("nsource")) }}, "exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库", Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"}, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mdb, ok := m.Target.Server.(*MDB) // {{{ + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + mdb, ok := m.Target.Server.(*MDB) m.Assert(ok, "目标模块类型错误") m.Assert(len(arg) > 0, "缺少参数") m.Assert(mdb.DB != nil, "数据库未打开") @@ -118,11 +105,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", m.Add("append", "LastInsertId", fmt.Sprintf("%d", id)) m.Add("append", "RowsAffected", fmt.Sprintf("%d", n)) m.Log("info", nil, "last(%d) rows(%d)", id, n) - return "" - // }}} }}, - "query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mdb, ok := m.Target.Server.(*MDB) // {{{ + "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) m.Assert(ok, "目标模块类型错误") m.Assert(len(arg) > 0, "缺少参数") m.Assert(mdb.DB != nil, "数据库未打开") @@ -161,10 +146,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心", } m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"])) - return "" - // }}} }}, }, + Index: map[string]*ctx.Context{ + "void": &ctx.Context{Name: "void", + Commands: map[string]*ctx.Command{ + "open": &ctx.Command{}, + }, + }, + }, } func init() { diff --git a/src/context/nfs/nfs.go b/src/context/nfs/nfs.go index 02cd72b6..9b2e2201 100644 --- a/src/context/nfs/nfs.go +++ b/src/context/nfs/nfs.go @@ -1,6 +1,6 @@ -package nfs // {{{ -// }}} -import ( // {{{ +package nfs + +import ( "context" "fmt" @@ -9,69 +9,59 @@ import ( // {{{ "strconv" ) -// }}} - type NFS struct { file *os.File *ctx.Context } -func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ - info, ok := m.Data["info"].(os.FileInfo) - m.Assert(ok) +func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { + file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) + m.Assert(e) + info, e := os.Stat(arg[0]) + m.Assert(e) c.Caches = map[string]*ctx.Cache{ "name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"}, - "mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件名"}, - "time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "文件名"}, - "size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件名"}, - "pos": &ctx.Cache{Name: "pos", Value: "0", Help: "文件名"}, + "mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"}, + "time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "创建时间"}, + "size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"}, + "pos": &ctx.Cache{Name: "pos", Value: "0", Help: "读写位置"}, } c.Configs = map[string]*ctx.Config{} s := new(NFS) s.Context = c + s.file = file return s } -// }}} -func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ +func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { if nfs.Context == Index { Pulse = m } return nfs - } -// }}} -func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{ - m.Log("info", nil, "%d open %s", Pulse.Capi("nfile", 1), m.Cap("name")) +func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { + m.Log("info", nil, "%d open %s", Pulse.Capi("nfile"), m.Cap("name")) m.Cap("stream", m.Cap("name")) return false } -// }}} -func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { switch nfs.Context { case m.Target: - if nfs.Context == Index { - return false - } - if nfs.file != nil { m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name")) nfs.file.Close() - return true + nfs.file = nil } case m.Source: } - return true } -// }}} - var Pulse *ctx.Message var Index = &ctx.Context{Name: "nfs", Help: "存储中心", Caches: map[string]*ctx.Cache{ @@ -81,22 +71,11 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", "size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"}, }, 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) string { - file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) // {{{ - m.Assert(e) - info, e := os.Stat(arg[0]) - m.Assert(e) - m.Put("option", "info", info).Start("file"+m.Cap("nfile"), "打开文件", arg...) - - nfs, ok := m.Target.Server.(*NFS) - m.Assert(ok) - - nfs.file = file - return "" - // }}} + "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...) }}, - "read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - nfs, ok := m.Target.Server.(*NFS) // {{{ + "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) m.Assert(ok) var e error @@ -105,45 +84,47 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", n, e = strconv.Atoi(arg[0]) m.Assert(e) } - - buf := make([]byte, n) if len(arg) > 1 { m.Cap("pos", arg[1]) } + buf := make([]byte, n) if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF { m.Assert(e) } + m.Echo(string(buf)) if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") { m.Cap("pos", "0") } - - return string(buf) - // }}} }}, - "write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - nfs, ok := m.Target.Server.(*NFS) // {{{ + "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) if m.Assert(ok); len(arg) > 1 { m.Cap("pos", arg[1]) } if len(arg[0]) == 0 { - e := nfs.file.Truncate(0) - m.Assert(e) - m.Cap("size", "0") + m.Assert(nfs.file.Truncate(int64(m.Capi("pos")))) + m.Cap("size", m.Cap("pos")) m.Cap("pos", "0") - return "" + } else { + n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) + if m.Assert(e) && m.Capi("pos", n) > m.Capi("size") { + m.Cap("size", m.Cap("pos")) + } } - n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) - if m.Assert(e); m.Capi("pos", n) > m.Capi("size") { - m.Cap("size", m.Cap("pos")) - } - return m.Cap("pos") - // }}} + m.Echo(m.Cap("pos")) }}, }, + Index: map[string]*ctx.Context{ + "void": &ctx.Context{Name: "void", + Commands: map[string]*ctx.Command{ + "open": &ctx.Command{}, + }, + }, + }, } func init() { diff --git a/src/context/tcp/tcp.go b/src/context/tcp/tcp.go index b8162a1d..c6b7d118 100644 --- a/src/context/tcp/tcp.go +++ b/src/context/tcp/tcp.go @@ -1,6 +1,6 @@ -package tcp // {{{ -// }}} -import ( // {{{ +package tcp + +import ( "context" "fmt" @@ -8,15 +8,13 @@ import ( // {{{ "strconv" ) -// }}} - type TCP struct { net.Conn net.Listener *ctx.Context } -func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ +func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { c.Caches = map[string]*ctx.Cache{ "protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"}, "security": &ctx.Cache{Name: "security(true/false)", Value: m.Conf("security"), Help: "加密通信"}, @@ -27,20 +25,17 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server s := new(TCP) s.Context = c return s - } -// }}} -func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ - if m.Target == Index { +func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { + if tcp.Context == Index { Pulse = m } return tcp } -// }}} -func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 1 { m.Cap("address", arg[1]) } @@ -53,92 +48,64 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{ c, e := net.Dial(m.Cap("protocol"), m.Cap("address")) m.Assert(e) tcp.Conn = c - m.Log("info", nil, "dial(%d) %v->%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr()) - m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())) + m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))) - msg := m.Spawn(m.Source).Put("option", "io", c) + msg := m.Reply("open").Put("option", "io", c) msg.Cmd("open") - msg.Cap("stream", tcp.RemoteAddr().String()) - - if tcp.Sessions == nil { - tcp.Sessions = make(map[string]*ctx.Message) - } - tcp.Sessions["open"] = msg - msg.Name = "open" - - // m.Reply(c.LocalAddr().String()).Put("option", "io", c).Cmd("open") + msg.Cap("stream", tcp.LocalAddr().String()) return false case "accept": c, e := m.Data["io"].(net.Conn) m.Assert(e) tcp.Conn = c - m.Log("info", nil, "accept(%d) %v<-%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr()) - m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())) + m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))) - s, e := m.Data["source"].(*ctx.Context) - m.Assert(e) - msg := m.Spawn(s).Put("option", "io", c) + msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", c) msg.Cmd("open") msg.Cap("stream", tcp.RemoteAddr().String()) - - if tcp.Sessions == nil { - tcp.Sessions = make(map[string]*ctx.Message) - } - tcp.Sessions["open"] = msg - msg.Name = "open" - - // m.Reply(c.RemoteAddr().String()) return false } l, e := net.Listen(m.Cap("protocol"), m.Cap("address")) m.Assert(e) tcp.Listener = l - m.Log("info", nil, "listen(%d) %v", m.Capi("nlisten", 1), l.Addr()) - m.Cap("stream", fmt.Sprintf("%s", l.Addr())) + m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", l.Addr()))) for { c, e := l.Accept() m.Assert(e) - m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", m.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 } -// }}} -func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { switch tcp.Context { case m.Target: - if tcp.Context == Index { - return false - } - if tcp.Listener != nil { - m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, tcp.Listener.Addr()) + m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream")) tcp.Listener.Close() tcp.Listener = nil } + if tcp.Conn != nil { + m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, m.Cap("stream")) + tcp.Conn.Close() + tcp.Conn = nil + } case m.Source: if tcp.Conn != nil { msg := m.Spawn(tcp.Context) - msg.Master = tcp.Context - if !tcp.Context.Close(msg, arg...) { + if msg.Master = tcp.Context; !tcp.Context.Close(msg, arg...) { return false } } } - if tcp.Conn != nil { - m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, tcp.Conn.LocalAddr()) - tcp.Conn.Close() - tcp.Conn = nil - } return true } -// }}} - +var Pulse *ctx.Message var Index = &ctx.Context{Name: "tcp", Help: "网络中心", Caches: map[string]*ctx.Cache{ "nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"}, @@ -149,53 +116,29 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", "security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"}, }, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen [address [security]]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - switch len(arg) { // {{{ - case 0: - m.Travel(nil, func(m *ctx.Message) bool { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Listener != nil { - m.Echo("%s %v\n", m.Target.Name, tcp.Addr()) - } - return true - }) - default: - m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten")+1), "网络监听", m.Meta["detail"]...) - } - return "" - // }}} + "listen": &ctx.Command{Name: "listen address [security]", 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"]...) }}, - "dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - switch len(arg) { // {{{ - case 0: - m.Travel(nil, func(m *ctx.Message) bool { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { - m.Echo("%s %v<->%v\n", m.Target.Name, tcp.LocalAddr(), tcp.RemoteAddr()) - } - return true - }) - default: - m.Start(fmt.Sprintf("com%d", m.Capi("nclient")+1), "网络连接", m.Meta["detail"]...) - } - return "" - // }}} + "dial": &ctx.Command{Name: "dial [address [security]]", 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"]...) }}, - "send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{ - tcp.Conn.Write([]byte(arg[0])) - } - return "" - // }}} + "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) + m.Assert(ok && tcp.Conn != nil) + + 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) 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) + m.Assert(ok && tcp.Conn != nil) + size, e := strconv.Atoi(arg[0]) m.Assert(e) - if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{ - buf := make([]byte, size) - tcp.Conn.Read(buf) - return string(buf) - } - return "" - // }}} + + buf := make([]byte, size) + tcp.Conn.Read(buf) + m.Echo(string(buf)) }}, }, Index: map[string]*ctx.Context{ @@ -208,8 +151,6 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", }, } -var Pulse *ctx.Message - func init() { tcp := &TCP{} tcp.Context = Index diff --git a/src/context/web/web.go b/src/context/web/web.go index e7c6453c..d4fa7171 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -1,6 +1,6 @@ -package web // {{{ -// }}} -import ( // {{{ +package web + +import ( "context" "html/template" @@ -13,12 +13,10 @@ import ( // {{{ "strings" ) -// }}} - type MUX interface { Handle(string, http.Handler) HandleFunc(string, func(http.ResponseWriter, *http.Request)) - Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string) string) + Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string)) } type WEB struct { @@ -29,10 +27,9 @@ type WEB struct { *ctx.Context } -func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...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) { - msg := m.Spawn(m.Target) - msg.Set("detail", key) + msg := m.Spawn(m.Target).Set("detail", key) for k, v := range r.Form { msg.Add("option", k) msg.Meta[k] = v @@ -44,16 +41,11 @@ 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.Put("option", "request", r) msg.Put("option", "response", w) - - ret := hand(msg, msg.Target, key) - if ret != "" { - msg.Echo(ret) - } + hand(msg, msg.Target, key) header := w.Header() for _, k := range msg.Meta["append"] { - ce := &http.Cookie{Name: k, Value: msg.Get(k)} - header.Add("Set-Cookie", ce.String()) + header.Add("Set-Cookie", (&http.Cookie{Name: k, Value: msg.Get(k)}).String()) } for _, v := range msg.Meta["result"] { w.Write([]byte(v)) @@ -61,12 +53,10 @@ 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 { log.Println() web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL) - defer log.Println() if web.Cap("logheaders") == "yes" { for k, v := range r.Header { @@ -75,8 +65,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ log.Println() } - r.ParseForm() - if len(r.PostForm) > 0 { + if r.ParseForm(); len(r.PostForm) > 0 { for k, v := range r.PostForm { log.Printf("%s: %s", k, v[0]) } @@ -86,21 +75,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ web.ServeMux.ServeHTTP(w, r) - if web.Message != nil { - if web.Cap("logheaders") == "yes" { - for k, v := range w.Header() { - log.Println(k+":", v[0]) - } + if web.Message != nil && web.Cap("logheaders") == "yes" { + for k, v := range w.Header() { + log.Println(k+":", v[0]) } + log.Println() } } -// }}} - -func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ - c.Caches = map[string]*ctx.Cache{ - "directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}, - } +func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { + c.Caches = map[string]*ctx.Cache{} c.Configs = map[string]*ctx.Config{} s := new(WEB) @@ -108,15 +92,14 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -// }}} -func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ - if len(arg) > 0 { - m.Cap("directory", arg[0]) - } - +func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { + web.Caches["directory"] = &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"} web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "请求路径"} web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"} web.Caches["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"} + if len(arg) > 0 { + m.Cap("directory", arg[0]) + } web.ServeMux = http.NewServeMux() if mux, ok := m.Target.Server.(MUX); ok { @@ -130,8 +113,7 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ return web } -// }}} -func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ +func (web *WEB) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 { m.Cap("directory", arg[0]) } @@ -192,32 +174,23 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{ return true } -// }}} -func (web *WEB) Close(m *ctx.Message, arg ...string) bool { // {{{ +func (web *WEB) Close(m *ctx.Message, arg ...string) bool { switch web.Context { case m.Target: case m.Source: } - return true } -// }}} - var Index = &ctx.Context{Name: "web", Help: "应用中心", - Caches: map[string]*ctx.Cache{ - "directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}, - }, + Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{}, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - m.Meta["detail"] = arg // {{{ - m.Target.Start(m) - return "" - // }}} + "listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + m.Set("detail", arg...).Target.Start(m) }}, - "route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { - mux, ok := m.Target.Server.(MUX) // {{{ + "route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + mux, ok := m.Target.Server.(MUX) m.Assert(ok, "模块类型错误") m.Assert(len(arg) == 3, "缺少参数") @@ -225,7 +198,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", case "directory": mux.Handle(arg[1], http.FileServer(http.Dir(arg[2]))) case "template": - mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { // {{{ + mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) { w := m.Data["response"].(http.ResponseWriter) if _, e := os.Stat(arg[2]); e == nil { @@ -234,16 +207,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", template.Must(template.New("temp").Parse(arg[2])).Execute(w, m) } - return "" }) - // }}} case "script": - cli := m.Find("cli", true) // {{{ + cli := m.Find("cli", true) lex := m.Find("lex", true) - mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { + mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) { f, e := os.Open(arg[2]) line, bio := "", bufio.NewReader(f) - if e != nil { line = arg[2] } @@ -260,18 +230,12 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", break } } - - return "" }) - // }}} } - - return "" - // }}} }}, - "/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { + "/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { m.Add("append", "hi", "hello") - return "hello" + m.Echo("hello\n") }}, }, }