1
0
forked from x/ContextOS

vpn mod context.Request 简化了服务管理与消息请求

This commit is contained in:
shaoying 2017-11-02 18:10:25 +08:00
parent 21408e523c
commit 2f03c83978
7 changed files with 119 additions and 119 deletions

View File

@ -1,13 +1,14 @@
# context # context
context: 通过提供动态接口,自由组合功能模块,让开发变得更简单。 context: 通过提供动态接口,自由组合功能模块,让开发变得更简单。
## 模块设计原则 ## 模块设计原则WCW(want, can, will)
* 必须让自己容易知道想做什么 * 必须让自己容易知道想做什么
* 必须让研发容易知道能做什么 * 必须让研发容易知道能做什么
* 必须让用户容易知道会做什么 * 必须让用户容易知道会做什么
## 模块设计接口 ## 模块设计接口CMOS(context, message, operation, system)
* 模块管理context * 模块管理context
* 消息管理message
* 命令管理command * 命令管理command
* 配置管理config * 配置管理config
* 缓存管理cache * 缓存管理cache

View File

@ -7,4 +7,3 @@ alias * message
~root ~root
~tcp ~tcp
&

View File

@ -222,14 +222,7 @@ func (cli *CLI) Start() bool { // {{{
} }
// }}} // }}}
func (cli *CLI) Fork(c *ctx.Context, key string) ctx.Server { // {{{ func (cli *CLI) Spawn(c *ctx.Context, arg ...string) ctx.Server { // {{{
s := new(CLI)
s.Context = c
return s
}
// }}}
func (cli *CLI) Spawn(c *ctx.Context, key string) ctx.Server { // {{{
s := new(CLI) s := new(CLI)
s.Context = c s.Context = c
return s return s
@ -430,8 +423,6 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
} }
case 2, 3, 4, 5: case 2, 3, 4, 5:
switch arg[1] { switch arg[1] {
case "fork":
msg.Target.Fork(arg[2])
case "spawn": case "spawn":
msg.Target.Spawn(arg[2]) msg.Target.Spawn(arg[2])
case "root": case "root":
@ -478,6 +469,7 @@ var Index = &ctx.Context{Name: "cli", Help: "本地控制",
} }
} }
} }
return "" return ""
// }}} // }}}
}}, }},

View File

