Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

add filter function on message process #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion service/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ func (this *service) processor() {

func (this *service) processIncoming(msg message.Message) error {
var err error = nil

if this.messageFilter != nil {
err = this.messageFilter(this.sess.Cmsg, msg)
if err != nil {
return err
}
}
switch msg := msg.(type) {
case *message.PublishMessage:
// For PUBLISH message, we should figure out what QoS it is and process accordingly
Expand Down
16 changes: 11 additions & 5 deletions service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ const (
DefaultTopicsProvider = "mem"
)

type (
MessageFilterFunc func(cmsg *message.ConnectMessage, msg message.Message) error
)

// Server is a library implementation of the MQTT server that, as best it can, complies
// with the MQTT 3.1 and 3.1.1 specs.
type Server struct {
Expand Down Expand Up @@ -109,8 +113,9 @@ type Server struct {
// A indicator on whether this server has already checked configuration
configOnce sync.Once

subs []interface{}
qoss []byte
subs []interface{}
qoss []byte
MessageFilter MessageFilterFunc
}

// ListenAndServe listents to connections on the URI requested, and handles any
Expand Down Expand Up @@ -310,9 +315,10 @@ func (this *Server) handleConnection(c io.Closer) (svc *service, err error) {
ackTimeout: this.AckTimeout,
timeoutRetries: this.TimeoutRetries,

conn: conn,
sessMgr: this.sessMgr,
topicsMgr: this.topicsMgr,
conn: conn,
sessMgr: this.sessMgr,
topicsMgr: this.topicsMgr,
messageFilter: this.MessageFilter,
}

err = this.getSession(svc, req, resp)
Expand Down
7 changes: 4 additions & 3 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ type service struct {
intmp []byte
outtmp []byte

subs []interface{}
qoss []byte
rmsgs []*message.PublishMessage
subs []interface{}
qoss []byte
rmsgs []*message.PublishMessage
messageFilter MessageFilterFunc
}

func (this *service) start() error {
Expand Down