From 5a692b1aa5f64dea43b64dba84ac8a471e5ca0f4 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sat, 4 Nov 2017 07:25:03 +0800 Subject: [PATCH] =?UTF-8?q?mac=20add=20message.spwan=20=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86start,=20spawn,=20post,=20cmd?= =?UTF-8?q?=E7=9A=84=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=9B=B4=E5=8A=A0=E5=AE=8C?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + etc/init.sh | 2 - src/context/cli/cli.go | 139 ++++++++++++--------------- src/context/ctx.go | 210 ++++++++++++++++++++++++----------------- src/context/tcp/tcp.go | 54 +++++------ src/example/bench.go | 4 +- 6 files changed, 212 insertions(+), 200 deletions(-) diff --git a/.gitignore b/.gitignore index a1338d68..fd28c36f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +bin/ +var/ +pkg/ # Binaries for programs and plugins *.exe *.dll diff --git a/etc/init.sh b/etc/init.sh index 1bda9b54..4d429893 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -4,5 +4,3 @@ alias @ config alias $ cache alias & server alias * message - -remote slave listen :9393 tcp diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index 24920313..ce478e51 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -31,7 +31,6 @@ type CLI struct { target *ctx.Context *ctx.Context - *ctx.CTX } func (cli *CLI) push(f io.ReadCloser) { // {{{ @@ -54,16 +53,13 @@ func (cli *CLI) parse() bool { // {{{ line := "" if cli.next == "" { - l, e := cli.bio.ReadString('\n') + ls, e := cli.bio.ReadString('\n') if e == io.EOF { l := len(cli.ins) if l == 1 { - if cli.Conf("slient") != "yes" { - cli.echo("\n") - cli.echo(cli.Conf("结束语")) - } - cli.echo("\n") - cli.exit <- true + cli.echo("\n%s\n", cli.Conf("结束语")) + ls = "exit" + e = nil } else { cli.ins = cli.ins[:l-1] cli.bios = cli.bios[:l-1] @@ -73,7 +69,7 @@ func (cli *CLI) parse() bool { // {{{ } } cli.Check(e) - line = l + line = ls if len(cli.ins) > 1 || cli.Conf("slient") != "yes" { cli.echo(line) @@ -129,6 +125,7 @@ func (cli *CLI) parse() bool { // {{{ } } + log.Printf("%d spawn: %s->%s %v", cli.Resource[0].Code, msg.Context.Name, msg.Target.Name, msg.Meta["detail"]) cli.Post(msg) for _, v := range msg.Meta["result"] { @@ -143,9 +140,14 @@ func (cli *CLI) deal(msg *ctx.Message) bool { // {{{ defer func() { if e := recover(); e != nil { msg.Echo("%s\n", e) - debug.PrintStack() - log.Println(e) - msg.End(false) + + if e == io.EOF { + panic(e) + } else { + debug.PrintStack() + log.Println(e) + msg.End(false) + } } }() @@ -160,8 +162,8 @@ func (cli *CLI) deal(msg *ctx.Message) bool { // {{{ if _, ok := cli.Commands[detail[0]]; ok { cli.next = cli.Cmd(msg, detail...) - } else if _, ok := cli.target.Commands[detail[0]]; ok { - cli.next = cli.target.Cmd(msg, detail...) + } else if _, ok := msg.Target.Commands[detail[0]]; ok { + cli.next = msg.Cmd() } else { cmd := exec.Command(detail[0], detail[1:]...) v, e := cmd.CombinedOutput() @@ -169,7 +171,6 @@ func (cli *CLI) deal(msg *ctx.Message) bool { // {{{ msg.Echo("%s\n", e) } msg.Echo(string(v)) - log.Println(cli.Name, "command:", detail) } msg.End(true) @@ -190,32 +191,29 @@ func (cli *CLI) echo(str string, arg ...interface{}) { // {{{ // }}} -func (cli *CLI) Begin() bool { // {{{ - if f, e := os.Open(cli.Conf("init.sh")); e == nil { - cli.push(f) - } - +func (cli *CLI) Begin() ctx.Server { // {{{ cli.history = make([]map[string]string, 0, 100) cli.alias = make(map[string]string, 10) cli.exit = make(chan bool) + cli.target = cli.Context - return true + return cli.Server } // }}} -func (cli *CLI) Start() bool { // {{{ - if msg, ok := cli.Session["remote"]; ok { - cli.push(msg.Data["result"].(io.ReadCloser)) - cli.out = msg.Data["result"].(io.WriteCloser) - cli.echo(cli.Conf("开场白")) - } - if cli.Conf("slient") != "yes" { - cli.echo("\n") - cli.echo(cli.Conf("开场白")) - cli.echo("\n") - } +func (cli *CLI) Start(m *ctx.Message) bool { // {{{ + + if detail, ok := m.Data["detail"]; ok { + io := detail.(io.ReadWriteCloser) + cli.out = io + cli.push(io) + + cli.echo("%s\n", cli.Conf("开场白")) + + if f, e := os.Open(cli.Conf("init.sh")); e == nil { + cli.push(f) + } - if cli.Conf("mode") == "local" { go func() { defer recover() for cli.parse() { @@ -231,11 +229,15 @@ func (cli *CLI) Start() bool { // {{{ // }}} func (cli *CLI) Spawn(c *ctx.Context, arg ...string) ctx.Server { // {{{ - c.Caches = map[string]*ctx.Cache{} + c.Caches = map[string]*ctx.Cache{ + "status": &ctx.Cache{Name: "status", Value: "stop", Help: "服务状态"}, + } c.Configs = map[string]*ctx.Config{ "address": &ctx.Config{Name: "address", Value: arg[0], Help: "监听地址"}, "protocol": &ctx.Config{Name: "protocol", Value: arg[1], Help: "监听协议"}, } + c.Commands = cli.Commands + c.Messages = make(chan *ctx.Message, 10) s := new(CLI) s.Context = c @@ -247,17 +249,10 @@ func (cli *CLI) Spawn(c *ctx.Context, arg ...string) ctx.Server { // {{{ var Index = &ctx.Context{Name: "cli", Help: "本地控制", Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{ - "开场白": &ctx.Config{Name: "开场白", Value: "你好,命令行", Help: "开场白"}, - "结束语": &ctx.Config{Name: "结束语", Value: "再见,命令行", Help: "结束语"}, - "mode": &ctx.Config{Name: "mode", Value: "local", Help: "命令执行模式"}, - "io": &ctx.Config{Name: "io", Value: "stdout", Help: "输入输出", Hand: func(c *ctx.Context, arg string) string { - cli := c.Server.(*CLI) // {{{ - cli.out = os.Stdout - cli.push(os.Stdin) - return arg - // }}} - }}, - "slient": &ctx.Config{Name: "slient", Value: "yes", Help: "静默启动"}, + "开场白": &ctx.Config{Name: "开场白", Value: "\n~~~ Hello Context & Message World ~~~\n", Help: "开场白"}, + "结束语": &ctx.Config{Name: "结束语", Value: "\n~~~ Byebye Context & Message World ~~~\n", Help: "结束语"}, + "slient": &ctx.Config{Name: "slient", Value: "yes", Help: "屏蔽脚本输出"}, + "PS1": &ctx.Config{Name: "PS1", Value: "etcvpn>", Help: "命令行提示符", Hand: func(c *ctx.Context, arg string) string { cli := c.Server.(*CLI) // {{{ if cli != nil && cli.target != nil { @@ -356,29 +351,13 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", return "" // }}} }}, - "message": &ctx.Command{"message detail...", "查看上下文", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { - m := ctx.Pulse - switch len(arg) { - case 1: - fmt.Println(msg.Code, msg.Time.Format("2006/01/02 15:04:05")) - case 2: - switch arg[1] { - case "home": - m = msg.Message - case "root": - } - fmt.Println(m.Code, m.Time.Format("2006/01/02 15:04:05")) - for i, v := range m.Messages { - fmt.Println(i, v.Code, v.Time.Format("2006/01/02 15:04:05")) - } - } - - return "" - msg.Meta["detail"] = arg[1:] // {{{ - if c == msg.Target { - go msg.Target.Post(msg) - } else { - msg.Target.Post(msg) + "message": &ctx.Command{"message detail...", "查看上下文", func(c *ctx.Context, m *ctx.Message, arg ...string) string { + // {{{ + ms := []*ctx.Message{ctx.Pulse} + for i := 0; i < len(ms); i++ { + m.Echo("%d %s.%s -> %s.%d\n", ms[i].Code, ms[i].Context.Name, ms[i].Name, ms[i].Target.Name, ms[i].Index) + // m.Echo("%d %s %s.%s -> %s.%d\n", ms[i].Code, ms[i].Time.Format("2006/01/02 15:03:04"), ms[i].Context.Name, ms[i].Name, ms[i].Target.Name, ms[i].Index) + ms = append(ms, ms[i].Messages...) } return "" // }}} @@ -392,7 +371,7 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", switch arg[1] { case "start": if s != nil { - go s.Start() + go s.Start(msg) } case "stop": case "switch": @@ -525,8 +504,11 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", return "" // }}} }}, + "exit": &ctx.Command{"exit", "退出", func(c *ctx.Context, m *ctx.Message, arg ...string) string { + panic(io.EOF) + }}, "remote": &ctx.Command{"remote master|slave listen|dial address protocol", "建立远程连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { - switch len(arg) { + switch len(arg) { // {{{ case 1: case 5: if arg[1] == "master" { @@ -535,23 +517,19 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", } } else { if arg[2] == "listen" { - msg := &ctx.Message{Code: c.Root.Capi("nmessage", 1), Time: time.Now()} - msg.Add("mode", "local") - msg.Target = c.Root.Find(strings.Split(arg[4], ".")) - msg.Context = m.Context - msg.Target.Cmd(msg, "listen", arg[3]) + s := c.Root.Find(strings.Split(arg[4], ".")) + m.Message.Spawn(s, arg[3], 0).Add("detail", "listen", arg[3]).Post(c) } else { } } } return "" + // }}} }}, - "accept": &ctx.Command{"accept address protocl", "建立远程连接", func(c *ctx.Context, msg *ctx.Message, arg ...string) string { - s := msg.Context.Spawn(arg[1], arg[2]) - s.Session = make(map[string]*ctx.Message) - s.Session["remote"] = msg - s.Start() + "accept": &ctx.Command{"accept address protocl", "建立远程连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { + m.Start(arg[1:]...) // {{{ return "" + // }}} }}, }, Messages: make(chan *ctx.Message, 10), @@ -559,7 +537,6 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制", func init() { cli := &CLI{} - cli.CTX = ctx.Ctx cli.Context = Index ctx.Index.Register(Index, cli) } diff --git a/src/context/ctx.go b/src/context/ctx.go index 3c8f877d..cd8ad71d 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -5,6 +5,7 @@ import ( // {{{ "fmt" "log" "os" + "regexp" "runtime/debug" "strconv" "strings" @@ -38,43 +39,50 @@ type Command struct { // {{{ // }}} type Message struct { // {{{ - Time time.Time Code int - User string + Time time.Time Meta map[string][]string Data map[string]interface{} Wait chan bool - Index int - Target *Context - - Name string *Context + Name string + Target *Context + Index int Messages []*Message Message *Message Root *Message } -func (m *Message) Spawn(c *Context, key string) *Message { // {{{ - msg := &Message{Time: time.Now(), Code: m.Capi("nmessage", 1)} +func (m *Message) Spawn(c *Context, key string, index int) *Message { // {{{ + msg := &Message{ + Code: m.Capi("nmessage", 1), + Time: time.Now(), + Target: c, + Name: key, + Message: m, + Root: m.Root, + } msg.Context = m.Target - msg.Target = c + if msg.Session == nil { + msg.Session = make(map[string]*Message) + } + msg.Session[key] = msg if m.Messages == nil { m.Messages = make([]*Message, 0, 10) } m.Messages = append(m.Messages, msg) - msg.Message = m - msg.Root = m.Root - return m + log.Printf("%d spawn %d: %s.%s->%s.%d", m.Code, msg.Code, msg.Context.Name, msg.Name, msg.Target.Name, msg.Index) + return msg } // }}} -func (m *Message) Add(key string, value ...string) string { // {{{ +func (m *Message) Add(key string, value ...string) *Message { // {{{ if m.Meta == nil { m.Meta = make(map[string][]string) } @@ -83,17 +91,17 @@ func (m *Message) Add(key string, value ...string) string { // {{{ } m.Meta[key] = append(m.Meta[key], value...) - return value[0] + return m } // }}} -func (m *Message) Put(key string, value interface{}) interface{} { // {{{ +func (m *Message) Put(key string, value interface{}) *Message { // {{{ if m.Data == nil { m.Data = make(map[string]interface{}) } m.Data[key] = value - return value + return m } // }}} @@ -116,7 +124,7 @@ func (m *Message) Get(key string) string { // {{{ } // }}} -func (m *Message) Echo(str string, arg ...interface{}) string { // {{{ +func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{ if m.Meta == nil { m.Meta = make(map[string][]string) } @@ -126,7 +134,7 @@ func (m *Message) Echo(str string, arg ...interface{}) string { // {{{ s := fmt.Sprintf(str, arg...) m.Meta["result"] = append(m.Meta["result"], s) - return s + return m } // }}} @@ -137,11 +145,38 @@ func (m *Message) End(s bool) { // {{{ m.Wait = nil } +// }}} +func (m *Message) Cmd() string { // {{{ + return m.Target.Cmd(m, m.Meta["detail"]...) +} + +// }}} +func (m *Message) Post(c *Context) bool { // {{{ + if c.Messages == nil { + c.Messages = make(chan *Message, c.Confi("MessageQueueSize")) + } + + c.Messages <- m + if m.Wait != nil { + return <-m.Wait + } + return true + +} + +// }}} +func (m *Message) Start(arg ...string) bool { // {{{ + s := m.Target.Spawn(arg...).Begin() + m.Target = s + go s.Start(m) + return true +} + // }}} // }}} type Server interface { // {{{ - Begin() bool - Start() bool + Begin() Server + Start(m *Message) bool Spawn(c *Context, arg ...string) Server } @@ -210,18 +245,6 @@ func (c *Context) Init(arg ...string) { // {{{ root = root.Context } - if len(arg) > 0 { - root.Conf("bench.log", arg[0]) - } else { - root.Conf("bench.log", root.Conf("bench.log")) - } - - if len(arg) > 1 { - root.Conf("init.sh", arg[1]) - } else { - root.Conf("init.sh", root.Conf("init.sh")) - } - cs := []*Context{root} for i := 0; i < len(cs); i++ { @@ -233,38 +256,39 @@ func (c *Context) Init(arg ...string) { // {{{ } } - Pulse.Context = Index Pulse.Root = Pulse + Pulse.Target = Index + Pulse.Context = Index - if len(arg) > 2 { - for _, v := range arg[2:] { - cs = root.Search(v) - for _, s := range cs { - go s.Start() - } + for _, s := range root.Contexts { + if ok, _ := regexp.MatchString(root.Conf("start"), s.Name); ok { + go s.Start(Pulse.Spawn(s, s.Name, 0).Put("detail", os.Stdout)) } - } else { - s := root.Find(strings.Split(root.Conf("default"), ".")) - go s.Start() - Pulse.Target = s - s.Post(Pulse) } - <-make(chan bool) + Pulse.Wait = make(chan bool) + for { + <-Pulse.Wait + if Index.Capi("nserver", 0) == 0 { + return + } + } } // }}} -func (c *Context) Find(name []string) *Context { // {{{ - if x, ok := c.Contexts[name[0]]; ok { - log.Println(c.Name, "find:", x.Name) - if len(name) == 1 { - return x - } - return x.Find(name[1:]) - } +func (c *Context) Find(name []string) (s *Context) { // {{{ + log.Println(c.Name, "find:", name) - log.Println(c.Name, "not find:", name[0]) - return nil + cs := c.Contexts + for _, v := range name { + if x, ok := cs[v]; ok { + s = x + cs = x.Contexts + continue + } + panic(errors.New("not find: " + v)) + } + return } // }}} @@ -454,32 +478,41 @@ func (c *Context) Del(arg ...string) { // {{{ // }}} -func (c *Context) Begin() bool { // {{{ +func (c *Context) Begin() *Context { // {{{ c.Root.Capi("ncontext", 1) - for k, v := range c.Configs { - c.Conf(k, v.Value) + for _, v := range c.Configs { + if v.Hand != nil { + v.Hand(c, v.Value) + } } if c.Server != nil { - return c.Server.Begin() + c.Server.Begin() } - return true + return c } // }}} -func (c *Context) Start() bool { // {{{ - defer recover() +func (c *Context) Start(m *Message) bool { // {{{ + defer func() { + if e := recover(); e != nil { + log.Println(e) + } + Pulse.Wait <- true + }() if c.Server != nil && c.Cap("status") != "start" { + c.Cap("status", "status", "start", "服务状态") defer c.Cap("status", "stop") c.Root.Capi("nserver", 1) defer c.Root.Capi("nserver", -1) - log.Println(c.Name, "start:") - c.Server.Start() - log.Println(c.Name, "stop:") + c.Resource = []*Message{m} + log.Println(m.Code, "start:", c.Name) + c.Server.Start(m) + log.Println(m.Code, "stop:", c.Name) } return true @@ -490,8 +523,6 @@ func (c *Context) Spawn(arg ...string) *Context { // {{{ s := &Context{Name: arg[0], Help: c.Help} c.Register(s, c.Server.Spawn(s, arg...)) s.Begin() - - log.Println(c.Name, "spawn:", s.Name) return s } @@ -501,10 +532,6 @@ func (c *Context) Post(m *Message) bool { // {{{ if c.Messages == nil { c.Messages = make(chan *Message, c.Confi("MessageQueueSize")) } - if c.Resource == nil { - c.Resource = make([]*Message, 0, c.Confi("MessageQueueSize")) - } - c.Resource = append(c.Resource, m) c.Messages <- m if m.Wait != nil { @@ -526,7 +553,6 @@ func (c *Context) Get() *Message { // {{{ func (c *Context) Cmd(m *Message, arg ...string) string { // {{{ if x, ok := c.Commands[arg[0]]; ok { - log.Println(c.Name, "command:", arg) return x.Hand(c, m, arg...) } @@ -615,12 +641,12 @@ func (c *Context) Cap(arg ...string) string { // {{{ return c.Context.Cap(arg...) } case 4: - if v, ok := c.Caches[arg[0]]; ok { - panic(errors.New(v.Name + "缓存项已存在")) - } - + // if v, ok := c.Caches[arg[0]]; ok { + // panic(errors.New(v.Name + "缓存项已存在")) + // } + // c.Caches[arg[0]] = &Cache{arg[1], arg[2], arg[3], nil} - log.Println(c.Name, "cache:", arg) + // log.Println(c.Name, "cache:", arg) return arg[2] default: panic(errors.New(arg[0] + "缓存项参数错误")) @@ -634,17 +660,12 @@ func (c *Context) Capi(key string, value int) int { // {{{ n, e := strconv.Atoi(c.Cap(key)) c.Check(e) c.Cap(key, strconv.Itoa(n+value)) - log.Println(c.Name, "cache:", key, n+value) - return n + return n + value } // }}} // }}} -type CTX struct{} - -var Ctx = &CTX{} - var Index = &Context{Name: "ctx", Help: "根上下文", Caches: map[string]*Cache{ "status": &Cache{Name: "status", Value: "stop", Help: "服务状态"}, @@ -663,7 +684,7 @@ var Index = &Context{Name: "ctx", Help: "根上下文", "key": &Config{Name: "key", Value: "etc/key.pem", Help: "私钥文件"}, "debug": &Config{Name: "debug", Value: "on", Help: "调试模式"}, - "default": &Config{Name: "default", Value: "cli", Help: "默认启动模块"}, + "start": &Config{Name: "start", Value: "cli", Help: "默认启动模块"}, "init.sh": &Config{Name: "init.sh", Value: "etc/init.sh", Help: "默认启动脚本"}, "bench.log": &Config{Name: "bench.log", Value: "var/bench.log", Help: "默认日志文件", Hand: func(c *Context, arg string) string { l, e := os.Create(arg) // {{{ @@ -675,10 +696,27 @@ var Index = &Context{Name: "ctx", Help: "根上下文", }, Commands: map[string]*Command{}, Session: map[string]*Message{"root": Pulse}, + Resource: []*Message{Pulse}, } -var Pulse = &Message{Time: time.Now(), Code: 0} +var Pulse = &Message{Code: 0, Time: time.Now(), Index: 0, Name: "root"} + +func init() { + if len(os.Args) > 1 { + Index.Conf("bench.log", os.Args[1]) + } else { + Index.Conf("bench.log", Index.Conf("bench.log")) + } + + if len(os.Args) > 2 { + Index.Conf("init.sh", os.Args[2]) + } + + if len(os.Args) > 3 { + Index.Conf("start", os.Args[3]) + } +} func Start() { - Index.Init(os.Args[1:]...) + Index.Init() } diff --git a/src/context/tcp/tcp.go b/src/context/tcp/tcp.go index 5a874567..51b106da 100644 --- a/src/context/tcp/tcp.go +++ b/src/context/tcp/tcp.go @@ -1,23 +1,24 @@ -package tcp - -import ( +package tcp // {{{ +// }}} +import ( // {{{ "context" "log" "net" - "time" ) +// }}} + type TCP struct { listener net.Listener *ctx.Context - *ctx.CTX } -func (tcp *TCP) Begin() bool { - return true +func (tcp *TCP) Begin() ctx.Server { // {{{ + return tcp } -func (tcp *TCP) Start() bool { +// }}} +func (tcp *TCP) Start(m *ctx.Message) bool { // {{{ if tcp.Conf("address") == "" { return true } @@ -33,18 +34,15 @@ func (tcp *TCP) Start() bool { log.Println(tcp.Name, "accept:", c.LocalAddr(), "<-", c.RemoteAddr()) tcp.Check(e) - msg := &ctx.Message{Code: tcp.Root.Capi("nmessage", 1), Time: time.Now()} - msg.Context = tcp.Resource[0].Context - msg.Index = tcp.Capi("nclient", 1) - msg.Target = tcp.Context - msg.Put("result", c) - msg.Context.Cmd(msg, "accept", c.RemoteAddr().String(), "tcp") - tcp.Resource = append(tcp.Resource, msg) + m := m.Spawn(m.Context, c.RemoteAddr().String(), 0) + m.Add("detail", "accept", c.RemoteAddr().String(), "tcp").Put("detail", c).Cmd() } + return true } -func (tcp *TCP) Spawn(c *ctx.Context, arg ...string) ctx.Server { +// }}} +func (tcp *TCP) Spawn(c *ctx.Context, arg ...string) ctx.Server { // {{{ c.Caches = map[string]*ctx.Cache{ "nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"}, } @@ -55,8 +53,11 @@ func (tcp *TCP) Spawn(c *ctx.Context, arg ...string) ctx.Server { s := new(TCP) s.Context = c return s + } +// }}} + var Index = &ctx.Context{Name: "tcp", Help: "网络连接", Caches: map[string]*ctx.Cache{ "nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"}, @@ -66,25 +67,20 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络连接", "address": &ctx.Config{Name: "address", Value: "", Help: "监听地址"}, }, Commands: map[string]*ctx.Command{ - "listen": &ctx.Command{"listen", "监听端口", func(c *ctx.Context, m *ctx.Message, arg ...string) string { - switch len(arg) { + "listen": &ctx.Command{"listen address", "监听端口", func(c *ctx.Context, m *ctx.Message, arg ...string) string { + switch len(arg) { // {{{ case 1: - for k, s := range c.Contexts { - x := s.Server.(*TCP) - m.Echo("%s %s\n", k, x.listener.Addr().String()) + for k, s := range m.Target.Contexts { + m.Echo("%s %s\n", k, s.Server.(*TCP).listener.Addr().String()) } case 2: - s := c.Spawn(arg[1:]...) - s.Resource = make([]*ctx.Message, 0, 3) - s.Resource = append(s.Resource, m) - m.Target = s - m.Index = 0 - go s.Start() + m.Start(arg[1:]...) } return "" + // }}} }}, "dial": &ctx.Command{"dial", "建立连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string { - tcp := c.Server.(*TCP) + tcp := c.Server.(*TCP) // {{{ switch len(arg) { case 1: for i, v := range tcp.Resource { @@ -97,13 +93,13 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络连接", log.Println(tcp.Name, "dial:", conn.LocalAddr(), "->", conn.RemoteAddr()) } return "" + // }}} }}, }, } func init() { tcp := &TCP{} - tcp.CTX = ctx.Ctx tcp.Context = Index ctx.Index.Register(Index, tcp) } diff --git a/src/example/bench.go b/src/example/bench.go index bddf2800..97399170 100644 --- a/src/example/bench.go +++ b/src/example/bench.go @@ -3,9 +3,9 @@ package main import ( "context" _ "context/cli" - _ "context/ssh" _ "context/tcp" - _ "context/web" + // _ "context/ssh" + // _ "context/web" ) func main() {