paho

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2021 License: EPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultKeepAlive       = 60 * time.Second
	DefaultShutdownTimeout = 10 * time.Second
	DefaultPacketTimeout   = 10 * time.Second
)
View Source
var (
	ErrClosed  = fmt.Errorf("paho: client closed")
	ErrTimeout = fmt.Errorf("paho: request timeout")
)
View Source
var ErrNoMoreIDs = errors.New("no more packet ids available.")

ErrNoMoreIDs is an error returned when there are no more available packet IDs.

Functions

func BoolToByte

func BoolToByte(b bool) *byte

BoolToByte is a helper function that take a bool and returns a pointer to a byte of value 1 if true or 0 if false

func Byte

func Byte(b byte) *byte

Byte is a helper function that take a byte and returns a pointer to a byte of that value

func Uint16

func Uint16(u uint16) *uint16

Uint16 is a helper function that take a uint16 and returns a pointer to a uint16 of that value

func Uint32

func Uint32(u uint32) *uint32

Uint32 is a helper function that take a uint32 and returns a pointer to a uint32 of that value

Types

type Auth

type Auth struct {
	Properties *AuthProperties
	ReasonCode byte
}

Auth is a representation of the MQTT Auth packet

func AuthFromPacketAuth

func AuthFromPacketAuth(a *packets.Auth) *Auth

AuthFromPacketAuth takes a packets library Auth and returns a paho library Auth

func (*Auth) InitProperties

func (a *Auth) InitProperties(p *packets.Properties)

InitProperties is a function that takes a lower level Properties struct and completes the properties of the Auth on which it is called

func (*Auth) Packet

func (a *Auth) Packet() *packets.Auth

Packet returns a packets library Auth from the paho Auth on which it is called

type AuthProperties

type AuthProperties struct {
	AuthData     []byte
	AuthMethod   string
	ReasonString string
	User         map[string]string
}

AuthProperties is a struct of the properties that can be set for a Auth packet

type AuthResponse

type AuthResponse struct {
	Properties *AuthProperties
	ReasonCode byte
	Success    bool
}

AuthResponse is a represenation of the response to an Auth packet

func AuthResponseFromPacketAuth

func AuthResponseFromPacketAuth(a *packets.Auth) *AuthResponse

AuthResponseFromPacketAuth takes a packets library Auth and returns a paho library AuthResponse

func AuthResponseFromPacketDisconnect

func AuthResponseFromPacketDisconnect(d *packets.Disconnect) *AuthResponse

AuthResponseFromPacketDisconnect takes a packets library Disconnect and returns a paho library AuthResponse

type Auther

type Auther interface {
	Authenticate(*Auth) *Auth
	Authenticated()
}

Auther is the interface for something that implements the extended authentication flows in MQTT v5.

type CPContext

type CPContext struct {
	Context context.Context
	Return  chan packets.ControlPacket
}

CPContext is the struct that is used to return responses to ControlPackets that have them, eg: the suback to a subscribe. The response packet is send down the Return channel and the Context is used to track timeouts.

type Client

type Client struct {
	ClientConfig
	// contains filtered or unexported fields
}

Client is the struct representing an MQTT client

func NewClient

func NewClient(conf ClientConfig) *Client

NewClient is used to create a new default instance of an MQTT client. It returns a pointer to the new client instance. The default client uses the provided MessageID and StandardRouter implementations, and a noop Persistence. These should be replaced if desired before the client is connected. client.Conn *MUST* be set to an already connected net.Conn before Connect() is called.

func (*Client) Authenticate

func (c *Client) Authenticate(ctx context.Context, a *Auth) (*AuthResponse, error)

Authenticate is used to initiate a reauthentication of credentials with the server. This function sends the initial Auth packet to start the reauthentication then relies on the client AuthHandler managing any further requests from the server until either a successful Auth packet is passed back, or a Disconnect is received.

func (*Client) Close

func (c *Client) Close()

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error)

Connect is used to connect the client to a server. It presumes that the Client instance already has a working network connection. The function takes a pre-prepared Connect packet, and uses that to establish an MQTT connection. Assuming the connection completes successfully the rest of the client is initiated and the Connack returned. Otherwise the failure Connack (if there is one) is returned along with an error indicating the reason for the failure to connect.

func (*Client) Disconnect

func (c *Client) Disconnect(ctx context.Context, d *Disconnect) error

Disconnect is used to send a Disconnect packet to the MQTT server Whether or not the attempt to send the Disconnect packet fails (and if it does this function returns any error) the network connection is .

func (*Client) Done

func (c *Client) Done() <-chan struct{}

func (*Client) IsAlive

func (c *Client) IsAlive() bool

func (*Client) Publish

func (c *Client) Publish(ctx context.Context, p *Publish) (_ *PublishResponse, err error)

Publish is used to send a publication to the MQTT server. It is passed a pre-prepared Publish packet and blocks waiting for the appropriate response, or for the timeout to fire. Any response message is returned from the function, along with any errors.

func (*Client) Shutdown

func (c *Client) Shutdown(ctx context.Context)

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, s *Subscribe) (*Suback, error)

Subscribe is used to send a Subscription request to the MQTT server. It is passed a pre-prepared Subscribe packet and blocks waiting for a response Suback, or for the timeout to fire. Any response Suback is returned from the function, along with any errors.

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(ctx context.Context, u *Unsubscribe) (*Unsuback, error)

Unsubscribe is used to send an Unsubscribe request to the MQTT server. It is passed a pre-prepared Unsubscribe packet and blocks waiting for a response Unsuback, or for the timeout to fire. Any response Unsuback is returned from the function, along with any errors.

type ClientConfig

type ClientConfig struct {
	Conn            net.Conn
	MIDs            MIDService
	AuthHandler     Auther
	Router          Router
	Persistence     Persistence
	PacketTimeout   time.Duration
	ShutdownTimeout time.Duration
	Trace           Trace
	Logger          func(context.Context, LogEntry)
	OnClose         func()
}

ClientConfig are the user configurable options for the client, an instance of this struct is passed into NewClient(), not all options are required to be set, defaults are provided for Persistence, MIDs, PacketTimeout and Router.

type CommsProperties

type CommsProperties struct {
	MaximumPacketSize    uint32
	ReceiveMaximum       uint16
	TopicAliasMaximum    uint16
	MaximumQoS           byte
	RetainAvailable      bool
	WildcardSubAvailable bool
	SubIDAvailable       bool
	SharedSubAvailable   bool
}

CommsProperties is a struct of the communication properties that may be set by the server in the Connack and that the client needs to be aware of for future subscribes/publishes

type Connack

type Connack struct {
	Properties     *ConnackProperties
	ReasonCode     byte
	SessionPresent bool
}

Connack is a representation of the MQTT Connack packet

func ConnackFromPacketConnack

func ConnackFromPacketConnack(c *packets.Connack) *Connack

ConnackFromPacketConnack takes a packets library Connack and returns a paho library Connack

func (*Connack) InitProperties

func (c *Connack) InitProperties(p *packets.Properties)

InitProperties is a function that takes a lower level Properties struct and completes the properties of the Connack on which it is called

type ConnackProperties

type ConnackProperties struct {
	AuthData             []byte
	AuthMethod           string
	ResponseInfo         string
	ServerReference      string
	ReasonString         string
	AssignedClientID     string
	MaximumPacketSize    *uint32
	ReceiveMaximum       *uint16
	TopicAliasMaximum    *uint16
	ServerKeepAlive      *uint16
	MaximumQoS           *byte
	User                 map[string]string
	WildcardSubAvailable bool
	SubIDAvailable       bool
	SharedSubAvailable   bool
	RetainAvailable      bool
}

ConnackProperties is a struct of the properties that can be set for a Connack packet

type Connect

type Connect struct {
	Password       []byte
	Username       string
	ClientID       string
	Properties     *ConnectProperties
	WillMessage    *WillMessage
	WillProperties *WillProperties
	KeepAlive      uint16
	CleanStart     bool
	UsernameFlag   bool
	PasswordFlag   bool
}

Connect is a representation of the MQTT Connect packet

func ConnectFromPacketConnect

func ConnectFromPacketConnect(p *packets.Connect) *Connect

ConnectFromPacketConnect takes a packets library Connect and returns a paho library Connect

func (*Connect) InitProperties

func (c *Connect) InitProperties(p *packets.Properties)

InitProperties is a function that takes a lower level Properties struct and completes the properties of the Connect on which it is called

func (*Connect) InitWillProperties

func (c *Connect) InitWillProperties(p *packets.Properties)

InitWillProperties is a function that takes a lower level Properties struct and completes the properties of the Will in the Connect on which it is called

func (*Connect) Packet

func (c *Connect) Packet() *packets.Connect

Packet returns a packets library Connect from the paho Connect on which it is called

type ConnectProperties

type ConnectProperties struct {
	AuthData              []byte
	AuthMethod            string
	SessionExpiryInterval *uint32
	WillDelayInterval     *uint32
	ReceiveMaximum        *uint16
	TopicAliasMaximum     *uint16
	MaximumQOS            *byte
	MaximumPacketSize     *uint32
	User                  map[string]string
	RequestProblemInfo    bool
	RequestResponseInfo   bool
}

ConnectProperties is a struct of the properties that can be set for a Connect packet

type Disconnect

type Disconnect struct {
	Properties *DisconnectProperties
	ReasonCode byte
}

Disconnect is a representation of the MQTT Disconnect packet

func DisconnectFromPacketDisconnect

func DisconnectFromPacketDisconnect(p *packets.Disconnect) *Disconnect

DisconnectFromPacketDisconnect takes a packets library Disconnect and returns a paho library Disconnect

func (*Disconnect) InitProperties

func (d *Disconnect) InitProperties(p *packets.Properties)

InitProperties is a function that takes a lower level Properties struct and completes the properties of the Disconnect on which it is called

func (*Disconnect) Packet

func (d *Disconnect) Packet() *packets.Disconnect

Packet returns a packets library Disconnect from the paho Disconnect on which it is called

type DisconnectProperties

type DisconnectProperties struct {
	ServerReference       string
	ReasonString          string
	SessionExpiryInterval *uint32
	User                  map[string]string
}

DisconnectProperties is a struct of the properties that can be set for a Disconnect packet

type LogEntry

type LogEntry struct {
	Level         LogLevel
	Message       string
	Error         error
	ControlPacket *packets.ControlPacket
}

type LogLevel

type LogLevel byte
const (
	LevelTrace LogLevel
	LevelDebug
	LevelWarn
	LevelError
)

type MIDService

type MIDService interface {
	Request(*CPContext) (uint16, error)
	Get(uint16) *CPContext
	Free(uint16)
	Clear()
}

MIDService defines the interface for a struct that handles the relationship between message ids and CPContexts Request() takes a *CPContext and returns a uint16 that is the messageid that should be used by the code that called Request() Get() takes a uint16 that is a messageid and returns the matching *CPContext that the MIDService has associated with that messageid Free() takes a uint16 that is a messageid and instructs the MIDService to mark that messageid as available for reuse Clear() resets the internal state of the MIDService

type MIDs

type MIDs struct {
	sync.Mutex
	// contains filtered or unexported fields
}

MIDs is the default MIDService provided by this library. It uses a map of uint16 to *CPContext to track responses to messages with a messageid

func (*MIDs) Clear

func (m *MIDs) Clear()

Clear is the library provided MIDService's implementation of the required interface function()

func (*MIDs) Free

func (m *MIDs) Free(i uint16)

Free is the library provided MIDService's implementation of the required interface function()

func (*MIDs) Get

func (m *MIDs) Get(i uint16) *CPContext

Get is the library provided MIDService's implementation of the required interface function()

func (*MIDs) Request

func (m *MIDs) Request(c *CPContext) (uint16, error)

Request is the library provided MIDService's implementation of the required interface function()

type MemoryPersistence

type MemoryPersistence struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

MemoryPersistence is an implementation of a Persistence that stores the ControlPackets in memory using a map

func (*MemoryPersistence) All

All is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Close

func (m *MemoryPersistence) Close()

Close is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Delete

func (m *MemoryPersistence) Delete(id uint16)

Delete is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Get

Get is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Open

func (m *MemoryPersistence) Open()

Open is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Put

Put is the library provided MemoryPersistence's implementation of the required interface function()

func (*MemoryPersistence) Reset

func (m *MemoryPersistence) Reset()

Reset is the library provided MemoryPersistence's implementation of the required interface function()

type Persistence

type Persistence interface {
	Open()
	Put(uint16, packets.ControlPacket)
	Get(uint16) packets.ControlPacket
	All() []packets.ControlPacket
	Delete(uint16)
	Close()
	Reset()
}

Persistence is an interface of the functions for a struct that is used to persist ControlPackets. Open() is an initialiser to prepare the Persistence for use Put() takes a uint16 which is a messageid and a ControlPacket to persist against that messageid Get() takes a uint16 which is a messageid and returns the persisted ControlPacket from the Persistence for that messageid All() returns a slice of all ControlPackets persisted Delete() takes a uint16 which is a messageid and deletes the associated stored ControlPacket from the Persistence Close() closes the Persistence Reset() clears the Persistence and prepares it to be reused

type Publish

type Publish struct {
	QoS        byte
	Retain     bool
	Topic      string
	Properties *PublishProperties
	Payload    []byte
}

Publish is a reporesentation of the MQTT Publish packet

func (*Publish) InitProperties

func (p *Publish) InitProperties(prop *packets.Properties)

InitProperties is a function that takes a lower level Properties struct and completes the properties of the Publish on which it is called

func (*Publish) Packet

func (p *Publish) Packet() *packets.Publish

Packet returns a packets library Publish from the paho Publish on which it is called

func (*Publish) String

func (p *Publish) String() string

type PublishDoneTrace

type PublishDoneTrace struct {
	Error error
}

type PublishProperties

type PublishProperties struct {
	CorrelationData        []byte
	ContentType            string
	ResponseTopic          string
	PayloadFormat          *byte
	MessageExpiry          *uint32
	SubscriptionIdentifier *uint32
	TopicAlias             *uint16
	User                   map[string]string
}

PublishProperties is a struct of the properties that can be set for a Publish packet

type PublishResponse

type PublishResponse struct {
	Properties *PublishResponseProperties
	ReasonCode byte
}

PublishResponse is a generic representation of a response to a QoS1 or QoS2 Publish

func PublishResponseFromPuback

func PublishResponseFromPuback(pa *packets.Puback) *PublishResponse

PublishResponseFromPuback takes a packets library Puback and returns a paho library PublishResponse

func PublishResponseFromPubcomp

func PublishResponseFromPubcomp(pc *packets.Pubcomp) *PublishResponse

PublishResponseFromPubcomp takes a packets library Pubcomp and returns a paho library PublishResponse

func PublishResponseFromPubrec

func PublishResponseFromPubrec(pr *packets.Pubrec) *PublishResponse

PublishResponseFromPubrec takes a packets library Pubrec and returns a paho library PublishResponse

type PublishResponseProperties

type PublishResponseProperties struct {
	ReasonString string
	User         map[string]string
}

PublishResponseProperties is the properties associated with a response to a QoS1 or QoS2 Publish

type PublishStartTrace

type PublishStartTrace struct {
	Packet *packets.Publish
	OnDone func(context.Context, PublishDoneTrace)
}

type RecvDoneTrace

type RecvDoneTrace struct {
	Packet     interface{}
	PacketType packets.PacketType
	Error      error
}

type RecvStartTrace

type RecvStartTrace struct {
	OnDone func(context.Context, RecvDoneTrace)
}

type Router

type Router interface {
	Route(pb *packets.Publish, ack func() error)
}

Router is an interface that capable of handling publish packets.

NOTE: its a Router responsibility to deal with concurrent packets processing (if needed).

type RouterFunc

type RouterFunc func(pb *packets.Publish, ack func() error)

RouterFunc is an adapter to allow the use of ordinary functions as Router.

func (RouterFunc) Route

func (f RouterFunc) Route(p *packets.Publish, ack func() error)

Route implements Router interface.

type SendDoneTrace

type SendDoneTrace struct {
	Error error
}

type SendStartTrace

