Debian Codegraph @

dummy-discovery/main.go

raw
1 // This file is part of dummy-discovery.
2 //
3 // SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies
4 // SPDX-License-Identifier: GPL-3.0-or-later
5
6 // main is the main module of the dummy-discovery binary. It implements a dummy discovery that can be used for testing purposes.
7 package main
8
9 import (
10 "errors"
11 "fmt"
12 "os"
13 "time"
14
15 "github.com/arduino/go-properties-orderedmap"
16 discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
17 "github.com/arduino/pluggable-discovery-protocol-handler/v2/dummy-discovery/args"
18 )
19
20 // dummyDiscovery is an example implementation of a Discovery.
21 // It simulates a real implementation of a Discovery by generating
22 // connected ports deterministically, it can also be used for testing
23 // purposes.
24 type dummyDiscovery struct {
25 startSyncCount int
26 closeChan chan<- bool
27 }
28
29 func main() {
30 args.Parse()
31 dummy := &dummyDiscovery{}
32 server := discovery.NewServer(dummy)
33 if err := server.Run(os.Stdin, os.Stdout); err != nil {
34 os.Exit(1)
35 }
36 }
37
38 // Hello does nothing.
39 // In a real implementation it could setup background processes
40 // or other kind of resources necessary to discover Ports.
41 func (d *dummyDiscovery) Hello(_ string, _ int) error {
42 return nil
43 }
44
45 // Quit does nothing.
46 // In a real implementation it can be used to tear down resources
47 // used to discovery Ports.
48 func (d *dummyDiscovery) Quit() {}
49
50 // Stop is used to stop the goroutine started by StartSync
51 // used to discover ports.
52 func (d *dummyDiscovery) Stop() error {
53 if d.closeChan != nil {
54 d.closeChan <- true
55 close(d.closeChan)
56 d.closeChan = nil
57 }
58 return nil
59 }
60
61 // StartSync starts the goroutine that generates fake Ports.
62 func (d *dummyDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
63 d.startSyncCount++
64 if d.startSyncCount%5 == 0 {
65 return errors.New("could not start_sync every 5 times")
66 }
67
68 c := make(chan bool)
69 d.closeChan = c
70
71 // Run synchronous event emitter
72 go func() {
73 var closeChan <-chan bool = c
74
75 // Output initial port state
76 eventCB("add", createDummyPort())
77 eventCB("add", createDummyPort())
78
79 // Start sending events
80 count := 0
81 for count < 2 {
82 count++
83
84 select {
85 case <-closeChan:
86 return
87 case <-time.After(2 * time.Second):
88 }
89
90 port := createDummyPort()
91 eventCB("add", port)
92
93 select {
94 case <-closeChan:
95 return
96 case <-time.After(2 * time.Second):
97 }
98
99 eventCB("remove", &discovery.Port{
100 Address: port.Address,
101 Protocol: port.Protocol,
102 })
103 }
104
105 errorCB("unrecoverable error, cannot send more events")
106 <-closeChan
107 }()
108
109 return nil
110 }
111
112 var dummyCounter = 0
113
114 // createDummyPort creates a Port with fake data
115 func createDummyPort() *discovery.Port {
116 dummyCounter++
117 mac := fmt.Sprintf("%d", dummyCounter*384782)
118 return &discovery.Port{
119 Address: fmt.Sprintf("%d", dummyCounter),
120 AddressLabel: "Dummy upload port",
121 Protocol: "dummy",
122 ProtocolLabel: "Dummy protocol",
123 HardwareID: mac,
124 Properties: properties.NewFromHashmap(map[string]string{
125 "vid": "0x2341",
126 "pid": "0x0041",
127 "mac": mac,
128 }),
129 }
130 }