Skip to content

Commit 090c9aa

Browse files
committed
Added godoc comments
1 parent 85d0f95 commit 090c9aa

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

discovery_server.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
// a commercial license, send an email to license@arduino.cc.
1616
//
1717

18+
// discovery is a library for handling the Arduino Pluggable-Discovery protocol
19+
// (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout)
20+
//
21+
// The library implements the state machine and the parsing logic to communicate with a pluggable-discovery client.
22+
// All the commands issued by the client are conveniently translated into function calls, in particular
23+
// the Discovery interface are the only functions that must be implemented to get a fully working pluggable discovery
24+
// using this library.
25+
//
26+
// A usage example is provided in the dummy-discovery package.
1827
package discovery
1928

2029
import (
@@ -30,6 +39,7 @@ import (
3039
"github.com/arduino/go-properties-orderedmap"
3140
)
3241

42+
// Port is a descriptor for a board port
3343
type Port struct {
3444
Address string `json:"address"`
3545
AddressLabel string `json:"label,omitempty"`
@@ -38,16 +48,39 @@ type Port struct {
3848
Properties *properties.Map `json:"properties,omitempty"`
3949
}
4050

41-
type EventCallback func(event string, port *Port)
42-
51+
// Discovery is an interface that represents the business logic that
52+
// a pluggable discovery must implement. The communication protocol
53+
// is completely hidden and it's handled by a DiscoveryServer.
4354
type Discovery interface {
55+
// Hello is called once at startup to provide the userAgent string
56+
// and the protocolVersion negotiated with the client.
4457
Hello(userAgent string, protocolVersion int) error
58+
59+
// Start is called to start the discovery internal subroutines.
4560
Start() error
46-
Stop() error
47-
List() ([]*Port, error)
61+
62+
// List returns the list of the currently available ports. It works
63+
// only after a Start.
64+
List() (portList []*Port, err error)
65+
66+
// StartSync is called to put the discovery in event mode. When the
67+
// function returns the discovery must send port events ("add" or "remove")
68+
// using the eventCB function.
4869
StartSync(eventCB EventCallback) (chan<- bool, error)
70+
71+
// Stop stops the discovery internal subroutines. If the discovery is
72+
// in event mode it must stop sending events through the eventCB previously
73+
// set.
74+
Stop() error
4975
}
5076

77+
// EventCallback is a callback function to call to transmit port
78+
// metadata when the discovery is in "sync" mode and a new event
79+
// is detected.
80+
type EventCallback func(event string, port *Port)
81+
82+
// A DiscoveryServer is a pluggable discovery protocol handler,
83+
// it must be created using the NewDiscoveryServer function.
5184
type DiscoveryServer struct {
5285
impl Discovery
5386
out io.Writer
@@ -60,12 +93,20 @@ type DiscoveryServer struct {
6093
syncCloseChan chan<- bool
6194
}
6295

96+
// NewDiscoveryServer creates a new discovery server backed by the
97+
// provided pluggable discovery implementation. To start the server
98+
// use the Run method.
6399
func NewDiscoveryServer(impl Discovery) *DiscoveryServer {
64100
return &DiscoveryServer{
65101
impl: impl,
66102
}
67103
}
68104

105+
// Run starts the protocol handling loop on the given input and
106+
// output stream, usually `os.Stdin` and `os.Stdout` are used.
107+
// The function blocks until the `QUIT` command is received or
108+
// the input stream is closed. In case of IO error the error is
109+
// returned.
69110
func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
70111
d.out = out
71112
reader := bufio.NewReader(in)

0 commit comments

Comments
 (0)