1
0
mirror of https://shylinux.com/x/ContextOS synced 2025-04-26 09:14:06 +08:00

mac pro code

This commit is contained in:
shaoying 2017-12-19 20:54:35 +08:00
parent a2cb522578
commit dbf50a1cd4
4 changed files with 144 additions and 268 deletions

View File

@ -1,6 +1,6 @@
package mdb // {{{ package mdb
// }}}
import ( // {{{ import (
"context" "context"
"database/sql" "database/sql"
@ -9,14 +9,12 @@ import ( // {{{
"fmt" "fmt"
) )
// }}}
type MDB struct { type MDB struct {
*sql.DB *sql.DB
*ctx.Context *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{ c.Caches = map[string]*ctx.Cache{
"source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"}, "source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"},
"driver": &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 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 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 { if len(arg) > 0 {
m.Cap("source", arg[0]) m.Cap("source", arg[0])
} }
if len(arg) > 1 { if len(arg) > 1 {
m.Cap("driver", arg[1]) m.Cap("driver", arg[1])
} else { } else {
m.Cap("driver", m.Conf("driver")) m.Cap("driver", Pulse.Conf("driver"))
} }
if m.Cap("source") == "" || m.Cap("driver") == "" { if m.Cap("source") == "" || m.Cap("driver") == "" {
return false return false
} }
m.Cap("stream", m.Cap("source"))
db, e := sql.Open(m.Cap("driver"), m.Cap("source")) db, e := sql.Open(m.Cap("driver"), m.Cap("source"))
m.Assert(e) m.Assert(e)
mdb.DB = db 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 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 { switch mdb.Context {
case m.Target: case m.Target:
if mdb.Context == Index {
return false
}
if mdb.DB != nil { 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.Close()
mdb.DB = nil mdb.DB = nil
} }
case m.Source: case m.Source:
} }
return true return true
} }
// }}} var Pulse *ctx.Message
var Index = &ctx.Context{Name: "mdb", Help: "数据中心", var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
Caches: map[string]*ctx.Cache{ Caches: map[string]*ctx.Cache{
"nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"}, "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: "数据库驱动"}, "driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"},
}, },
Commands: map[string]*ctx.Command{ 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 { "open": &ctx.Command{Name: "open source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Assert(len(arg) > 2, "缺少参数") // {{{ m.Assert(len(arg) > 0, "缺少参数")
m.Master, m.Target = c, c m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...)
m.Cap("stream", m.Cap("nsource")) Pulse.Cap("stream", Pulse.Cap("nsource"))
m.Start(arg[0], "数据存储", arg[2:]...)
return ""
// }}}
}}, }},
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库", "exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"}, Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"},
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) {
mdb, ok := m.Target.Server.(*MDB) // {{{ mdb, ok := m.Target.Server.(*MDB)
m.Assert(ok, "目标模块类型错误") m.Assert(ok, "目标模块类型错误")
m.Assert(len(arg) > 0, "缺少参数") m.Assert(len(arg) > 0, "缺少参数")
m.Assert(mdb.DB != nil, "数据库未打开") m.Assert(mdb.DB != nil, "数据库未打开")
@ -118,11 +105,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
m.Add("append", "LastInsertId", fmt.Sprintf("%d", id)) m.Add("append", "LastInsertId", fmt.Sprintf("%d", id))
m.Add("append", "RowsAffected", fmt.Sprintf("%d", n)) m.Add("append", "RowsAffected", fmt.Sprintf("%d", n))
m.Log("info", nil, "last(%d) rows(%d)", id, 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 { "query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
mdb, ok := m.Target.Server.(*MDB) // {{{ mdb, ok := m.Target.Server.(*MDB)
m.Assert(ok, "目标模块类型错误") m.Assert(ok, "目标模块类型错误")
m.Assert(len(arg) > 0, "缺少参数") m.Assert(len(arg) > 0, "缺少参数")
m.Assert(mdb.DB != nil, "数据库未打开") m.Assert(mdb.DB != nil, "数据库未打开")
@ -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"])) 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() { func init() {

View File

@ -1,6 +1,6 @@
package nfs // {{{ package nfs
// }}}
import ( // {{{ import (
"context" "context"
"fmt" "fmt"
@ -9,69 +9,59 @@ import ( // {{{
"strconv" "strconv"
) )
// }}}
type NFS struct { type NFS struct {
file *os.File file *os.File
*ctx.Context *ctx.Context
} }
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{ func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
info, ok := m.Data["info"].(os.FileInfo) file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm)
m.Assert(ok) m.Assert(e)
info, e := os.Stat(arg[0])
m.Assert(e)
c.Caches = map[string]*ctx.Cache{ c.Caches = map[string]*ctx.Cache{
"name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"}, "name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"},
"mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件"}, "mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"},
"time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), 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: "文件"}, "size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"},
"pos": &ctx.Cache{Name: "pos", Value: "0", Help: "文件名"}, "pos": &ctx.Cache{Name: "pos", Value: "0", Help: "读写位置"},
} }
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
s := new(NFS) s := new(NFS)
s.Context = c s.Context = c
s.file = file
return s 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 { if nfs.Context == Index {
Pulse = m Pulse = m
} }
return nfs return nfs
} }
// }}} func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{ m.Log("info", nil, "%d open %s", Pulse.Capi("nfile"), m.Cap("name"))
m.Log("info", nil, "%d open %s", Pulse.Capi("nfile", 1), m.Cap("name"))
m.Cap("stream", m.Cap("name")) m.Cap("stream", m.Cap("name"))
return false 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 { switch nfs.Context {
case m.Target: case m.Target:
if nfs.Context == Index {
return false
}
if nfs.file != nil { if nfs.file != nil {
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name")) m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
nfs.file.Close() nfs.file.Close()
return true nfs.file = nil
} }
case m.Source: case m.Source:
} }
return true return true
} }
// }}}
var Pulse *ctx.Message var Pulse *ctx.Message
var Index = &ctx.Context{Name: "nfs", Help: "存储中心", var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
Caches: map[string]*ctx.Cache{ Caches: map[string]*ctx.Cache{
@ -81,22 +71,11 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
"size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"}, "size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) // {{{ m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
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 ""
// }}}
}}, }},
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
nfs, ok := m.Target.Server.(*NFS) // {{{ nfs, ok := m.Target.Server.(*NFS)
m.Assert(ok) m.Assert(ok)
var e error var e error
@ -105,45 +84,47 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
n, e = strconv.Atoi(arg[0]) n, e = strconv.Atoi(arg[0])
m.Assert(e) m.Assert(e)
} }
buf := make([]byte, n)
if len(arg) > 1 { if len(arg) > 1 {
m.Cap("pos", 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 { if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF {
m.Assert(e) m.Assert(e)
} }
m.Echo(string(buf))
if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") { if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") {
m.Cap("pos", "0") 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 { "write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
nfs, ok := m.Target.Server.(*NFS) // {{{ nfs, ok := m.Target.Server.(*NFS)
if m.Assert(ok); len(arg) > 1 { if m.Assert(ok); len(arg) > 1 {
m.Cap("pos", arg[1]) m.Cap("pos", arg[1])
} }
if len(arg[0]) == 0 { if len(arg[0]) == 0 {
e := nfs.file.Truncate(0) m.Assert(nfs.file.Truncate(int64(m.Capi("pos"))))
m.Assert(e) m.Cap("size", m.Cap("pos"))
m.Cap("size", "0")
m.Cap("pos", "0") m.Cap("pos", "0")
return "" } else {
}
n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos"))) n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos")))
if m.Assert(e); m.Capi("pos", n) > m.Capi("size") { if m.Assert(e) && m.Capi("pos", n) > m.Capi("size") {
m.Cap("size", m.Cap("pos")) 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() { func init() {

View File

@ -1,6 +1,6 @@
package tcp // {{{ package tcp
// }}}
import ( // {{{ import (
"context" "context"
"fmt" "fmt"
@ -8,15 +8,13 @@ import ( // {{{
"strconv" "strconv"
) )
// }}}
type TCP struct { type TCP struct {
net.Conn net.Conn
net.Listener net.Listener
*ctx.Context *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{ c.Caches = map[string]*ctx.Cache{
"protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"}, "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: "加密通信"}, "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 := new(TCP)
s.Context = c s.Context = c
return s return s
} }
// }}} func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ if tcp.Context == Index {
if m.Target == Index {
Pulse = m Pulse = m
} }
return tcp 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 { if len(arg) > 1 {
m.Cap("address", 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")) c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
m.Assert(e) m.Assert(e)
tcp.Conn = c tcp.Conn = c
m.Log("info", nil, "dial(%d) %v->%v", m.Capi("nclient", 1), 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())))
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.Cmd("open")
msg.Cap("stream", tcp.RemoteAddr().String()) msg.Cap("stream", tcp.LocalAddr().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")
return false return false
case "accept": case "accept":
c, e := m.Data["io"].(net.Conn) c, e := m.Data["io"].(net.Conn)
m.Assert(e) m.Assert(e)
tcp.Conn = c tcp.Conn = c
m.Log("info", nil, "accept(%d) %v<-%v", m.Capi("nclient", 1), 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())))
m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))
s, e := m.Data["source"].(*ctx.Context) msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", c)
m.Assert(e)
msg := m.Spawn(s).Put("option", "io", c)
msg.Cmd("open") msg.Cmd("open")
msg.Cap("stream", tcp.RemoteAddr().String()) 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 return false
} }
l, e := net.Listen(m.Cap("protocol"), m.Cap("address")) l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
m.Assert(e) m.Assert(e)
tcp.Listener = l tcp.Listener = l
m.Log("info", nil, "listen(%d) %v", m.Capi("nlisten", 1), l.Addr()) m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", l.Addr())))
m.Cap("stream", fmt.Sprintf("%s", l.Addr()))
for { for {
c, e := l.Accept() c, e := l.Accept()
m.Assert(e) 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 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 { switch tcp.Context {
case m.Target: case m.Target:
if tcp.Context == Index {
return false
}
if tcp.Listener != nil { 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.Close()
tcp.Listener = nil 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: case m.Source:
if tcp.Conn != nil { if tcp.Conn != nil {
msg := m.Spawn(tcp.Context) msg := m.Spawn(tcp.Context)
msg.Master = tcp.Context if msg.Master = tcp.Context; !tcp.Context.Close(msg, arg...) {
if !tcp.Context.Close(msg, arg...) {
return false 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 return true
} }
// }}} var Pulse *ctx.Message
var Index = &ctx.Context{Name: "tcp", Help: "网络中心", var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
Caches: map[string]*ctx.Cache{ Caches: map[string]*ctx.Cache{
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"}, "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: "加密通信"}, "security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
}, },
Commands: map[string]*ctx.Command{ 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 { "listen": &ctx.Command{Name: "listen address [security]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
switch len(arg) { // {{{ m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
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 ""
// }}}
}}, }},
"dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
switch len(arg) { // {{{ m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
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 ""
// }}}
}}, }},
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{ tcp, ok := m.Target.Server.(*TCP)
m.Assert(ok && tcp.Conn != nil)
tcp.Conn.Write([]byte(arg[0])) tcp.Conn.Write([]byte(arg[0]))
}
return ""
// }}}
}}, }},
"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]) size, e := strconv.Atoi(arg[0])
m.Assert(e) m.Assert(e)
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{
buf := make([]byte, size) buf := make([]byte, size)
tcp.Conn.Read(buf) tcp.Conn.Read(buf)
return string(buf) m.Echo(string(buf))
}
return ""
// }}}
}}, }},
}, },
Index: map[string]*ctx.Context{ Index: map[string]*ctx.Context{
@ -208,8 +151,6 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
}, },
} }
var Pulse *ctx.Message
func init() { func init() {
tcp := &TCP{} tcp := &TCP{}
tcp.Context = Index tcp.Context = Index

View File

@ -1,6 +1,6 @@
package web // {{{ package web
// }}}
import ( // {{{ import (
"context" "context"
"html/template" "html/template"
@ -13,12 +13,10 @@ import ( // {{{
"strings" "strings"
) )
// }}}
type MUX interface { type MUX interface {
Handle(string, http.Handler) Handle(string, http.Handler)
HandleFunc(string, func(http.ResponseWriter, *http.Request)) 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 { type WEB struct {
@ -29,10 +27,9 @@ type WEB struct {
*ctx.Context *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) { web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
msg := m.Spawn(m.Target) msg := m.Spawn(m.Target).Set("detail", key)
msg.Set("detail", key)
for k, v := range r.Form { for k, v := range r.Form {
msg.Add("option", k) msg.Add("option", k)
msg.Meta[k] = v 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.Log("cmd", nil, "%s %v", key, msg.Meta["option"])
msg.Put("option", "request", r) msg.Put("option", "request", r)
msg.Put("option", "response", w) msg.Put("option", "response", w)
hand(msg, msg.Target, key)
ret := hand(msg, msg.Target, key)
if ret != "" {
msg.Echo(ret)
}
header := w.Header() header := w.Header()
for _, k := range msg.Meta["append"] { for _, k := range msg.Meta["append"] {
ce := &http.Cookie{Name: k, Value: msg.Get(k)} header.Add("Set-Cookie", (&http.Cookie{Name: k, Value: msg.Get(k)}).String())
header.Add("Set-Cookie", ce.String())
} }
for _, v := range msg.Meta["result"] { for _, v := range msg.Meta["result"] {
w.Write([]byte(v)) 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 { if web.Message != nil {
log.Println() log.Println()
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL) web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
defer log.Println()
if web.Cap("logheaders") == "yes" { if web.Cap("logheaders") == "yes" {
for k, v := range r.Header { for k, v := range r.Header {
@ -75,8 +65,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
log.Println() log.Println()
} }
r.ParseForm() if r.ParseForm(); len(r.PostForm) > 0 {
if len(r.PostForm) > 0 {
for k, v := range r.PostForm { for k, v := range r.PostForm {
log.Printf("%s: %s", k, v[0]) 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) web.ServeMux.ServeHTTP(w, r)
if web.Message != nil { if web.Message != nil && web.Cap("logheaders") == "yes" {
if web.Cap("logheaders") == "yes" {
for k, v := range w.Header() { for k, v := range w.Header() {
log.Println(k+":", v[0]) 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{}
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: "服务目录"},
}
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
s := new(WEB) s := new(WEB)
@ -108,15 +92,14 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s return s
} }
// }}} func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server {
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ web.Caches["directory"] = &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}
if len(arg) > 0 {
m.Cap("directory", arg[0])
}
web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", 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["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"}
web.Caches["master"] = &ctx.Cache{Name: "master(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() web.ServeMux = http.NewServeMux()
if mux, ok := m.Target.Server.(MUX); ok { 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 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 { if len(arg) > 0 {
m.Cap("directory", arg[0]) m.Cap("directory", arg[0])
} }
@ -192,32 +174,23 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
return true 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 { switch web.Context {
case m.Target: case m.Target:
case m.Source: case m.Source:
} }
return true return true
} }
// }}}
var Index = &ctx.Context{Name: "web", Help: "应用中心", var Index = &ctx.Context{Name: "web", Help: "应用中心",
Caches: map[string]*ctx.Cache{ Caches: map[string]*ctx.Cache{},
"directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"},
},
Configs: map[string]*ctx.Config{}, Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{ 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 { "listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Meta["detail"] = arg // {{{ m.Set("detail", arg...).Target.Start(m)
m.Target.Start(m)
return ""
// }}}
}}, }},
"route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string { "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) // {{{ mux, ok := m.Target.Server.(MUX)
m.Assert(ok, "模块类型错误") m.Assert(ok, "模块类型错误")
m.Assert(len(arg) == 3, "缺少参数") m.Assert(len(arg) == 3, "缺少参数")
@ -225,7 +198,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
case "directory": case "directory":
mux.Handle(arg[1], http.FileServer(http.Dir(arg[2]))) mux.Handle(arg[1], http.FileServer(http.Dir(arg[2])))
case "template": 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) w := m.Data["response"].(http.ResponseWriter)
if _, e := os.Stat(arg[2]); e == nil { 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) template.Must(template.New("temp").Parse(arg[2])).Execute(w, m)
} }
return ""
}) })
// }}}
case "script": case "script":
cli := m.Find("cli", true) // {{{ cli := m.Find("cli", true)
lex := m.Find("lex", 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]) f, e := os.Open(arg[2])
line, bio := "", bufio.NewReader(f) line, bio := "", bufio.NewReader(f)
if e != nil { if e != nil {
line = arg[2] line = arg[2]
} }
@ -260,18 +230,12 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
break 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") m.Add("append", "hi", "hello")
return "hello" m.Echo("hello\n")
}}, }},
}, },
} }