2019-11-18 20:33:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 IBM Corp.
|
|
|
|
*
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* Allan Stockdill-Mander
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mqtt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/eclipse/paho.mqtt.golang/packets"
|
|
|
|
)
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// PacketAndToken is a struct that contains both a ControlPacket and a
|
|
|
|
// Token. This struct is passed via channels between the client interface
|
|
|
|
// code and the underlying code responsible for sending and receiving
|
|
|
|
// MQTT messages.
|
2019-11-18 20:33:15 +03:00
|
|
|
type PacketAndToken struct {
|
|
|
|
p packets.ControlPacket
|
2020-09-23 02:43:58 +03:00
|
|
|
t tokenCompletor
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// Token defines the interface for the tokens used to indicate when
|
|
|
|
// actions have completed.
|
2019-11-18 20:33:15 +03:00
|
|
|
type Token interface {
|
2021-02-04 00:30:55 +03:00
|
|
|
// Wait will wait indefinitely for the Token to complete, ie the Publish
|
|
|
|
// to be sent and confirmed receipt from the broker.
|
2019-11-18 20:33:15 +03:00
|
|
|
Wait() bool
|
2021-02-04 00:30:55 +03:00
|
|
|
|
|
|
|
// WaitTimeout takes a time.Duration to wait for the flow associated with the
|
|
|
|
// Token to complete, returns true if it returned before the timeout or
|
|
|
|
// returns false if the timeout occurred. In the case of a timeout the Token
|
|
|
|
// does not have an error set in case the caller wishes to wait again.
|
2019-11-18 20:33:15 +03:00
|
|
|
WaitTimeout(time.Duration) bool
|
2021-02-04 00:30:55 +03:00
|
|
|
|
|
|
|
// Done returns a channel that is closed when the flow associated
|
|
|
|
// with the Token completes. Clients should call Error after the
|
|
|
|
// channel is closed to check if the flow completed successfully.
|
|
|
|
//
|
|
|
|
// Done is provided for use in select statements. Simple use cases may
|
|
|
|
// use Wait or WaitTimeout.
|
|
|
|
Done() <-chan struct{}
|
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
Error() error
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
type TokenErrorSetter interface {
|
|
|
|
setError(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type tokenCompletor interface {
|
|
|
|
Token
|
|
|
|
TokenErrorSetter
|
|
|
|
flowComplete()
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
type baseToken struct {
|
|
|
|
m sync.RWMutex
|
|
|
|
complete chan struct{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:30:55 +03:00
|
|
|
// Wait implements the Token Wait method.
|
2019-11-18 20:33:15 +03:00
|
|
|
func (b *baseToken) Wait() bool {
|
2020-09-23 02:43:58 +03:00
|
|
|
<-b.complete
|
|
|
|
return true
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:30:55 +03:00
|
|
|
// WaitTimeout implements the Token WaitTimeout method.
|
2019-11-18 20:33:15 +03:00
|
|
|
func (b *baseToken) WaitTimeout(d time.Duration) bool {
|
2020-09-23 02:43:58 +03:00
|
|
|
timer := time.NewTimer(d)
|
|
|
|
select {
|
|
|
|
case <-b.complete:
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
2020-09-23 02:43:58 +03:00
|
|
|
return true
|
|
|
|
case <-timer.C:
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
2020-09-23 02:43:58 +03:00
|
|
|
|
|
|
|
return false
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:30:55 +03:00
|
|
|
// Done implements the Token Done method.
|
|
|
|
func (b *baseToken) Done() <-chan struct{} {
|
|
|
|
return b.complete
|
|
|
|
}
|
|
|
|
|
2019-11-18 20:33:15 +03:00
|
|
|
func (b *baseToken) flowComplete() {
|
2020-09-23 02:43:58 +03:00
|
|
|
select {
|
|
|
|
case <-b.complete:
|
|
|
|
default:
|
|
|
|
close(b.complete)
|
|
|
|
}
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *baseToken) Error() error {
|
|
|
|
b.m.RLock()
|
|
|
|
defer b.m.RUnlock()
|
|
|
|
return b.err
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
func (b *baseToken) setError(e error) {
|
|
|
|
b.m.Lock()
|
|
|
|
b.err = e
|
|
|
|
b.flowComplete()
|
|
|
|
b.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func newToken(tType byte) tokenCompletor {
|
2019-11-18 20:33:15 +03:00
|
|
|
switch tType {
|
|
|
|
case packets.Connect:
|
|
|
|
return &ConnectToken{baseToken: baseToken{complete: make(chan struct{})}}
|
|
|
|
case packets.Subscribe:
|
|
|
|
return &SubscribeToken{baseToken: baseToken{complete: make(chan struct{})}, subResult: make(map[string]byte)}
|
|
|
|
case packets.Publish:
|
|
|
|
return &PublishToken{baseToken: baseToken{complete: make(chan struct{})}}
|
|
|
|
case packets.Unsubscribe:
|
|
|
|
return &UnsubscribeToken{baseToken: baseToken{complete: make(chan struct{})}}
|
|
|
|
case packets.Disconnect:
|
|
|
|
return &DisconnectToken{baseToken: baseToken{complete: make(chan struct{})}}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// ConnectToken is an extension of Token containing the extra fields
|
|
|
|
// required to provide information about calls to Connect()
|
2019-11-18 20:33:15 +03:00
|
|
|
type ConnectToken struct {
|
|
|
|
baseToken
|
2020-09-23 02:43:58 +03:00
|
|
|
returnCode byte
|
|
|
|
sessionPresent bool
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 00:30:55 +03:00
|
|
|
// ReturnCode returns the acknowledgement code in the connack sent
|
2020-09-23 02:43:58 +03:00
|
|
|
// in response to a Connect()
|
2019-11-18 20:33:15 +03:00
|
|
|
func (c *ConnectToken) ReturnCode() byte {
|
|
|
|
c.m.RLock()
|
|
|
|
defer c.m.RUnlock()
|
|
|
|
return c.returnCode
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// SessionPresent returns a bool representing the value of the
|
|
|
|
// session present field in the connack sent in response to a Connect()
|
|
|
|
func (c *ConnectToken) SessionPresent() bool {
|
|
|
|
c.m.RLock()
|
|
|
|
defer c.m.RUnlock()
|
|
|
|
return c.sessionPresent
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishToken is an extension of Token containing the extra fields
|
|
|
|
// required to provide information about calls to Publish()
|
2019-11-18 20:33:15 +03:00
|
|
|
type PublishToken struct {
|
|
|
|
baseToken
|
|
|
|
messageID uint16
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// MessageID returns the MQTT message ID that was assigned to the
|
|
|
|
// Publish packet when it was sent to the broker
|
2019-11-18 20:33:15 +03:00
|
|
|
func (p *PublishToken) MessageID() uint16 {
|
|
|
|
return p.messageID
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// SubscribeToken is an extension of Token containing the extra fields
|
|
|
|
// required to provide information about calls to Subscribe()
|
2019-11-18 20:33:15 +03:00
|
|
|
type SubscribeToken struct {
|
|
|
|
baseToken
|
|
|
|
subs []string
|
|
|
|
subResult map[string]byte
|
2021-02-04 00:30:55 +03:00
|
|
|
messageID uint16
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// Result returns a map of topics that were subscribed to along with
|
|
|
|
// the matching return code from the broker. This is either the Qos
|
|
|
|
// value of the subscription or an error code.
|
2019-11-18 20:33:15 +03:00
|
|
|
func (s *SubscribeToken) Result() map[string]byte {
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
return s.subResult
|
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// UnsubscribeToken is an extension of Token containing the extra fields
|
|
|
|
// required to provide information about calls to Unsubscribe()
|
2019-11-18 20:33:15 +03:00
|
|
|
type UnsubscribeToken struct {
|
|
|
|
baseToken
|
2021-02-04 00:30:55 +03:00
|
|
|
messageID uint16
|
2019-11-18 20:33:15 +03:00
|
|
|
}
|
|
|
|
|
2020-09-23 02:43:58 +03:00
|
|
|
// DisconnectToken is an extension of Token containing the extra fields
|
|
|
|
// required to provide information about calls to Disconnect()
|
2019-11-18 20:33:15 +03:00
|
|
|
type DisconnectToken struct {
|
|
|
|
baseToken
|
|
|
|
}
|