Skip to content

Commit

Permalink
allow discovery registry to register
Browse files Browse the repository at this point in the history
Allow the discovery registry to optionally register devices that it
discovers with the device registry. This reduces the work that a user
has to do to couple them, which they almost always will want to do.
  • Loading branch information
danjacques committed Apr 19, 2018
1 parent 275f087 commit 75c5b1f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions discovery/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Registry struct {
// If <= 0, a device will never expire once observed.
Expiration time.Duration

// DeviceRegistry, if not nil, is a device.Registry that will be updated when
// new devices are registered.
DeviceRegistry *device.Registry

// Protects the following data members.
mu sync.Mutex
// Map of active devices.
Expand Down Expand Up @@ -70,6 +74,16 @@ func (reg *Registry) Devices() []device.D {
// it has not been observed before, or refresh its timeout and metadata if it
// has.
func (reg *Registry) Observe(dh *protocol.DiscoveryHeaders) (d device.D, isNew bool) {
d, isNew = reg.observeImpl(dh)

if reg.DeviceRegistry != nil {
reg.DeviceRegistry.Add(d)
}

return
}

func (reg *Registry) observeImpl(dh *protocol.DiscoveryHeaders) (d device.D, isNew bool) {
// Use hardware address as ID.
id := dh.HardwareAddr().String()
now := time.Now()
Expand Down

0 comments on commit 75c5b1f

Please sign in to comment.