1515// a commercial license, send an email to [email protected] . 1616// 
1717
18- // discovery is a library for handling the Arduino Pluggable-Discovery protocol 
18+ // Package  discovery is a library for handling the Arduino Pluggable-Discovery protocol 
1919// (https://github.com/arduino/tooling-rfcs/blob/main/RFCs/0002-pluggable-discovery.md#pluggable-discovery-api-via-stdinstdout) 
2020// 
2121// The library implements the state machine and the parsing logic to communicate with a pluggable-discovery client. 
@@ -81,9 +81,9 @@ type EventCallback func(event string, port *Port)
8181// performs a STOP+START_SYNC cycle. 
8282type  ErrorCallback  func (err  string )
8383
84- // A DiscoveryServer  is a pluggable discovery protocol handler, 
85- // it must be created using the NewDiscoveryServer  function. 
86- type  DiscoveryServer  struct  {
84+ // A Server  is a pluggable discovery protocol handler, 
85+ // it must be created using the NewServer  function. 
86+ type  Server  struct  {
8787	impl                Discovery 
8888	outputChan          chan  * message 
8989	userAgent           string 
@@ -95,11 +95,11 @@ type DiscoveryServer struct {
9595	cachedErr           string 
9696}
9797
98- // NewDiscoveryServer  creates a new discovery server backed by the 
98+ // NewServer  creates a new discovery server backed by the 
9999// provided pluggable discovery implementation. To start the server 
100100// use the Run method. 
101- func  NewDiscoveryServer (impl  Discovery ) * DiscoveryServer  {
102- 	return  & DiscoveryServer {
101+ func  NewServer (impl  Discovery ) * Server  {
102+ 	return  & Server {
103103		impl :       impl ,
104104		outputChan : make (chan  * message ),
105105	}
@@ -110,7 +110,7 @@ func NewDiscoveryServer(impl Discovery) *DiscoveryServer {
110110// The function blocks until the `QUIT` command is received or 
111111// the input stream is closed. In case of IO error the error is 
112112// returned. 
113- func  (d  * DiscoveryServer ) Run (in  io.Reader , out  io.Writer ) error  {
113+ func  (d  * Server ) Run (in  io.Reader , out  io.Writer ) error  {
114114	go  d .outputProcessor (out )
115115	defer  close (d .outputChan )
116116	reader  :=  bufio .NewReader (in )
@@ -150,7 +150,7 @@ func (d *DiscoveryServer) Run(in io.Reader, out io.Writer) error {
150150	}
151151}
152152
153- func  (d  * DiscoveryServer ) hello (cmd  string ) {
153+ func  (d  * Server ) hello (cmd  string ) {
154154	if  d .initialized  {
155155		d .outputChan  <-  messageError ("hello" , "HELLO already called" )
156156		return 
@@ -162,12 +162,12 @@ func (d *DiscoveryServer) hello(cmd string) {
162162		return 
163163	}
164164	d .userAgent  =  matches [2 ]
165- 	if  v , err  :=  strconv .ParseInt (matches [1 ], 10 , 64 ); err  !=  nil  {
165+ 	v , err  :=  strconv .ParseInt (matches [1 ], 10 , 64 )
166+ 	if  err  !=  nil  {
166167		d .outputChan  <-  messageError ("hello" , "Invalid protocol version: " + matches [2 ])
167168		return 
168- 	} else  {
169- 		d .reqProtocolVersion  =  int (v )
170169	}
170+ 	d .reqProtocolVersion  =  int (v )
171171	if  err  :=  d .impl .Hello (d .userAgent , 1 ); err  !=  nil  {
172172		d .outputChan  <-  messageError ("hello" , err .Error ())
173173		return 
@@ -180,7 +180,7 @@ func (d *DiscoveryServer) hello(cmd string) {
180180	d .initialized  =  true 
181181}
182182
183- func  (d  * DiscoveryServer ) start () {
183+ func  (d  * Server ) start () {
184184	if  d .started  {
185185		d .outputChan  <-  messageError ("start" , "Discovery already STARTed" )
186186		return 
@@ -199,7 +199,7 @@ func (d *DiscoveryServer) start() {
199199	d .outputChan  <-  messageOk ("start" )
200200}
201201
202- func  (d  * DiscoveryServer ) eventCallback (event  string , port  * Port ) {
202+ func  (d  * Server ) eventCallback (event  string , port  * Port ) {
203203	id  :=  port .Address  +  "|"  +  port .Protocol 
204204	if  event  ==  "add"  {
205205		d .cachedPorts [id ] =  port 
@@ -209,11 +209,11 @@ func (d *DiscoveryServer) eventCallback(event string, port *Port) {
209209	}
210210}
211211
212- func  (d  * DiscoveryServer ) errorCallback (msg  string ) {
212+ func  (d  * Server ) errorCallback (msg  string ) {
213213	d .cachedErr  =  msg 
214214}
215215
216- func  (d  * DiscoveryServer ) list () {
216+ func  (d  * Server ) list () {
217217	if  ! d .started  {
218218		d .outputChan  <-  messageError ("list" , "Discovery not STARTed" )
219219		return 
@@ -236,7 +236,7 @@ func (d *DiscoveryServer) list() {
236236	}
237237}
238238
239- func  (d  * DiscoveryServer ) startSync () {
239+ func  (d  * Server ) startSync () {
240240	if  d .syncStarted  {
241241		d .outputChan  <-  messageError ("start_sync" , "Discovery already START_SYNCed" )
242242		return 
@@ -253,7 +253,7 @@ func (d *DiscoveryServer) startSync() {
253253	d .outputChan  <-  messageOk ("start_sync" )
254254}
255255
256- func  (d  * DiscoveryServer ) stop () {
256+ func  (d  * Server ) stop () {
257257	if  ! d .syncStarted  &&  ! d .started  {
258258		d .outputChan  <-  messageError ("stop" , "Discovery already STOPped" )
259259		return 
@@ -269,18 +269,18 @@ func (d *DiscoveryServer) stop() {
269269	d .outputChan  <-  messageOk ("stop" )
270270}
271271
272- func  (d  * DiscoveryServer ) syncEvent (event  string , port  * Port ) {
272+ func  (d  * Server ) syncEvent (event  string , port  * Port ) {
273273	d .outputChan  <-  & message {
274274		EventType : event ,
275275		Port :      port ,
276276	}
277277}
278278
279- func  (d  * DiscoveryServer ) errorEvent (msg  string ) {
279+ func  (d  * Server ) errorEvent (msg  string ) {
280280	d .outputChan  <-  messageError ("start_sync" , msg )
281281}
282282
283- func  (d  * DiscoveryServer ) outputProcessor (outWriter  io.Writer ) {
283+ func  (d  * Server ) outputProcessor (outWriter  io.Writer ) {
284284	// Start go routine to serialize messages printing 
285285	go  func () {
286286		for  msg  :=  range  d .outputChan  {
0 commit comments