git-svn-id: svn://47.119.165.148/zhub@61 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-08 11:52:30 +00:00
parent c0e9fa0c6b
commit 2dcfc5e9bf
3 changed files with 5 additions and 161 deletions

View File

@@ -1,8 +1,6 @@
package zdb
import (
"fmt"
"github.com/robfig/cron"
"log"
"net"
"strconv"
@@ -60,55 +58,13 @@ func execCmd(rcmd []string, conn net.Conn) {
case "daly":
daly(rcmd, conn)
case "timer":
timer(rcmd, conn)
Timer(rcmd, conn)
default:
conn.Write([]byte("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]\r\n"))
return
}
}
func timer(rcmd []string, conn net.Conn) {
ztimer := zTimer[rcmd[1]]
if ztimer == nil {
ztimer = &ZTimer{
conns: []*net.Conn{},
topic: rcmd[1],
}
zTimer[rcmd[1]] = ztimer
}
_conns := make([]*net.Conn, 0)
for _, c := range ztimer.conns {
if *&conn == *c {
continue
}
_conns = append(_conns, c)
}
_conns = append(_conns, &conn)
ztimer.conns = _conns
if !strings.EqualFold(ztimer.expr, rcmd[2]) {
ztimer.expr = rcmd[2]
if ztimer.cron != nil {
ztimer.cron.Stop()
}
ztimer.cron = func() *cron.Cron {
c := cron.New()
c.AddFunc(ztimer.expr, func() {
fmt.Println(time.Now().Second())
for _, conn := range ztimer.conns {
send(*conn, "timer", ztimer.topic)
}
})
go c.Run()
return c
}()
}
zTimer[ztimer.topic] = ztimer
fmt.Println("xx")
}
// daly topic valye 100
func daly(rcmd []string, conn net.Conn) {
if len(rcmd) != 4 {
@@ -229,7 +185,7 @@ func publish(rcmd []string, conn net.Conn) {
msgs := []string{"message", topic, v}
for _, c := range subs {
send(*c.conn, msgs...)
Send(*c.conn, msgs...)
/*_conn.Write([]byte("*3\r\n"))
for _, msg := range msgs {
_conn.Write([]byte("$" + strconv.Itoa(len(msg)) + "\r\n"))
@@ -240,7 +196,7 @@ func publish(rcmd []string, conn net.Conn) {
var wlock = sync.Mutex{}
func send(conn net.Conn, vs ...string) (err error) {
func Send(conn net.Conn, vs ...string) (err error) {
//chSend <- vs
wlock.Lock()
defer wlock.Unlock()

View File

@@ -3,7 +3,6 @@ package zdb
import (
"bufio"
"fmt"
"github.com/robfig/cron"
"log"
"net"
"strconv"
@@ -16,7 +15,6 @@ var (
zkv = make(map[string]string)
zsub = make(map[string][]*ConnContext) // topic -- connx[]
retOk = []byte("+OK")
zTimer = make(map[string]*ZTimer)
retHelp = []byte(
"\n--- zdb help ---\n" +
"______ _____ _____ \n|___ / | _ \\ | _ \\ \n / / | | | | | |_| | \n / / | | | | | _ { \n / /__ | |_| | | |_| | \n/_____| |_____/ |_____/ \n" +
@@ -49,16 +47,9 @@ type ConnContext struct {
createTime time.Time
}
type ZTimer struct {
conns []*net.Conn
expr string
topic string
cron *cron.Cron
}
// ======================================================================
// zdb 服务启动
// ZHub 服务启动
func ServerStart(host string, port int) {
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {