Skip to content

Commit 19c3235

Browse files
author
rsora
committed
Refactor stream helpers
1 parent 1cf35e6 commit 19c3235

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

arduino/utils/stream.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package utils
17+
18+
import "io"
19+
20+
// FeedStreamTo creates a pipe to pass data to the writer function.
21+
// FeedStreamTo returns the io.Writer side of the pipe, on which the user can write data
22+
func FeedStreamTo(writer func(data []byte)) io.Writer {
23+
r, w := io.Pipe()
24+
go func() {
25+
data := make([]byte, 1024)
26+
for {
27+
if n, err := r.Read(data); err == nil {
28+
writer(data[:n])
29+
} else {
30+
return
31+
}
32+
}
33+
}()
34+
return w
35+
}
36+
37+
// ConsumeStreamFrom creates a pipe to consume data from the reader function.
38+
// ConsumeStreamFrom returns the io.Reader side of the pipe, which the user can use to consume the data
39+
func ConsumeStreamFrom(reader func() ([]byte, error)) io.Reader {
40+
41+
r, w := io.Pipe()
42+
go func() {
43+
for {
44+
if data, err := reader(); err != nil {
45+
return
46+
} else if _, err := w.Write(data); err != nil {
47+
return
48+
}
49+
}
50+
}()
51+
return r
52+
}

commands/daemon/daemon.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package daemon
1919

2020
import (
2121
"context"
22-
"io"
2322
"net/http"
2423

24+
"github.com/arduino/arduino-cli/arduino/utils"
2525
"github.com/arduino/arduino-cli/commands"
2626
"github.com/arduino/arduino-cli/commands/board"
2727
"github.com/arduino/arduino-cli/commands/compile"
@@ -125,8 +125,8 @@ func (s *ArduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionReq
125125
func (s *ArduinoCoreServerImpl) Compile(req *rpc.CompileReq, stream rpc.ArduinoCore_CompileServer) error {
126126
resp, err := compile.Compile(
127127
stream.Context(), req,
128-
feedStream(func(data []byte) { stream.Send(&rpc.CompileResp{OutStream: data}) }),
129-
feedStream(func(data []byte) { stream.Send(&rpc.CompileResp{ErrStream: data}) }),
128+
utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.CompileResp{OutStream: data}) }),
129+
utils.FeedStreamTo(func(data []byte) { stream.Send(&rpc.CompileResp{ErrStream: data}) }),
130130
false) // set debug to false
131131
if err != nil {
132132
return err
@@ -222,21 +222,6 @@ func (s *ArduinoCoreServerImpl) Upload(req *rpc.UploadReq, stream rpc.ArduinoCor
222222
return stream.Send(resp)
223223
}
224224

225-
func feedStream(streamer func(data []byte)) io.Writer {
226-
r, w := io.Pipe()
227-
go func() {
228-
data := make([]byte, 1024)
229-
for {
230-
if n, err := r.Read(data); err == nil {
231-
streamer(data[:n])
232-
} else {
233-
return
234-
}
235-
}
236-
}()
237-
return w
238-
}
239-
240225
// LibraryDownload FIXMEDOC
241226
func (s *ArduinoCoreServerImpl) LibraryDownload(req *rpc.LibraryDownloadReq, stream rpc.ArduinoCore_LibraryDownloadServer) error {
242227
resp, err := lib.LibraryDownload(

commands/daemon/debug.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ package daemon
1717

1818
import (
1919
"fmt"
20+
"github.com/arduino/arduino-cli/arduino/utils"
2021
cmd "github.com/arduino/arduino-cli/commands/debug"
21-
"io"
22-
2322
dbg "github.com/arduino/arduino-cli/rpc/debug"
2423
)
2524

@@ -45,30 +44,15 @@ func (s *DebugService) Debug(stream dbg.Debug_DebugServer) error {
4544

4645
// launch debug recipe attaching stdin and out to grpc streaming
4746
resp, err := cmd.Debug(stream.Context(), req,
48-
copyStream(func() ([]byte, error) {
47+
utils.ConsumeStreamFrom(func() ([]byte, error) {
4948
command, err := stream.Recv()
5049
return command.GetData(), err
5150
}),
52-
feedStream(func(data []byte) {
51+
utils.FeedStreamTo(func(data []byte) {
5352
stream.Send(&dbg.DebugResp{Data: data})
5453
}))
5554
if err != nil {
5655
return (err)
5756
}
5857
return stream.Send(resp)
5958
}
60-
61-
func copyStream(streamIn func() ([]byte, error)) io.Reader {
62-
63-
r, w := io.Pipe()
64-
go func() {
65-
for {
66-
if data, err := streamIn(); err != nil {
67-
return
68-
} else if _, err := w.Write(data); err != nil {
69-
return
70-
}
71-
}
72-
}()
73-
return r
74-
}

0 commit comments

Comments
 (0)