// This file is part of dummy-discovery. // // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies // SPDX-License-Identifier: GPL-3.0-or-later // main is the main module of the dummy-discovery binary. It implements a dummy discovery that can be used for testing purposes. package main import ( "errors" "fmt" "os" "time" "github.com/arduino/go-properties-orderedmap" discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2" "github.com/arduino/pluggable-discovery-protocol-handler/v2/dummy-discovery/args" ) // dummyDiscovery is an example implementation of a Discovery. // It simulates a real implementation of a Discovery by generating // connected ports deterministically, it can also be used for testing // purposes. type dummyDiscovery struct { startSyncCount int closeChan chan<- bool } func main() { args.Parse() dummy := &dummyDiscovery{} server := discovery.NewServer(dummy) if err := server.Run(os.Stdin, os.Stdout); err != nil { os.Exit(1) } } // Hello does nothing. // In a real implementation it could setup background processes // or other kind of resources necessary to discover Ports. func (d *dummyDiscovery) Hello(_ string, _ int) error { return nil } // Quit does nothing. // In a real implementation it can be used to tear down resources // used to discovery Ports. func (d *dummyDiscovery) Quit() {} // Stop is used to stop the goroutine started by StartSync // used to discover ports. func (d *dummyDiscovery) Stop() error { if d.closeChan != nil { d.closeChan <- true close(d.closeChan) d.closeChan = nil } return nil } // StartSync starts the goroutine that generates fake Ports. func (d *dummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error { d.startSyncCount++ if d.startSyncCount%5 == 0 { return errors.New("could not start_sync every 5 times") } c := make(chan bool) d.closeChan = c // Run synchronous event emitter go func() { var closeChan <-chan bool = c // Output initial port state eventCB("add", createDummyPort()) eventCB("add", createDummyPort()) // Start sending events count := 0 for count < 2 { count++ select { case <-closeChan: return case <-time.After(2 * time.Second): } port := createDummyPort() eventCB("add", port) select { case <-closeChan: return case <-time.After(2 * time.Second): } eventCB("remove", &discovery.Port{ Address: port.Address, Protocol: port.Protocol, }) } errorCB("unrecoverable error, cannot send more events") <-closeChan }() return nil } var dummyCounter = 0 // createDummyPort creates a Port with fake data func createDummyPort() *discovery.Port { dummyCounter++ mac := fmt.Sprintf("%d", dummyCounter*384782) return &discovery.Port{ Address: fmt.Sprintf("%d", dummyCounter), AddressLabel: "Dummy upload port", Protocol: "dummy", ProtocolLabel: "Dummy protocol", HardwareID: mac, Properties: properties.NewFromHashmap(map[string]string{ "vid": "0x2341", "pid": "0x0041", "mac": mac, }), } }