@@ -19,6 +19,7 @@ import (
1919 "context"
2020 "fmt"
2121 "io"
22+ "time"
2223
2324 "github.com/arduino/arduino-cli/executils"
2425 dbg "github.com/arduino/arduino-cli/rpc/debug"
@@ -35,7 +36,7 @@ func Debug(ctx context.Context, req *dbg.DebugReq, inStream dbg.Debug_StreamingO
3536
3637 in , err := cmd .StdinPipe ()
3738 if err != nil {
38- fmt .Println ("%v\n " , err )
39+ fmt .Printf ("%v\n " , err )
3940 return & dbg.StreamingOpenResp {}, nil // TODO: send error in response
4041 }
4142 defer in .Close ()
@@ -44,61 +45,23 @@ func Debug(ctx context.Context, req *dbg.DebugReq, inStream dbg.Debug_StreamingO
4445
4546 err = cmd .Start ()
4647 if err != nil {
47- fmt .Println ("%v\n " , err )
48+ fmt .Printf ("%v\n " , err )
4849 return & dbg.StreamingOpenResp {}, nil // TODO: send error in response
4950 }
5051
51- // we'll use these channels to communicate with the goroutines
52- // handling the stream and the target respectively
53- streamClosed := make (chan error )
54- targetClosed := make (chan error )
55- defer close (streamClosed )
56- defer close (targetClosed )
57-
5852 // now we can read the other commands and re-route to the Debug Client...
5953 go func () {
6054 for {
61- command , err := inStream .Recv ()
62- if err == io .EOF {
63- // stream was closed
64- streamClosed <- nil
55+ if command , err := inStream .Recv (); err != nil {
6556 break
66- }
67-
68- if err != nil {
69- // error reading from stream
70- streamClosed <- err
71- break
72- }
73-
74- if _ , err := in .Write (command .GetData ()); err != nil {
75- // error writing to target
76- targetClosed <- err
57+ } else if _ , err := in .Write (command .GetData ()); err != nil {
7758 break
7859 }
7960 }
61+ time .Sleep (time .Second )
62+ cmd .Process .Kill ()
8063 }()
8164
82- // let goroutines route messages from/to the Debug
83- // until either the client closes the stream or the
84- // Debug target is closed
85- for {
86- select {
87- case <- ctx .Done ():
88- cmd .Process .Kill ()
89- cmd .Wait ()
90- case err := <- streamClosed :
91- fmt .Println ("streamClosed" )
92- cmd .Process .Kill ()
93- cmd .Wait ()
94- return & dbg.StreamingOpenResp {}, err // TODO: send error in response
95- case err := <- targetClosed :
96- fmt .Println ("targetClosed" )
97- cmd .Process .Kill ()
98- cmd .Wait ()
99- return & dbg.StreamingOpenResp {}, err // TODO: send error in response
100- }
101- }
102-
65+ err = cmd .Wait () // TODO: handle err
10366 return & dbg.StreamingOpenResp {}, nil
10467}
0 commit comments