dean

package module
v0.0.0-...-94d6f8d Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: BSD-3-Clause Imports: 20 Imported by: 37

README

Dean

Go Reference Go Report Card

Gopher Thing

Dean is a 100% Go framework. It's currently under develoment.

Dean wraps a Server around a Thing. The Server extends Go's net/http web server. The Thing has a ServeHTTP() handler, subscribed message handlers, and a Run loop.

Dean also works with TinyGo*. You can deploy a Thing on a microcontroller using TinyGo.

Example

There is an example in examples/main.go. To run:

go run examples/main.go

This will start a web server on port :8080. Open a browser to http://localhost:8080.

To run on a microcontroller with TinyGo*:

tinygo flash -monitor -target nano-rp2004 -stack-size 4KB ~/work/dean/example/

This will start a web server on port :8080. Open a browser to http://[IP addr]:8080, where [IP addr] is the microcontroller's IP address.

* requires netdev PRs to TinyGo

Documentation

Index

Constants

View Source
const (
	// Socket is broadcast-ready.  If flag is not set, msgs will not be
	// broadcast on this socket.
	SocketFlagBcast uint32 = 1 << iota
)
View Source
const (
	// ThingFlagMetal indicates thing is running the Run() loop
	ThingFlagMetal uint32 = 1 << iota
)

Variables

This section is empty.

Functions

func GetEnv

func GetEnv(name string, defaultValue string) string

func ThingRestore

func ThingRestore(t Thinger)

func ThingStore

func ThingStore(t Thinger)

func ValidId

func ValidId(s string) bool

ValidId is a non-empty string with only [a-z], [A-Z], [0-9], underscore, or dash characters.

Types

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

Bus is a logical msg broadcast bus. Msgs arrive on sockets connected to the bus. A received msg can be broadcast to the other sockets, or replied back to sender. A socket has a tag, and the bus segregates the sockets by tag. Msgs arriving on a tagged socket will be broadcast only to other sockets with same tag. Think of a tag as a VLAN. The empty tag "" is the default tag on the bus.

func NewBus

func NewBus(name string, connect, disconnect func(Socketer)) *Bus

NewBus returns a new bus with connect and disconnect callbacks

func (*Bus) Handle

func (b *Bus) Handle(tag string, handler func(*Msg)) bool

Handle sets the msg handler for a msg tag

func (*Bus) MaxSockets

func (b *Bus) MaxSockets(maxSockets int)

MaxSockets sets the maximum number of socket connections that can be made to the bus. Any socket connection attempts past the maximum will block until other sockets drop.

func (*Bus) Name

func (b *Bus) Name() string

func (*Bus) Unhandle

func (b *Bus) Unhandle(tag string)

Unhandle removes the msg handle for the msg tag

type CompositeFS

type CompositeFS struct {
	// contains filtered or unexported fields
}

CompositeFS is an ordered (layered) file system, built up from individual file systems

func NewCompositeFS

func NewCompositeFS() *CompositeFS

func (*CompositeFS) AddFS

func (c *CompositeFS) AddFS(fsys fs.ReadFileFS)

AddFS adds fsys to the composite fs. Order matters: first added is lowest in priority when searching for a file name in the composite fs.

func (*CompositeFS) Open

func (c *CompositeFS) Open(name string) (fs.File, error)

Open a file by name

func (*CompositeFS) ParseFS

func (c *CompositeFS) ParseFS(pattern string) *template.Template

ParseFS returns a template by parsing the composite file system for the template name matching the pattern name

func (*CompositeFS) ReadFile

func (c *CompositeFS) ReadFile(name string) ([]byte, error)

Read a file

type Injector

type Injector struct {
	// contains filtered or unexported fields
}

Injector is a one-way socket used for injecting msgs onto the bus. The msgs cannot be sent back (replied) on an injector socket.

func NewInjector

func NewInjector(name string, bus *Bus) *Injector

func (*Injector) Inject

func (i *Injector) Inject(msg *Msg)

Inject a msg onto the bus

type Locker

type Locker interface {
	Lock()
	Unlock()
}

type Maker

type Maker interface {
	ThingMaker
}

Maker can make a Thing

type Makers

type Makers map[string]ThingMaker

Makers is a map of ThinkMakers, keyed by model

type Msg

type Msg struct {
	// contains filtered or unexported fields
}

Msg is sent and received on a bus via a socket

func (*Msg) Broadcast

func (m *Msg) Broadcast() *Msg

Broadcast the msg to all other matching-tagged sockets on the bus. The source socket is excluded.

func (*Msg) Bytes

func (m *Msg) Bytes() []byte

Bytes returns the msg payload

func (*Msg) Marshal

func (m *Msg) Marshal(v any) *Msg

Marshal the msg payload as JSON from v

func (*Msg) Reply

func (m *Msg) Reply() *Msg

Reply sends the msg back to sender. The msg can be modified before calling Reply.

func (*Msg) String

func (m *Msg) String() string

func (*Msg) Unmarshal

func (m *Msg) Unmarshal(v any) *Msg

Unmarshal the msg payload as JSON into v

type Runner

type Runner struct {
	// contains filtered or unexported fields
}

Runner runs a Thing

func NewRunner

func NewRunner(thinger Thinger, user, passwd string) *Runner

func (*Runner) Dial

func (r *Runner) Dial(url *url.URL, tries int) Socketer

func (*Runner) Dials

func (r *Runner) Dials(durls string)

func (*Runner) Run

func (r *Runner) Run()

Run the main run loop

