1
0
forked from x/ContextOS

vps mod nfs 回复格式

This commit is contained in:
shaoying 2018-04-15 01:00:09 +08:00
parent 4745e5b1e2
commit 347dbd5de1
3 changed files with 73 additions and 57 deletions

View File

@ -1,7 +1,6 @@
package ctx // {{{ package ctx // {{{
// }}} // }}}
import ( // {{{ import ( // {{{
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"errors" "errors"

View File

@ -19,7 +19,7 @@ type NFS struct {
io io.ReadWriteCloser io io.ReadWriteCloser
*bufio.Reader *bufio.Reader
*bufio.Writer *bufio.Writer
send map[string]*ctx.Message send map[int]*ctx.Message
in *os.File in *os.File
out *os.File out *os.File
@ -86,16 +86,34 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.io = socket.(io.ReadWriteCloser) nfs.io = socket.(io.ReadWriteCloser)
nfs.Reader = bufio.NewReader(nfs.io) nfs.Reader = bufio.NewReader(nfs.io)
nfs.Writer = bufio.NewWriter(nfs.io) nfs.Writer = bufio.NewWriter(nfs.io)
nfs.send = make(map[string]*ctx.Message) nfs.send = make(map[int]*ctx.Message)
target, msg := m.Target(), m.Spawn(m.Target()) target, msg := m.Target(), m.Spawn(m.Target())
nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: target.Name, Help: "文件名"} nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: target.Name, Help: "文件名"}
nsend := 0
for { for {
line, e := nfs.Reader.ReadString('\n') line, e := nfs.Reader.ReadString('\n')
m.Assert(e) m.Assert(e)
if line = strings.TrimSpace(line); len(line) == 0 { if line = strings.TrimSpace(line); len(line) > 0 {
ls := strings.SplitN(line, ":", 2)
ls[0] = strings.TrimSpace(ls[0])
ls[1], e = url.QueryUnescape(strings.TrimSpace(ls[1]))
m.Assert(e)
switch ls[0] {
case "nsend":
n, e := strconv.Atoi(ls[1])
m.Assert(e)
nsend = n
default:
msg.Add("option", ls[0], ls[1])
}
continue
}
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") { if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") {
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"]) msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
@ -124,11 +142,12 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
} }
} else { } else {
msg.Log("info", nil, "%s echo: %v", msg.Get("nsend"), msg.Meta["result"]) msg.Log("info", nil, "%d echo: %v", nsend, msg.Meta["result"])
m.Cap("result", msg.Get("result")) m.Cap("result", msg.Get("result"))
msg.Meta["append"] = msg.Meta["option"] msg.Meta["append"] = msg.Meta["option"]
send := nfs.send[msg.Get("nsend")] delete(msg.Meta, "option")
send := nfs.send[nsend]
send.Meta = msg.Meta send.Meta = msg.Meta
if send.Has("io") { if send.Has("io") {
@ -140,17 +159,9 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
send.Recv <- true send.Recv <- true
} }
msg = m.Spawn(target) msg = m.Spawn(target)
m.Cap("target", target.Name) m.Cap("target", target.Name)
continue
}
ls := strings.SplitN(line, ":", 2)
ls[0] = strings.TrimSpace(ls[0])
ls[1], e = url.QueryUnescape(strings.TrimSpace(ls[1]))
m.Assert(e)
msg.Add("option", ls[0], ls[1])
} }
return true return true
} }
@ -319,7 +330,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} }
} else { } else {
nfs.send[m.Option("nrecv", m.Capi("nsend", 1))] = m nfs.send[m.Optioni("nrecv", m.Capi("nsend", 1))] = m
if len(arg) > 1 && arg[0] == "file" { if len(arg) > 1 && arg[0] == "file" {
info, e := os.Stat(arg[1]) info, e := os.Stat(arg[1])
@ -352,8 +363,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
f, e := os.Open(arg[1]) f, e := os.Open(arg[1])
m.Assert(e) m.Assert(e)
defer f.Close() defer f.Close()
n, e := io.Copy(nfs.Writer, f) _, e = io.Copy(nfs.Writer, f)
m.Log("fuck", nil, "write %d %v", n, e)
} }
} }
@ -375,7 +385,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
return return
} }
nfs.send[m.Option("nrecv", m.Capi("nsend", 1))] = m nfs.send[m.Optioni("nrecv", m.Capi("nsend", 1))] = m
if len(arg) > 1 && arg[0] == "file" { if len(arg) > 1 && arg[0] == "file" {
f, e := os.Create(arg[1]) f, e := os.Create(arg[1])

View File

@ -33,7 +33,14 @@ type WEB struct {
} }
func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{ func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{
b, e := json.Marshal(msg.Meta) meta := map[string][]string{}
meta["result"] = msg.Meta["result"]
meta["append"] = msg.Meta["append"]
for _, v := range msg.Meta["append"] {
meta[v] = msg.Meta[v]
}
b, e := json.Marshal(meta)
msg.Assert(e) msg.Assert(e)
return string(b) return string(b)