@ -8,6 +8,7 @@ import ( // {{{
"runtime/debug" "runtime/debug"
"strconv" "strconv"
"strings" "strings"
"time"
) )
// }}} // }}}
@ -38,12 +39,17 @@ type Command struct { // {{{
// }}} // }}}
type Message struct { // {{{ type Message struct { // {{{
Code int Code int
Time time.Time
Meta map[string][]string Meta map[string][]string
Data map[string]interface{} Data map[string]interface{}
Wait chan bool Wait chan bool
Target *Context Name string
*Context *Context
Target *Context
Index int
} }
func (m *Message) Add(key string, value ...string) string { // {{{ func (m *Message) Add(key string, value ...string) string { // {{{
@ -103,7 +109,7 @@ func (m *Message) Echo(str string, arg ...interface{}) string { // {{{
// }}} // }}}
func (m *Message) End(s bool) { // {{{ func (m *Message) End(s bool) { // {{{
log.Println(m.Name, "end", m.Code, ":", m.Meta["detail"]) // log.Println(m.Name, "end", m.Code, ":", m.Meta["detail"])
if m.Wait != nil { if m.Wait != nil {
m.Wait <- s m.Wait <- s
} }
@ -115,8 +121,7 @@ func (m *Message) End(s bool) { // {{{
type Server interface { // {{{ type Server interface { // {{{
Begin() bool Begin() bool
Start() bool Start() bool
Spawn(c *Context, key string) Server Spawn(c *Context, arg ...string) Server
Fork(c *Context, key string) Server
} }
// }}} // }}}
@ -135,9 +140,11 @@ type Context struct { // {{{
Context *Context Context *Context
Contexts map[string]*Context Contexts map[string]*Context
Auth string
Index map[string]*Context Index map[string]*Context
Shadows map[string]*Context Shadows map[string]*Context
Session map[string]*Message
Resource []*Message
} }
func (c *Context) Check(e error) bool { // {{{ func (c *Context) Check(e error) bool { // {{{
@ -152,6 +159,37 @@ func (c *Context) Check(e error) bool { // {{{
} }
// }}} // }}}
func (c *Context) Request(arg ...string) bool { // {{{
if c.Session == nil {
c.Session = make(map[string]*Message)
}
m := &Message{
Code: c.Capi("nmessage", 1),
Time: time.Now(),
Meta: map[string][]string{},
Data: map[string]interface{}{},
Name: arg[0],
}
m.Context = c
return true
}
// }}}
func (c *Context) Release(key string) bool { // {{{
return true
}
// }}}
func (c *Context) Destroy(index int) bool { // {{{
return true
}
// }}}
func (c *Context) Add(arg ...string) { // {{{ func (c *Context) Add(arg ...string) { // {{{
switch arg[0] { switch arg[0] {
case "context": case "context":
@ -421,7 +459,6 @@ func (c *Context) Init(arg ...string) { // {{{
// }}} // }}}
func (c *Context) Begin() bool { // {{{ func (c *Context) Begin() bool { // {{{
log.Println(c.Name, "init:")
for k, v := range c.Configs { for k, v := range c.Configs {
c.Conf(k, v.Value) c.Conf(k, v.Value)
} }
@ -445,46 +482,19 @@ func (c *Context) Start() bool { // {{{
log.Println(c.Name, "start:") log.Println(c.Name, "start:")
c.Server.Start() c.Server.Start()
log.Println(c.Name, "stop:")
} }
return true return true
} }
// }}} // }}}
func (c *Context) Fork(key string) *Context { // {{{ func (c *Context) Spawn(arg ...string) *Context { // {{{
s := new(Context) s := &Context{Name: arg[0], Help: c.Help}
s.Name = c.Name + key c.Register(s, c.Server.Spawn(s, arg...))
s.Help = c.Help
s.Caches = c.Caches
s.Configs = c.Configs
s.Commands = c.Commands
log.Println(c.Name, "fork:", s.Name)
if c.Messages != nil {
s.Messages = make(chan *Message, len(c.Messages))
}
c.Context.Register(s, c.Server.Fork(s, key))
s.Begin() s.Begin()
return s
}
// }}}
func (c *Context) Spawn(key string) *Context { // {{{
s := new(Context)
s.Name = c.Name + key
s.Help = c.Help
s.Caches = make(map[string]*Cache)
s.Configs = make(map[string]*Config)
s.Commands = make(map[string]*Command)
log.Println(c.Name, "spawn:", s.Name) log.Println(c.Name, "spawn:", s.Name)
if c.Messages != nil {
s.Messages = make(chan *Message, len(c.Messages))
}
c.Register(s, c.Server.Spawn(s, key))
s.Begin()
return s return s
} }
@ -496,7 +506,7 @@ func (c *Context) Get() *Message { // {{{
select { select {
case msg := <-c.Messages: case msg := <-c.Messages:
log.Println(c.Name, "get", msg.Code, ":", msg.Meta["detail"]) // log.Println(c.Name, "get", msg.Code, ":", msg.Meta["detail"])
return msg return msg
} }
@ -510,8 +520,8 @@ func (c *Context) Post(m *Message) bool { // {{{
} }
m.Code = c.Root.Capi("nmessage", 1) m.Code = c.Root.Capi("nmessage", 1)
log.Println(c.Name, "post", m.Code, ":", m.Meta["detail"]) // log.Println(c.Name, "post", m.Code, ":", m.Meta["detail"])
defer log.Println(c.Name, "done", m.Code, ":", m.Meta["detail"]) // defer log.Println(c.Name, "done", m.Code, ":", m.Meta["detail"])
c.Messages <- m c.Messages <- m
if m.Wait != nil { if m.Wait != nil {
@ -592,7 +602,7 @@ func (c *Context) Cap(arg ...string) string { // {{{
if v.Hand != nil { if v.Hand != nil {
v.Value = v.Hand(c, v.Value) v.Value = v.Hand(c, v.Value)
} }
log.Println(c.Name, "cache:", arg, v.Value) // log.Println(c.Name, "cache:", arg, v.Value)
return v.Value return v.Value
} }
@ -605,7 +615,7 @@ func (c *Context) Cap(arg ...string) string { // {{{
if v.Hand != nil { if v.Hand != nil {
v.Value = v.Hand(c, v.Value) v.Value = v.Hand(c, v.Value)
} }
log.Println(c.Name, "cache:", arg) // log.Println(c.Name, "cache:", arg)
return v.Value return v.Value
} }
@ -632,6 +642,7 @@ func (c *Context) Capi(key string, value int) int { // {{{
n, e := strconv.Atoi(c.Cap(key)) n, e := strconv.Atoi(c.Cap(key))
c.Check(e) c.Check(e)
c.Cap(key, strconv.Itoa(n+value)) c.Cap(key, strconv.Itoa(n+value))
// log.Println(c.Name, "cache:", n+value)
return n return n
} }

View File

@ -212,14 +212,7 @@ func (ssh *SSH) Start() bool { // {{{
} }
// }}} // }}}
func (ssh *SSH) Fork(c *ctx.Context, key string) ctx.Server { // {{{ func (ssh *SSH) Spawn(c *ctx.Context, arg ...string) ctx.Server { // {{{
s := new(SSH)
s.Context = c
return s
}
// }}}
func (ssh *SSH) Spawn(c *ctx.Context, key string) ctx.Server { // {{{
s := new(SSH) s := new(SSH)
s.Context = c s.Context = c
return s return s

View File

@ -2,89 +2,96 @@ package tcp
import ( import (
"context" "context"
"fmt"
"log" "log"
"net" "net"
"time"
) )
type TCP struct { type TCP struct {
listener net.Listener listener net.Listener
conn net.Conn conn []net.Conn
target *ctx.Context target *ctx.Context
*ctx.Context *ctx.Context
} }
func (tcp *TCP) Begin() bool { func (tcp *TCP) Begin() bool {
tcp.conn = make([]net.Conn, 0, 3)
return true return true
} }
func (tcp *TCP) Start() bool { func (tcp *TCP) Start() bool {
log.Println(tcp.Name, "start:") log.Println(tcp.Conf("address"))
if tcp.Conf("address") == "" { if tcp.Conf("address") == "" {
log.Println(tcp.Name, "start:")
for {
msg := tcp.Get()
arg := msg.Meta["detail"]
switch arg[0] {
case "listen":
s := tcp.Context.Spawn(arg[1])
s.Conf("addresss", "address", arg[1], "监听地址")
go s.Start()
}
if msg.Wait != nil {
msg.Wait <- true
}
msg.End(true)
}
return true return true
} else { }
if tcp.Conf("remote") == "" {
l, e := net.Listen("tcp", tcp.Conf("address")) l, e := net.Listen("tcp", tcp.Conf("address"))
tcp.Check(e) tcp.Check(e)
tcp.listener = l tcp.listener = l
tcp.Capi("nlisten", 1)
log.Println(tcp.Name, "listen:", l.Addr())
for { for {
c, e := l.Accept() c, e := l.Accept()
log.Println(tcp.Name, "accept:", c.LocalAddr(), "<-", c.RemoteAddr())
tcp.Check(e) tcp.Check(e)
tcp.conn = append(tcp.conn, c)
s := tcp.Context.Spawn(c.RemoteAddr().String()) tcp.Capi("nclient", 1)
s.Conf("remote", "remote", c.RemoteAddr().String(), "连接地址")
x := s.Server.(*TCP)
x.conn = c
go s.Start()
} }
return true return true
} else {
for {
fmt.Fprintln(tcp.conn, "hello context world!\n")
time.Sleep(3 * time.Second)
}
} }
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: "连接数量"},
} }
c.Configs = map[string]*ctx.Config{
"address": &ctx.Config{Name: "address", Value: arg[0], Help: "监听地址"},
} }
func (tcp *TCP) Spawn(c *ctx.Context, key string) ctx.Server {
s := new(TCP)
s.Context = c
return s
}
func (tcp *TCP) Fork(c *ctx.Context, key string) ctx.Server {
s := new(TCP) s := new(TCP)
s.Context = c s.Context = c
return s return s
} }
var Index = &ctx.Context{Name: "tcp", Help: "网络连接", var Index = &ctx.Context{Name: "tcp", Help: "网络连接",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{
"nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"},
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "连接数量"},
},
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"address": &ctx.Config{Name: "address", Value: "", Help: "监听地址"}, "address": &ctx.Config{Name: "address", Value: "", Help: "监听地址"},
"remote": &ctx.Config{Name: "remote", Value: "", Help: "远程地址"},
}, },
Commands: map[string]*ctx.Command{}, Commands: map[string]*ctx.Command{
"listen": &ctx.Command{"listen", "监听端口", 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())
}
case 2:
s := c.Spawn(arg[1:]...)
go s.Start()
}
return ""
}},
"dial": &ctx.Command{"dial", "建立连接", func(c *ctx.Context, m *ctx.Message, arg ...string) string {
tcp := c.Server.(*TCP)
switch len(arg) {
case 1:
for i, v := range tcp.conn {
m.Echo(tcp.Name, "conn: %s %s -> %s\n", i, v.LocalAddr(), v.RemoteAddr())
}
case 2:
conn, e := net.Dial("tcp", arg[1])
c.Check(e)
tcp.conn = append(tcp.conn, conn)
log.Println(tcp.Name, "dial:", conn.LocalAddr(), "->", conn.RemoteAddr())
}
return ""
}},
},
} }
func init() { func init() {

View File

@ -19,13 +19,10 @@ func (web *WEB) Start() bool {
http.ListenAndServe(web.Conf("address"), nil) http.ListenAndServe(web.Conf("address"), nil)
return true return true
} }
func (web *WEB) Spawn(c *ctx.Context, key string) ctx.Server { func (web *WEB) Spawn(c *ctx.Context, arg ...string) ctx.Server {
return nil return nil
} }
func (web *WEB) Fork(c *ctx.Context, key string) ctx.Server {
return nil
}
var Index = &ctx.Context{Name: "web", Help: "网页服务", var Index = &ctx.Context{Name: "web", Help: "网页服务",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},