Skip to content

Commit

Permalink
Restruct (#34)
Browse files Browse the repository at this point in the history
* modify

* remove

* modify

* modify

* remove no use

* add online/offline notification

* modify

* format log

* add reference
  • Loading branch information
chowyu08 authored Dec 26, 2018
1 parent 84e7fe2 commit 7547ad3
Show file tree
Hide file tree
Showing 16 changed files with 1,232 additions and 714 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ Common Options:

* Supports will messages

* Queue subscribe

* Websocket Support

* TLS/SSL Support
Expand All @@ -95,13 +93,6 @@ Common Options:

```

### QUEUE SUBSCRIBE
~~~
| Prefix | Examples |
| ------------- |---------------------------------|
| $queue/ | mosquitto_sub -t ‘$queue/topic’ |
~~~

### ACL Configure
#### The ACL rules define:
~~~
Expand Down Expand Up @@ -154,6 +145,14 @@ Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | -->
allow | deny allow | deny allow | deny
~~~

### Online/Offline Notification
```bash
topic:
$SYS/broker/connection/clients/<clientID>
payload:
{"clientID":"client001","online":true/false,"timestamp":"2018-10-25T09:32:32Z"}
```

## Performance

* High throughput
Expand All @@ -166,3 +165,8 @@ Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | -->
## License

* Apache License Version 2.0


## Reference

* Surgermq.(https://github.com/surgemq/surgemq)
62 changes: 48 additions & 14 deletions broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package broker

import (
"crypto/tls"
"fmt"
"net"
"net/http"
"runtime/debug"
Expand All @@ -13,6 +14,8 @@ import (

"github.com/eclipse/paho.mqtt.golang/packets"
"github.com/fhmq/hmq/lib/acl"
"github.com/fhmq/hmq/lib/sessions"
"github.com/fhmq/hmq/lib/topics"
"github.com/fhmq/hmq/pool"
"github.com/shirou/gopsutil/mem"
"go.uber.org/zap"
Expand Down Expand Up @@ -42,9 +45,9 @@ type Broker struct {
remotes sync.Map
nodes map[string]interface{}
clusterPool chan *Message
sl *Sublist
rl *RetainList
queues map[string]int
topicsMgr *topics.Manager
sessionMgr *sessions.Manager
// messagePool []chan *Message
}

Expand All @@ -62,13 +65,24 @@ func NewBroker(config *Config) (*Broker, error) {
id: GenUniqueId(),
config: config,
wpool: pool.New(config.Worker),
sl: NewSublist(),
rl: NewRetainList(),
nodes: make(map[string]interface{}),
queues: make(map[string]int),
clusterPool: make(chan *Message),
// messagePool: newMessagePool(),
}

var err error
b.topicsMgr, err = topics.NewManager("mem")
if err != nil {
log.Error("new topic manager error", zap.Error(err))
return nil, err
}

b.sessionMgr, err = sessions.NewManager("mem")
if err != nil {
log.Error("new session manager error", zap.Error(err))
return nil, err
}

if b.config.TlsPort != "" {
tlsconfig, err := NewTLSConfig(b.config.TlsInfo)
if err != nil {
Expand Down Expand Up @@ -333,6 +347,12 @@ func (b *Broker) handleConnection(typ int, conn net.Conn) {

c.init()

err = b.getSession(c, msg, connack)
if err != nil {
log.Error("get session error: ", zap.String("clientID", c.info.clientID))
return
}

cid := c.info.clientID

var exist bool
Expand All @@ -349,6 +369,8 @@ func (b *Broker) handleConnection(typ int, conn net.Conn) {
}
}
b.clients.Store(cid, c)

b.OnlineOfflineNotification(cid, true)
case ROUTER:
old, exist = b.routes.Load(cid)
if exist {
Expand Down Expand Up @@ -535,9 +557,9 @@ func (b *Broker) SendLocalSubsToRouter(c *client) {
b.clients.Range(func(key, value interface{}) bool {
client, ok := value.(*client)
if ok {
subs := client.subs
subs := client.subMap
for _, sub := range subs {
subInfo.Topics = append(subInfo.Topics, string(sub.topic))
subInfo.Topics = append(subInfo.Topics, sub.topic)
subInfo.Qoss = append(subInfo.Qoss, sub.qos)
}
}
Expand Down Expand Up @@ -593,17 +615,20 @@ func (b *Broker) removeClient(c *client) {
}

func (b *Broker) PublishMessage(packet *packets.PublishPacket) {
topic := packet.TopicName
r := b.sl.Match(topic)
if len(r.psubs) == 0 {
var subs []interface{}
var qoss []byte
err := b.topicsMgr.Subscribers([]byte(packet.TopicName), packet.Qos, &subs, &qoss)
if err != nil {
log.Error("search sub client error, ", zap.Error(err))
return
}

for _, sub := range r.psubs {
if sub != nil {
err := sub.client.WriterPacket(packet)
for _, sub := range subs {
s, ok := sub.(*subscription)
if ok {
err := s.client.WriterPacket(packet)
if err != nil {
log.Error("process message for psub error, ", zap.Error(err))
log.Error("write message error, ", zap.Error(err))
}
}
}
Expand All @@ -620,3 +645,12 @@ func (b *Broker) BroadcastUnSubscribe(subs map[string]*subscription) {
b.BroadcastSubOrUnsubMessage(unsub)
}
}

func (b *Broker) OnlineOfflineNotification(clientID string, online bool) {
packet := packets.NewControlPacket(packets.Publish).(*packets.PublishPacket)
packet.TopicName = "$SYS/broker/connection/clients/" + clientID
packet.Qos = 0
packet.Payload = []byte(fmt.Sprintf(`{"clientID":"%s","online":%v,"timestamp":"%s"}`, clientID, online, time.Now().UTC().Format(time.RFC3339)))

b.PublishMessage(packet)
}
Loading

0 comments on commit 7547ad3

Please sign in to comment.