tt

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 20 Imported by: 0

README

Build Status codecov Maintainability

gregoryv/tt - package provides components for writing mqtt-v5.0 clients and servers

Quick start

$ go install github.com/gregoryv/tt/cmd/tt@latest
$ tt -h

Even though this repository provides a basic client/server(WIP) command it's main purpose is to provide capabilities for others to write their own clients depending on their situation.

NOTE! this module is still not ready for public use. I'm experimenting with the design, so major changes will occur.

This package uses the sibling package gregoryv/mq for encoding control packets on the wire.

The design focuses on decoupling each specific feature as much as possible. One example being the creation of a network connection is not enforced, which makes it easy to create inmemory clients and servers during testing.

Documentation

Overview

Package tt provides components for writing mqtt-v5 clients.

Example (Client)

Example shows a simple client for connect, publish a QoS 0 and disconnect.

package main

import (
	"context"

	"github.com/gregoryv/mq"
	"github.com/gregoryv/tt"
	"github.com/gregoryv/tt/event"
)

func main() {
	client := tt.Client{
		Server: "tcp://localhost:1883",
	}
	ctx := context.Background()
	client.Start(ctx)

	// v is either an packet or a event type
	for v := range client.Signal() {
		switch v := v.(type) {
		case event.ClientUp:
			_ = client.Send(ctx, mq.NewConnect())

		case event.ClientConnect:
			// do something once you are connected
			p := mq.Pub(0, "gopher/happy", "yes")
			_ = client.Send(ctx, p)

		case *mq.Publish:
			_ = v // do something the received packet

		case event.ClientStop:
			// do some clean up maybe
		}
	}
}
Output:

Example (Server)

Example shows how to run the provided server.

package main

import (
	"context"

	"github.com/gregoryv/tt"
	"github.com/gregoryv/tt/event"
)

func main() {
	var srv tt.Server
	ctx, cancel := context.WithCancel(context.Background())
	srv.Start(ctx)

	for v := range srv.Signal() {
		switch v.(type) {
		case event.ServerStop:
			cancel()
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClientStopped = fmt.Errorf("Client stopped")
View Source
var ErrIDPoolEmpty = fmt.Errorf("no available packet ids")

Functions

This section is empty.

Types

type Bind added in v0.10.0

type Bind struct {
	// eg. tcp://localhost:
	URL string

	// eg. 500ms
	AcceptTimeout string
}

Bind holds server listening settings

type Client added in v0.9.0

type Client struct {
	// Server to connect to, defaults to tcp://localhost:1883
	Server string

	// Set to true to include more log output
	Debug bool

	// optional logger, leave empty for no logging
	*log.Logger `json:-`

	// Outgoing packets use ids from 1..MaxPacketID, this limits the
	// number of packets in flight.
	MaxPacketID uint16

	// show settings on client.Run
	ShowSettings bool
	// contains filtered or unexported fields
}

Client implements a mqtt-v5 client. Public fields should be set before calling Run.

func (*Client) Send added in v0.9.0

func (c *Client) Send(ctx context.Context, p mq.Packet) error

Send returns when the packet was successfully encoded on the wire. Returns ErrClientStopped if not running.

func (*Client) Signal added in v0.10.0

func (c *Client) Signal() <-chan interface{}

Signal returns a channel used by client to inform application layer of packets and events. E.g. event.ClientUp

func (*Client) Start added in v0.10.0

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

Start returns a channel where client pushes incoming packets or events.

type Server added in v0.2.0

type Server struct {
	// bind configuration where server listens for connections, empty
	// defaults to random port on localhost
	Binds []*Bind

	// client has to send the initial connect packet, default 200ms
	ConnectTimeout time.Duration

	// set to true for additional log information
	Debug bool

	ShowSettings bool
	// contains filtered or unexported fields
}

func (*Server) SetLogger added in v0.10.0

func (s *Server) SetLogger(v *log.Logger)

func (*Server) Signal added in v0.10.0

func (s *Server) Signal() <-chan interface{}

Signal returns a channel used by server to inform the application layer of events. E.g event.ServerStop

func (*Server) Start added in v0.10.0

func (s *Server) Start(ctx context.Context)

Start runs the server in a separate go routine. Use Server.Signal

Directories

Path Synopsis
Package arn provides an MQTT topic filter matcher.
Package arn provides an MQTT topic filter matcher.
cmd
tt
Command tt is a mqtt pub/sub client and broker
Command tt is a mqtt pub/sub client and broker
Command feature documents tt features
Command feature documents tt features
Package event provides client and server event types.
Package event provides client and server event types.
Package ttx provides test types
Package ttx provides test types

Jump to

Keyboard shortcuts

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