mqp
This commit is contained in:
1 parent
d37100d059
commit
65ac09cf50
1 file changed
+68
-46
+68
-46
@@ -6,6 +6,7 @@ import (
|
|||||||
"hmq/packets"
|
"hmq/packets"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,9 +21,9 @@ type Broker struct {
|
|||||||
config *Config
|
config *Config
|
||||||
tlsConfig *tls.Config
|
tlsConfig *tls.Config
|
||||||
AclConfig *acl.ACLConfig
|
AclConfig *acl.ACLConfig
|
||||||
clients cMap
|
clients sync.Map
|
||||||
routes cMap
|
routes sync.Map
|
||||||
remotes cMap
|
remotes sync.Map
|
||||||
sl *Sublist
|
sl *Sublist
|
||||||
rl *RetainList
|
rl *RetainList
|
||||||
queues map[string]int
|
queues map[string]int
|
||||||
@@ -30,14 +31,11 @@ type Broker struct {
|
|||||||
|
|
||||||
func NewBroker(config *Config) (*Broker, error) {
|
func NewBroker(config *Config) (*Broker, error) {
|
||||||
b := &Broker{
|
b := &Broker{
|
||||||
id: GenUniqueId(),
|
id: GenUniqueId(),
|
||||||
config: config,
|
config: config,
|
||||||
sl: NewSublist(),
|
sl: NewSublist(),
|
||||||
rl: NewRetainList(),
|
rl: NewRetainList(),
|
||||||
queues: make(map[string]int),
|
queues: make(map[string]int),
|
||||||
clients: NewClientMap(),
|
|
||||||
routes: NewClientMap(),
|
|
||||||
remotes: NewClientMap(),
|
|
||||||
}
|
}
|
||||||
if b.config.TlsPort != "" {
|
if b.config.TlsPort != "" {
|
||||||
tlsconfig, err := NewTLSConfig(b.config.TlsInfo)
|
tlsconfig, err := NewTLSConfig(b.config.TlsInfo)
|
||||||
@@ -217,20 +215,31 @@ func (b *Broker) handleConnection(typ int, conn net.Conn, idx uint64) {
|
|||||||
}
|
}
|
||||||
c.init()
|
c.init()
|
||||||
|
|
||||||
|
cid := c.info.clientID
|
||||||
|
|
||||||
var msgPool *MessagePool
|
var msgPool *MessagePool
|
||||||
var exist bool
|
var loaded bool
|
||||||
var old *client
|
var old *client
|
||||||
cid := string(c.info.clientID)
|
|
||||||
if typ == CLIENT {
|
switch typ {
|
||||||
old, exist = b.clients.Update(cid, c)
|
case CLIENT:
|
||||||
msgPool = MSGPool[idx%MessagePoolNum].GetPool()
|
msgPool = MSGPool[idx%MessagePoolNum].GetPool()
|
||||||
} else if typ == ROUTER {
|
old, loaded = b.clients.LoadOrStore(cid)
|
||||||
old, exist = b.routes.Update(cid, c)
|
if !loaded {
|
||||||
|
b.clients.Store(cid, c)
|
||||||
|
old.(*client).Close()
|
||||||
|
}
|
||||||
|
case ROUTER:
|
||||||
msgPool = MSGPool[MessagePoolNum].GetPool()
|
msgPool = MSGPool[MessagePoolNum].GetPool()
|
||||||
|
old, loaded = b.routes.LoadOrStore(cid)
|
||||||
|
if !loaded {
|
||||||
|
b.routes.Store(cid, c)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if exist {
|
|
||||||
|
if !loaded {
|
||||||
log.Warn("client or routers exists, close old...")
|
log.Warn("client or routers exists, close old...")
|
||||||
old.Close()
|
old.(*client).Close()
|
||||||
}
|
}
|
||||||
c.readLoop(msgPool)
|
c.readLoop(msgPool)
|
||||||
}
|
}
|
||||||
@@ -267,7 +276,7 @@ func (b *Broker) connectRouter(url, remoteID string) {
|
|||||||
route: route,
|
route: route,
|
||||||
info: info,
|
info: info,
|
||||||
}
|
}
|
||||||
b.remotes.Set(cid, c)
|
b.remotes.Store(cid, c)
|
||||||
c.SendConnect()
|
c.SendConnect()
|
||||||
c.SendInfo()
|
c.SendInfo()
|
||||||
// s.createRemote(conn, route)
|
// s.createRemote(conn, route)
|
||||||
@@ -278,29 +287,35 @@ func (b *Broker) connectRouter(url, remoteID string) {
|
|||||||
|
|
||||||
func (b *Broker) CheckRemoteExist(remoteID, url string) bool {
|
func (b *Broker) CheckRemoteExist(remoteID, url string) bool {
|
||||||
exist := false
|
exist := false
|
||||||
remotes := b.remotes.Items()
|
remotes := b.remotes.Range(func(key, value interface{}) bool {
|
||||||
for _, v := range remotes {
|
v, ok := value.(*client)
|
||||||
if v.route.remoteUrl == url {
|
if ok {
|
||||||
// if v.route.remoteID == "" || v.route.remoteID != remoteID {
|
if v.route.remoteUrl == url {
|
||||||
v.route.remoteID = remoteID
|
// if v.route.remoteID == "" || v.route.remoteID != remoteID {
|
||||||
// }
|
v.route.remoteID = remoteID
|
||||||
exist = true
|
// }
|
||||||
break
|
exist = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
return exist
|
return exist
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) SendLocalSubsToRouter(c *client) {
|
func (b *Broker) SendLocalSubsToRouter(c *client) {
|
||||||
clients := b.clients.Items()
|
|
||||||
subInfo := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
subInfo := packets.NewControlPacket(packets.Subscribe).(*packets.SubscribePacket)
|
||||||
for _, client := range clients {
|
b.clients.Range(func(key, value interface{}) bool {
|
||||||
subs := client.subs
|
client, ok := value.(*clients)
|
||||||
for _, sub := range subs {
|
if ok {
|
||||||
subInfo.Topics = append(subInfo.Topics, string(sub.topic))
|
subs := client.subs
|
||||||
subInfo.Qoss = append(subInfo.Qoss, sub.qos)
|
for _, sub := range subs {
|
||||||
|
subInfo.Topics = append(subInfo.Topics, string(sub.topic))
|
||||||
|
subInfo.Qoss = append(subInfo.Qoss, sub.qos)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return true
|
||||||
|
})
|
||||||
err := c.WriterPacket(subInfo)
|
err := c.WriterPacket(subInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Send localsubs To Router error :", err)
|
log.Error("Send localsubs To Router error :", err)
|
||||||
@@ -308,21 +323,28 @@ func (b *Broker) SendLocalSubsToRouter(c *client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) BroadcastInfoMessage(remoteID string, msg *packets.PublishPacket) {
|
func (b *Broker) BroadcastInfoMessage(remoteID string, msg *packets.PublishPacket) {
|
||||||
remotes := b.remotes.Items()
|
b.remotes.Range(func(key, value interface{}) bool {
|
||||||
for _, r := range remotes {
|
r, ok := value.(*client)
|
||||||
if r.route.remoteID == remoteID {
|
if ok {
|
||||||
continue
|
if r.route.remoteID == remoteID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
r.WriterPacket(msg)
|
||||||
}
|
}
|
||||||
r.WriterPacket(msg)
|
return true
|
||||||
}
|
|
||||||
|
})
|
||||||
// log.Info("BroadcastInfoMessage success ")
|
// log.Info("BroadcastInfoMessage success ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) BroadcastSubOrUnsubMessage(packet packets.ControlPacket) {
|
func (b *Broker) BroadcastSubOrUnsubMessage(packet packets.ControlPacket) {
|
||||||
remotes := b.remotes.Items()
|
b.remotes.Range(func(key, value interface{}) bool {
|
||||||
for _, r := range remotes {
|
r, ok := value.(*client)
|
||||||
r.WriterPacket(packet)
|
if ok {
|
||||||
}
|
r.WriterPacket(msg)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
// log.Info("BroadcastSubscribeMessage remotes: ", s.remotes)
|
// log.Info("BroadcastSubscribeMessage remotes: ", s.remotes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user