type Server

type Server struct {
	http.Server
	// contains filtered or unexported fields
}

Server serves a Thing

func NewServer

func NewServer(thinger Thinger, user, passwd, port string) *Server

NewServer returns a server, serving the Thinger

func (*Server) AdoptThing

func (s *Server) AdoptThing(thinger Thinger) error

AdoptThing adds a thing to server

func (*Server) CreateThing

func (s *Server) CreateThing(id, model, name string) (Thinger, error)

CreateThing creates a new Thing based on model

func (*Server) DeleteThing

func (s *Server) DeleteThing(id string) error

DeleteThing deletes a Thing given id

func (*Server) Dial

func (s *Server) Dial(url *url.URL, tries int) Socketer

func (*Server) Dials

func (s *Server) Dials(durls string)

func (*Server) GetModels

func (s *Server) GetModels() []string

GetModels returns a list of register models

func (*Server) Handle

func (s *Server) Handle(path string, handler http.Handler)

HandleFunc registers an http handler for path

func (*Server) HandleFunc

func (s *Server) HandleFunc(path string, handler http.HandlerFunc)

HandleFunc registers an http handler func for path

func (*Server) MaxSockets

func (s *Server) MaxSockets(maxSockets int)

MaxSocket sets the maximum number of sockets that can connect to the server

func (*Server) NewInjector

func (s *Server) NewInjector(id string) *Injector

func (*Server) RegisterModel

func (s *Server) RegisterModel(model string, maker ThingMaker)

RegisterModel registers a new Thing model

func (*Server) Run

func (s *Server) Run()

Run the server

func (*Server) ServeTLS

func (s *Server) ServeTLS(host string) error

func (*Server) Unhandle

func (s *Server) Unhandle(path string)

Unhandle unregisters the http handler (or func) for path

func (*Server) UnregisterModel

func (s *Server) UnregisterModel(model string)

UnregisterModel unregisters the Thing model

type Socketer

type Socketer interface {
	// Close the socket
	Close()
	// Send the msg on the socket
	Send(*Msg) error
	// Name of socket
	String() string
	// Tag returns the socket tag
	Tag() string
	// SetTag set the socket tag.  A socket tag is like a VLAN ID.
	SetTag(string)
	// SetFlag on socket
	SetFlag(uint32)
	// TestFlag returns true if flag is set
	TestFlag(uint32) bool
}

Socketer defines a socket interface

type Subscribers

type Subscribers map[string]func(*Msg)

type Thing

type Thing struct {
	Path   string
	Id     string
	Model  string
	Name   string
	Online bool
	// contains filtered or unexported fields
}

Thing implements Thinger and is the base structure for building things

func NewThing

func NewThing(id, model, name string) Thing

func (*Thing) Announce

func (t *Thing) Announce() *Msg

Announce returns an announcement msg. The announcement msg identifies the Thing.

func (*Thing) FailSafe

func (t *Thing) FailSafe()

func (*Thing) Identity

func (t *Thing) Identity() (string, string, string)

func (*Thing) IsMetal

func (t *Thing) IsMetal() bool

func (*Thing) IsOnline

func (t *Thing) IsOnline() bool

func (*Thing) Lock

func (t *Thing) Lock()

func (*Thing) Run

func (t *Thing) Run(*Injector)

func (*Thing) SetFlag

func (t *Thing) SetFlag(flag uint32)

func (*Thing) SetOnline

func (t *Thing) SetOnline(online bool)

func (*Thing) Setup

func (t *Thing) Setup()

func (*Thing) String

func (t *Thing) String() string

func (*Thing) Subscribers

func (t *Thing) Subscribers() Subscribers

func (*Thing) TestFlag

func (t *Thing) TestFlag(flag uint32) bool

func (*Thing) Unlock

func (t *Thing) Unlock()

type ThingMaker

type ThingMaker func(id, model, name string) Thinger

ThingMaker returns a Thinger

type ThingMsg

type ThingMsg struct {
	Path string
}

ThingMsg is the prototypical msg. All msgs have the Path member.

type ThingMsgAdopted

type ThingMsgAdopted struct {
	Path  string
	Id    string
	Model string
	Name  string
}

ThingMsgeAdopted is sent when as new Thing is adopted on a server

type ThingMsgAnnounce

type ThingMsgAnnounce struct {
	Path  string
	Id    string
	Model string
	Name  string
}

ThingMsgAnnounce is sent to annouce a Thing to a server

type ThingMsgConnect

type ThingMsgConnect struct {
	Path  string
	Id    string
	Model string
	Name  string
}

ThingMsgConnect is sent when a Thing connects to a server

type ThingMsgCreated

type ThingMsgCreated struct {
	Path  string
	Id    string
	Model string
	Name  string
}

ThingMsgCreated is sent when a new Thing is created on a server

type ThingMsgDeleted

type ThingMsgDeleted struct {
	Path string
	Id   string
}

ThingMsgDeleted is sent when Thing is deleted from a server

type ThingMsgDisconnect

type ThingMsgDisconnect struct {
	Path string
	Id   string
}

ThingMsgDisconnect is sent when a Thing disconnects from a server

type Thinger

type Thinger interface {
	Subscribers() Subscribers
	Announce() *Msg
	Setup()
	Run(*Injector)
	FailSafe()
	Identity() (string, string, string)
	IsOnline() bool
	SetOnline(bool)
	String() string
	SetFlag(uint32)
	TestFlag(uint32) bool
}

Thinger defines a thing interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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