.
git-svn-id: svn://47.119.165.148/zhub@63 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
108
zsub/msg-consumer.go
Normal file
108
zsub/msg-consumer.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func msgAccept(v Message) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Println("ExecCmd Recovered:", r)
|
||||
}
|
||||
}()
|
||||
c := v.Conn
|
||||
rcmd := v.Rcmd
|
||||
|
||||
if len(rcmd) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("rcmd: " + strings.Join(rcmd, " "))
|
||||
|
||||
if len(rcmd) == 1 {
|
||||
switch strings.ToLower(rcmd[0]) {
|
||||
default:
|
||||
// subscribe|unsubscribe|daly
|
||||
if strings.Index(rcmd[0], "subscribe") == 0 || strings.Index(rcmd[0], "unsubscribe") == 0 || strings.Index(rcmd[0], "daly") == 0 {
|
||||
rcmd = strings.Split(rcmd[0], " ")
|
||||
} else {
|
||||
send(c.conn, "-Error: not supported! (tips: send help)")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmd := rcmd[0]
|
||||
switch cmd {
|
||||
case "subscribe":
|
||||
//subscribe x y z
|
||||
for _, topic := range rcmd[1:] {
|
||||
zsub.subscribe(c, topic) // todo: 批量一次订阅
|
||||
}
|
||||
case "unsubscribe":
|
||||
for _, topic := range rcmd[1:] {
|
||||
zsub.unsubscribe(c, topic)
|
||||
}
|
||||
case "publish":
|
||||
if len(rcmd) != 3 {
|
||||
send(c.conn, "-Error: publish para number!")
|
||||
} else {
|
||||
zsub.publish(rcmd[1], rcmd[2])
|
||||
}
|
||||
case "daly":
|
||||
daly(rcmd, c)
|
||||
case "timer":
|
||||
// todo Timer(rcmd, conn)
|
||||
default:
|
||||
send(c.conn, "-Error: default not supported:["+strings.Join(rcmd, " ")+"]")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// daly topic valye 100
|
||||
func daly(rcmd []string, c *ZConn) {
|
||||
if len(rcmd) != 4 {
|
||||
send(c.conn, "-Error: subscribe para number!")
|
||||
return
|
||||
}
|
||||
|
||||
t, err := strconv.ParseInt(rcmd[3], 10, 64)
|
||||
if err != nil {
|
||||
send(c.conn, "-Error: "+strings.Join(rcmd, " "))
|
||||
return
|
||||
}
|
||||
|
||||
timer := time.NewTimer(time.Duration(t) * time.Millisecond)
|
||||
select {
|
||||
case <-timer.C:
|
||||
zsub.publish(rcmd[1], rcmd[2])
|
||||
}
|
||||
}
|
||||
|
||||
var wlock = sync.Mutex{}
|
||||
|
||||
// 发送消息
|
||||
func send(conn *net.Conn, vs ...string) error {
|
||||
wlock.Lock()
|
||||
defer wlock.Unlock()
|
||||
|
||||
var bytes []byte
|
||||
|
||||
if len(vs) == 1 {
|
||||
bytes = []byte(vs[0] + "\r\n")
|
||||
} else if len(vs) > 1 {
|
||||
data := "*" + strconv.Itoa(len(vs)) + "\r\n"
|
||||
for _, v := range vs {
|
||||
data += "$" + strconv.Itoa(len(v)) + "\r\n"
|
||||
data += v + "\r\n"
|
||||
}
|
||||
bytes = []byte(data)
|
||||
}
|
||||
_, err := (*conn).Write(bytes)
|
||||
return err
|
||||
}
|
||||
11
zsub/zdb.go
Normal file
11
zsub/zdb.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package zsub
|
||||
|
||||
var (
|
||||
chanMessages = make(chan Message, 1000) //接收到的 所有消息数据
|
||||
)
|
||||
|
||||
// 数据封装
|
||||
type Message struct {
|
||||
Conn *ZConn
|
||||
Rcmd []string
|
||||
}
|
||||
100
zsub/zsub.go
100
zsub/zsub.go
@@ -1,7 +1,11 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -12,13 +16,14 @@ var (
|
||||
type ZSub struct {
|
||||
sync.Mutex
|
||||
topics map[string]*ZTopic
|
||||
timers map[string]*ZTimer
|
||||
}
|
||||
|
||||
|
||||
type ZConn struct { //ZConn
|
||||
conn *net.Conn
|
||||
groupid string
|
||||
topics []string
|
||||
timers []string // 订阅、定时调度分别创建各自连接
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -77,7 +82,7 @@ func (s ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
|
||||
|
||||
/*
|
||||
发送主题消息
|
||||
1、写入主题消息列表(zdb)
|
||||
1、写入主题消息列表(_zdb)
|
||||
2、回复消息写入成功
|
||||
3、推送主题消息
|
||||
*/
|
||||
@@ -90,6 +95,95 @@ func (s ZSub) publish(topic string, message string) {
|
||||
}
|
||||
|
||||
for _, zgroup := range ztopic.groups {
|
||||
zgroup.chMsg <- message
|
||||
zgroup.chMsg <- message // 不同主题消费独立进行
|
||||
}
|
||||
}
|
||||
|
||||
func (s ZSub) close(c *ZConn) {
|
||||
// 订阅
|
||||
for _, topic := range c.topics {
|
||||
s.unsubscribe(c, topic)
|
||||
}
|
||||
|
||||
// 延时
|
||||
|
||||
// timer conn close
|
||||
for _, topic := range c.timers { // fixme: 数据逻辑交叉循环
|
||||
timer := s.timers[topic]
|
||||
if timer != nil {
|
||||
timer.close(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ================== ZHub 服务 =====================================
|
||||
func ServerStart(host string, port int) {
|
||||
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
log.Printf("_zdb started listen on: %s:%d \n", host, port)
|
||||
|
||||
// 启动消息监听处理
|
||||
go func() {
|
||||
for {
|
||||
v, ok := <-chanMessages
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
// 事件消费
|
||||
msgAccept(v)
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
conn, err := listen.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
fmt.Println("conn start: ", conn.RemoteAddr())
|
||||
|
||||
go zsub.acceptHandler(&ZConn{conn: &conn})
|
||||
}
|
||||
}
|
||||
|
||||
// 连接处理
|
||||
func (s ZSub) acceptHandler(c *ZConn) {
|
||||
defer func() {
|
||||
s.close(c) // 关闭连接
|
||||
}()
|
||||
|
||||
reader := bufio.NewReader(*c.conn)
|
||||
for {
|
||||
rcmd := make([]string, 0)
|
||||
line, _, err := reader.ReadLine()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
switch string(line[:1]) {
|
||||
case "*":
|
||||
n, _ := strconv.Atoi(string(line[1:]))
|
||||
for i := 0; i < n; i++ {
|
||||
reader.ReadLine()
|
||||
v, _, _ := reader.ReadLine()
|
||||
rcmd = append(rcmd, string(v))
|
||||
}
|
||||
default:
|
||||
rcmd = append(rcmd, string(line))
|
||||
}
|
||||
|
||||
if len(rcmd) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 接收消息 zdb fixme: 细节暴露太多
|
||||
chanMessages <- Message{Conn: c, Rcmd: rcmd}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
func TestName(t *testing.T) {
|
||||
sub := ZSub{
|
||||
topics: map[string]*ZTopic{},
|
||||
timers: map[string]*ZTimer{},
|
||||
}
|
||||
|
||||
sub.subscribe(&ZConn{
|
||||
|
||||
62
zsub/ztimer.go
Normal file
62
zsub/ztimer.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ZTimer struct {
|
||||
conns []*ZConn
|
||||
expr string
|
||||
topic string
|
||||
cron *cron.Cron
|
||||
}
|
||||
|
||||
func (s ZSub) timer(rcmd []string, c *ZConn) {
|
||||
timer := s.timers[rcmd[1]]
|
||||
if timer == nil {
|
||||
timer = &ZTimer{
|
||||
conns: []*ZConn{},
|
||||
topic: rcmd[1],
|
||||
}
|
||||
s.timers[rcmd[1]] = timer
|
||||
}
|
||||
|
||||
_conns := make([]*ZConn, 0)
|
||||
for _, conn := range timer.conns {
|
||||
if conn == c {
|
||||
continue
|
||||
}
|
||||
_conns = append(_conns, c)
|
||||
}
|
||||
_conns = append(_conns, c)
|
||||
timer.conns = _conns
|
||||
|
||||
if !strings.EqualFold(timer.expr, rcmd[2]) {
|
||||
timer.expr = rcmd[2]
|
||||
if timer.cron != nil {
|
||||
timer.cron.Stop()
|
||||
}
|
||||
timer.cron = func() *cron.Cron {
|
||||
c := cron.New()
|
||||
c.AddFunc(timer.expr, func() {
|
||||
fmt.Println(time.Now().Second())
|
||||
for _, conn := range timer.conns {
|
||||
send(conn.conn, "timer", timer.topic)
|
||||
}
|
||||
})
|
||||
go c.Run()
|
||||
return c
|
||||
}()
|
||||
}
|
||||
|
||||
s.timers[rcmd[1]] = timer
|
||||
fmt.Println("xx")
|
||||
}
|
||||
|
||||
func (t ZTimer) close(c *ZConn) {
|
||||
// todo timer zconn
|
||||
|
||||
}
|
||||
@@ -9,6 +9,6 @@ type ZTopic struct { //ZTopic
|
||||
chMsg chan string // 主题消息投递
|
||||
}
|
||||
|
||||
func createZTopic() {
|
||||
// 主题消息发送
|
||||
|
||||
}
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user