[add] Socket client mode support & fix watch bug

This commit is contained in:
Max 2022-03-01 16:39:36 +08:00
parent d6f01fea96
commit 8566db96b0
8 changed files with 71 additions and 65 deletions

View file

@ -88,15 +88,20 @@ var startCmd = &cobra.Command{
service.Watch(config.Conf) service.Watch(config.Conf)
} }
// 启用内测功能 // with the alpha features
if startAlpha { if startAlpha {
for _, srv := range gou.Servers { for _, sock := range gou.Sockets {
fmt.Println(color.WhiteString("\n---------------------------------")) fmt.Println(color.WhiteString("\n---------------------------------"))
fmt.Println(color.WhiteString(srv.Name)) fmt.Println(color.WhiteString(sock.Name))
fmt.Println(color.WhiteString("---------------------------------")) fmt.Println(color.WhiteString("---------------------------------"))
fmt.Println(color.GreenString("Host: %s://%s", srv.Protocol, srv.Host)) fmt.Println(color.GreenString("Mode: %s", sock.Mode))
fmt.Println(color.GreenString("Port: %s\n\n", srv.Port)) fmt.Println(color.GreenString("Host: %s://%s", sock.Protocol, sock.Host))
go srv.Start() fmt.Println(color.GreenString("Port: %s\n\n", sock.Port))
if sock.Mode == "server" {
go sock.Start()
} else if sock.Mode == "client" {
go sock.Connect()
}
} }
} }

View file

@ -208,7 +208,7 @@ func uiIndexHtml() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "ui/index.html", size: 22, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "ui/index.html", size: 22, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -228,7 +228,7 @@ func yaoDs_store() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -248,7 +248,7 @@ func yaoApisReadmeMd() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -268,7 +268,7 @@ func yaoApisChartHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -288,7 +288,7 @@ func yaoApisImportHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/import.http.json", size: 1676, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -308,7 +308,7 @@ func yaoApisPageHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -328,7 +328,7 @@ func yaoApisStorageHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -348,7 +348,7 @@ func yaoApisTableHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3196, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/table.http.json", size: 3196, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -368,7 +368,7 @@ func yaoApisUserHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -388,7 +388,7 @@ func yaoApisXiangHttpJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/apis/xiang.http.json", size: 1239, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -408,7 +408,7 @@ func yaoDataDs_store() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -428,7 +428,7 @@ func yaoDataAppJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/app.json", size: 277, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -448,7 +448,7 @@ func yaoDataIconsDs_store() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -468,7 +468,7 @@ func yaoDataIcons404Png() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -488,7 +488,7 @@ func yaoDataIconsIconIcns() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/icons/icon.icns", size: 67465, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -508,7 +508,7 @@ func yaoDataIconsIconIco() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/icons/icon.ico", size: 54993, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -528,7 +528,7 @@ func yaoDataIconsIconPng() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/icons/icon.png", size: 34558, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -548,7 +548,7 @@ func yaoDataIndexHtml() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/data/index.html", size: 282, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -568,7 +568,7 @@ func yaoFlowsReadmeMd() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -588,7 +588,7 @@ func yaoFlowsAdminGuardFlowJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/flows/admin/guard.flow.json", size: 559, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/flows/admin/guard.flow.json", size: 559, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -608,7 +608,7 @@ func yaoFlowsMenuFlowJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -628,7 +628,7 @@ func yaoFlowsUserFindFlowJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/flows/user/find.flow.json", size: 328, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -648,7 +648,7 @@ func yaoLangsEnUsJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/langs/en-US.json", size: 66, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -668,7 +668,7 @@ func yaoModelsReadmeMd() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -688,7 +688,7 @@ func yaoModelsMenuModJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/models/menu.mod.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -708,7 +708,7 @@ func yaoModelsUserModJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/models/user.mod.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -728,7 +728,7 @@ func yaoModelsWorkflowModJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/models/workflow.mod.json", size: 3333, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -748,7 +748,7 @@ func yaoTablesReadmeMd() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -768,7 +768,7 @@ func yaoTablesMenuTabJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10471, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/tables/menu.tab.json", size: 10471, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }
@ -788,7 +788,7 @@ func yaoTablesUserTabJson() (*asset, error) {
return nil, err return nil, err
} }
info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2357, mode: os.FileMode(420), modTime: time.Unix(1645092418, 0)} info := bindataFileInfo{name: "yao/tables/user.tab.json", size: 2357, mode: os.FileMode(420), modTime: time.Unix(1646123553, 0)}
a := &asset{bytes: bytes, info: info} a := &asset{bytes: bytes, info: info}
return a, nil return a, nil
} }

View file

@ -26,7 +26,7 @@ func LoadFrom(dir string, prefix string) error {
err := share.Walk(dir, ".sock.json", func(root, filename string) { err := share.Walk(dir, ".sock.json", func(root, filename string) {
name := prefix + share.SpecName(root, filename) name := prefix + share.SpecName(root, filename)
content := share.ReadFile(filename) content := share.ReadFile(filename)
_, err := gou.LoadServer(string(content), name) _, err := gou.LoadSocket(string(content), name)
if err != nil { if err != nil {
log.With(log.F{"root": root, "file": filename}).Error(err.Error()) log.With(log.F{"root": root, "file": filename}).Error(err.Error())
} }

View file

@ -16,8 +16,8 @@ func TestLoad(t *testing.T) {
func check(t *testing.T) { func check(t *testing.T) {
keys := []string{} keys := []string{}
for key := range gou.Servers { for key := range gou.Sockets {
keys = append(keys, key) keys = append(keys, key)
} }
assert.Equal(t, 1, len(keys)) assert.Equal(t, 2, len(keys))
} }

View file

@ -6,13 +6,14 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/yaoapp/kun/exception" "github.com/yaoapp/kun/exception"
) )
var watchDone = []chan bool{} var watchGroup sync.WaitGroup
var watchOp = map[fsnotify.Op]string{ var watchOp = map[fsnotify.Op]string{
fsnotify.Create: "create", fsnotify.Create: "create",
fsnotify.Write: "write", fsnotify.Write: "write",
@ -27,10 +28,12 @@ func Watch(root string, cb func(op string, file string)) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer watcher.Close() defer func() {
watcher.Close()
watchGroup.Done()
}()
watchDone = append(watchDone, make(chan bool)) watchGroup.Add(1)
last := len(watchDone) - 1
go func() { go func() {
for { for {
select { select {
@ -86,20 +89,6 @@ func Watch(root string, cb func(op string, file string)) {
return nil return nil
}) })
select { watchGroup.Wait()
case v := <-watchDone[last]:
fmt.Println(color.GreenString("Stop Watching: %s", root))
if v == true {
break
}
}
}
// StopWatch 停止监听
func StopWatch() {
for i := range watchDone {
fmt.Println(color.GreenString("Stop: %d", i))
watchDone[i] <- true
}
watchDone = []chan bool{}
} }

View file

@ -5,7 +5,6 @@ import (
"os" "os"
"path" "path"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -16,7 +15,5 @@ func TestWatch(t *testing.T) {
go Watch(root, func(op string, file string) { go Watch(root, func(op string, file string) {
log.Println(op, file) log.Println(op, file)
}) })
time.Sleep(time.Second * 2)
defer StopWatch()
}) })
} }

View file

@ -1,9 +1,12 @@
{ {
"name": "RFID接收器", "name": "RFID Receiver (Server Mode)",
"description": "用来接收并处理RIFD", "description": "RFID Receiver (Server Mode)",
"version": "0.0.1", "mode": "server",
"version": "0.9.2",
"protocol": "tcp", "protocol": "tcp",
"host": "0.0.0.0", "host": "0.0.0.0",
"port": "3019", "port": "3019",
"process": "flows.rfid.read" "process": "flows.rfid.read",
"buffer": 60,
"keep": 0
} }

View file

@ -0,0 +1,12 @@
{
"name": "RFID Receiver (Client Mode)",
"description": "RFID Receiver (Client Mode)",
"mode": "client",
"version": "0.9.2",
"protocol": "tcp",
"host": "192.168.1.192",
"port": "6000",
"process": "flows.rfid.read",
"buffer": 60,
"keep": 0
}