Documentation
¶
Overview ¶
Package binding resolves the entries of the node's Binding servers into the peers they direct: matter.js's BindingManager (packages/node/src/behaviors/binding/BindingManager.ts at 0ceb202d), the node-scoped service its BindingServer registers every entry with.
A controller writes a Binding entry on one of our endpoints — "talk to node 2, endpoint 1, cluster OnOff". The Manager hears it through the server's change hook (cluster/core.Binding.OnChange) and resolves it into a Resolution of one of three kinds:
- KindClient: the entry names another node. The manager takes the peer from Peers (client/peer.Set), opens a CASE session to it in the background and reports the resolution, with an interaction.Interaction bound to the peer, once a session exists.
- KindGroup: the entry names a group. It resolves once the fabric holds a key for the group (Topology.HasGroupKey), through the GroupSender — commands to it go out group-addressed and nobody answers.
- KindServer: the entry names another endpoint of this node (a self-binding). It resolves locally; nothing goes on the wire.
The host observes the outcome with Manager.OnEstablished and Manager.OnRemoved. Entries stay dormant — no peer, no CASE session — until something observes OnEstablished (BindingManager.ts:385-438).
An entry the manager cannot use is dropped with a warning whose reason attribute is matter.js's message, verbatim; the write that brought it already succeeded. The manager never closes a CASE session when a binding goes: matter.js's reference counts of bound peers have no consumer (BindingManager.ts:656-677) and are not ported. A peer the manager itself created for an entry — the Peers set held none for the node — is released each time the last entry naming the node goes: the entries' Connect calls end, then its connection process stops; the peer stays in the set, never closed, for another holder may use it, and a later entry for the node is released the same way (ahead of matter.js; review II, R2-P6-bound-2104, owner decision 221).
Index ¶
- Variables
- type Config
- type Entry
- type GroupSender
- type GroupSenderFunc
- type GroupTarget
- type Kind
- type Manager
- func (m *Manager) Attach(endpoint uint16, b *mattercore.Binding)
- func (m *Manager) Attached() []uint16
- func (m *Manager) Close()
- func (m *Manager) Detach(endpoint uint16)
- func (m *Manager) Established() []Resolution
- func (m *Manager) GroupKeysChanged(fabricIndex uint8)
- func (m *Manager) OnEstablished(fn func(Resolution)) (stop func())
- func (m *Manager) OnRemoved(fn func(Resolution)) (stop func())
- func (m *Manager) Register(sourceEndpoint uint16, entry Entry)
- func (m *Manager) Unregister(sourceEndpoint uint16, entry Entry)
- type Peers
- type Resolution
- type Topology
Constants ¶
This section is empty.
Variables ¶
var ErrNoTopology = errors.New("binding: a Manager needs a Topology")
ErrNoTopology is New without a Topology.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Topology is this node; required.
Topology Topology
// Peers gives the peer of a node entry; nil is a node without its
// client stack, whose node entries are dropped.
Peers Peers
// Groups gives the sender of a group entry; nil drops group entries
// once their key exists.
Groups GroupSender
// Interaction configures the interaction of a client resolution.
Interaction interaction.Config
// Logger receives the diagnostics; nil discards them.
Logger *slog.Logger
}
Config configures a Manager.
type Entry ¶
type Entry = mattercore.TargetStruct
Entry is one Binding entry: the Binding cluster's TargetStruct as the server keeps it.
type GroupSender ¶
type GroupSender interface {
Sender(ctx context.Context, fabricIndex uint8, groupID uint16) (GroupTarget, error)
}
GroupSender gives the GroupTarget of a fabric's group — matter.js's group peer `peers.forAddress({fabricIndex, NodeId.fromGroupId(group)})` (BindingManager.ts:458-461).
type GroupSenderFunc ¶
type GroupSenderFunc func(ctx context.Context, fabricIndex uint8, groupID uint16) (GroupTarget, error)
GroupSenderFunc adapts a function to GroupSender.
func (GroupSenderFunc) Sender ¶
func (f GroupSenderFunc) Sender(ctx context.Context, fabricIndex uint8, groupID uint16) (GroupTarget, error)
Sender implements GroupSender.
type GroupTarget ¶
type GroupTarget interface {
Invoke(ctx context.Context, path im.ConcreteCommandPath, fields spec.Encodable) error
}
GroupTarget sends to one group — the IM face of a group peer, matter.js ClientGroupInteraction (packages/node/src/node/client/ ClientGroupInteraction.ts:33-82): invoke only, group-addressed, no answer. The group sender of package groups provides it.
type Kind ¶
type Kind uint8
Kind is what a binding entry resolved to — matter.js BindingResolution's `kind` (BindingManager.ts:25-28).
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager resolves the entries of the node's Binding servers — matter.js BindingManager (BindingManager.ts:46-710). Every change of its state runs on one worker goroutine, in the order it was asked for, as matter.js's single event loop orders it; the OnEstablished and OnRemoved handlers run there too, so a handler that blocks delays the manager's other work — long work belongs in a goroutine of its own. It is safe for concurrent use.
func New ¶
New returns a manager over cfg. Its worker starts with the first work queued for it, so a manager built and dropped runs no goroutine (ADR 0026 principle 5); Manager.Close ends it.
func (*Manager) Attach ¶
func (m *Manager) Attach(endpoint uint16, b *mattercore.Binding)
Attach makes the manager resolve the entries of the Binding server on endpoint: every entry it holds now (BindingServer.ts:120-126) and every entry a later write adds or removes (:127-157). A server already attached to the endpoint is detached first (Manager.Detach); attaching the same server again does nothing.
func (*Manager) Attached ¶
Attached are the endpoints whose Binding server the manager listens to, ascending.
func (*Manager) Close ¶
func (m *Manager) Close()
Close detaches every Binding server, emitting removed for each established binding (BindingManager.disposeServer, :207-244, as on the node's shutdown — BindingServer.ts:52), stops the worker and the background connections, and waits for them. Work asked for before Close runs first; work asked for after it is dropped. Idempotent; not to be called from a handler.
func (*Manager) Detach ¶
Detach stops resolving the entries of endpoint's Binding server and emits removed for each of its established bindings — matter.js BindingManager.disposeServer (BindingManager.ts:191-228), which the server's disposal calls.
func (*Manager) Established ¶
func (m *Manager) Established() []Resolution
Established returns the established bindings, by source endpoint and entry key.
func (*Manager) GroupKeysChanged ¶
GroupKeysChanged tells the manager that a fabric's group key map or key sets changed: the group entries of the fabric that now hold a key resolve. Calls coalesce into one recheck, run after the change is complete — matter.js #scheduleGroupRecheck (BindingManager.ts:513-520).
func (*Manager) OnEstablished ¶
func (m *Manager) OnEstablished(fn func(Resolution)) (stop func())
OnEstablished registers fn to learn every binding established from now on — matter.js BindingServer.events.established (BindingServer.ts: 166-197) — and returns the function that removes it. Entries are dormant while no handler is registered; the first one wakes them (BindingManager.ts:391-438).
func (*Manager) OnRemoved ¶
func (m *Manager) OnRemoved(fn func(Resolution)) (stop func())
OnRemoved registers fn to learn every established binding that is removed — matter.js BindingServer.events.removed (BindingServer.ts: 199-212), also for every live binding on Manager.Detach and Manager.Close — and returns the function that removes it.
func (*Manager) Register ¶
Register resolves entry of the Binding server on sourceEndpoint in the background — matter.js BindingManager.register (BindingManager.ts: 119-129), which records a group entry as registered at once and resolves it later. An entry already registered under its key is left alone.
func (*Manager) Unregister ¶
Unregister forgets entry of the Binding server on sourceEndpoint in the background, emitting removed when it was established — matter.js BindingManager.unregister (BindingManager.ts:147-157).
type Peers ¶
type Peers interface {
For(addr handshake.PeerAddress) (*peer.Peer, error)
}
Peers is where the manager takes the peer of a node entry from — matter.js `node.peers.forAddress` (BindingManager.ts:333); client/peer.Set satisfies it. A Peers that also reports whether it holds a peer (`Has(handshake.PeerAddress) bool`, as client/peer.Set does) lets the manager release the peers it created once no entry names their node any more; with any other Peers it releases none.
type Resolution ¶
type Resolution struct {
Kind Kind
// Entry is the binding entry.
Entry Entry
// SourceEndpoint is the endpoint whose Binding server holds the entry.
SourceEndpoint uint16
// Peer is the bound node's peer (KindClient).
Peer *peer.Peer
// Interaction is the interaction with Peer, connecting when it has no
// session (KindClient).
Interaction *interaction.Interaction
// Endpoint is the target endpoint: the bound node's (KindClient), ours
// (KindServer), or — as matter.js materialises a group's endpoint under
// the source endpoint's number (BindingManager.ts:455 at b25ed960) — the source
// endpoint (KindGroup).
Endpoint uint16
// Group sends to the bound group (KindGroup).
Group GroupTarget
// Clients are the source endpoint's declared client clusters the entry
// directs, bindable ones only — what matter.js installs on the
// resolution's endpoint (BindingManager.ts:339, :472, :579-583).
Clients []uint32
}
Resolution is a binding entry resolved into the peer it directs — matter.js BindingResolution (BindingManager.ts:25-28). Where matter.js hands over a materialised endpoint carrying the declared client behaviors, a Resolution names the target endpoint and the clients.
type Topology ¶
type Topology interface {
// NodeID is our node id on the fabric; false when the fabric is not
// ours — matter.js `fabrics.has` / `fabrics.for(index).nodeId`
// (BindingManager.ts:276, :645-650).
NodeID(fabricIndex uint8) (uint64, bool)
// HasEndpoint reports whether the endpoint exists (:284).
HasEndpoint(endpoint uint16) bool
// HasServer reports whether the endpoint serves the cluster
// (#endpointHasClusterServer, :550-554).
HasServer(endpoint uint16, cluster uint32) bool
// ClientClusters are the endpoint's declared client clusters — its
// type's clientClusters (:566).
ClientClusters(endpoint uint16) []uint32
// HasGroupKey reports whether the fabric maps the group to a key set
// it holds (#holdsGroupKey, :482-490).
HasGroupKey(fabricIndex uint8, groupID uint16) bool
}
Topology is what the manager reads of this node: its fabrics, its endpoints and their servers, each endpoint's declared client clusters and the fabrics' group keys.