type SendStartTrace struct {
	Packet     interface{}
	PacketType packets.PacketType

	OnDone func(context.Context, SendDoneTrace)
}

type Suback

type Suback struct {
	Properties *SubackProperties
	Reasons    []byte
}

Suback is a representation of an MQTT suback packet

func SubackFromPacketSuback

func SubackFromPacketSuback(s *packets.Suback) *Suback

SubackFromPacketSuback takes a packets library Suback and returns a paho library Suback

func (*Suback) Packet

func (s *Suback) Packet() *packets.Suback

Packet returns a packets library Suback from the paho Suback on which it is called

type SubackProperties

type SubackProperties struct {
	ReasonString string
	User         map[string]string
}

SubackProperties is a struct of the properties that can be set for a Suback packet

type Subscribe

type Subscribe struct {
	Properties    *SubscribeProperties
	Subscriptions map[string]SubscribeOptions
}

Subscribe is a representation of a MQTT subscribe packet

func (*Subscribe) InitProperties

func (s *Subscribe) InitProperties(prop *packets.Properties)

InitProperties is a function that takes a packet library Properties struct and completes the properties of the Subscribe on which it is called

func (*Subscribe) Packet

func (s *Subscribe) Packet() *packets.Subscribe

Packet returns a packets library Subscribe from the paho Subscribe on which it is called

func (*Subscribe) PacketSubOptionsFromSubscribeOptions

func (s *Subscribe) PacketSubOptionsFromSubscribeOptions() map[string]packets.SubOptions

PacketSubOptionsFromSubscribeOptions returns a map of string to packet library SubOptions for the paho Subscribe on which it is called

type SubscribeOptions

type SubscribeOptions struct {
	QoS               byte
	RetainHandling    byte
	NoLocal           bool
	RetainAsPublished bool
}

SubscribeOptions is the struct representing the options for a subscription

type SubscribeProperties

type SubscribeProperties struct {
	SubscriptionIdentifier *uint32
	User                   map[string]string
}

SubscribeProperties is a struct of the properties that can be set for a Subscribe packet

type Trace

type Trace struct {
	OnSend    func(context.Context, *SendStartTrace)
	OnRecv    func(context.Context, *RecvStartTrace)
	OnPublish func(context.Context, *PublishStartTrace)
}

type Unsuback

type Unsuback struct {
	Reasons    []byte
	Properties *UnsubackProperties
}

Unsuback is a representation of an MQTT Unsuback packet

func UnsubackFromPacketUnsuback

func UnsubackFromPacketUnsuback(u *packets.Unsuback) *Unsuback

UnsubackFromPacketUnsuback takes a packets library Unsuback and returns a paho library Unsuback

func (*Unsuback) Packet

func (u *Unsuback) Packet() *packets.Unsuback

Packet returns a packets library Unsuback from the paho Unsuback on which it is called

type UnsubackProperties

type UnsubackProperties struct {
	ReasonString string
	User         map[string]string
}

UnsubackProperties is a struct of the properties that can be set for a Unsuback packet

type Unsubscribe

type Unsubscribe struct {
	Topics     []string
	Properties *UnsubscribeProperties
}

Unsubscribe is a representation of an MQTT unsubscribe packet

func (*Unsubscribe) Packet

func (u *Unsubscribe) Packet() *packets.Unsubscribe

Packet returns a packets library Unsubscribe from the paho Unsubscribe on which it is called

type UnsubscribeProperties

type UnsubscribeProperties struct {
	User map[string]string
}

UnsubscribeProperties is a struct of the properties that can be set for a Unsubscribe packet

type WillMessage

type WillMessage struct {
	Retain  bool
	QoS     byte
	Topic   string
	Payload []byte
}

WillMessage is a representation of the LWT message that can be sent with the Connect packet

type WillProperties

type WillProperties struct {
	WillDelayInterval *uint32
	PayloadFormat     *byte
	MessageExpiry     *uint32
	ContentType       string
	ResponseTopic     string
	CorrelationData   []byte
	User              map[string]string
}

WillProperties is a struct of the properties that can be set for a Will in a Connect packet

Directories

Path Synopsis
cmd
rpc
extensions
rpc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL