Skip to content

Commit 3a4b1da

Browse files
committed
GRPC: Added lib index update command with progress wrapper
1 parent c969114 commit 3a4b1da

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

app/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@
5555
<classpathentry kind="lib" path="lib/jtouchbar-1.0.0.jar"/>
5656
<classpathentry kind="lib" path="lib/commons-lang3-3.8.1.jar"/>
5757
<classpathentry kind="lib" path="lib/jssc-2.8.0-arduino4.jar"/>
58+
<classpathentry kind="lib" path="lib/grpc-core-1.20.0.jar"/>
5859
<classpathentry kind="output" path="bin"/>
5960
</classpath>

app/lib/grpc-core-1.20.0.jar

763 KB
Binary file not shown.

arduino-core/src/cc/arduino/cli/ArduinoCoreInstance.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,32 @@
2929

3030
package cc.arduino.cli;
3131

32+
import static processing.app.I18n.tr;
33+
34+
import java.util.ArrayList;
3235
import java.util.Iterator;
3336
import java.util.List;
3437

3538
import com.google.protobuf.ByteString;
3639

40+
import cc.arduino.cli.commands.ArduinoCoreGrpc.ArduinoCoreBlockingStub;
3741
import cc.arduino.cli.commands.Board.BoardDetailsReq;
3842
import cc.arduino.cli.commands.Board.BoardDetailsResp;
3943
import cc.arduino.cli.commands.Commands.DestroyReq;
4044
import cc.arduino.cli.commands.Commands.RescanReq;
45+
import cc.arduino.cli.commands.Commands.UpdateLibrariesIndexReq;
46+
import cc.arduino.cli.commands.Commands.UpdateLibrariesIndexResp;
47+
import cc.arduino.cli.commands.Common.DownloadProgress;
4148
import cc.arduino.cli.commands.Common.Instance;
4249
import cc.arduino.cli.commands.Compile.CompileReq;
4350
import cc.arduino.cli.commands.Compile.CompileResp;
4451
import cc.arduino.cli.commands.Lib.LibrarySearchReq;
4552
import cc.arduino.cli.commands.Lib.LibrarySearchResp;
4653
import cc.arduino.cli.commands.Lib.SearchedLibrary;
54+
import cc.arduino.contributions.ProgressListener;
55+
import cc.arduino.contributions.libraries.ContributedLibraryRelease;
4756
import io.grpc.StatusException;
4857
import io.grpc.StatusRuntimeException;
49-
import cc.arduino.cli.commands.ArduinoCoreGrpc.ArduinoCoreBlockingStub;
5058

5159
public class ArduinoCoreInstance {
5260

@@ -111,6 +119,24 @@ public void destroy() throws StatusException {
111119
}
112120
}
113121

122+
public void updateLibrariesIndex(ProgressListener progressListener)
123+
throws StatusException {
124+
try {
125+
Iterator<UpdateLibrariesIndexResp> stream = stub
126+
.updateLibrariesIndex(UpdateLibrariesIndexReq.newBuilder()
127+
.setInstance(instance).build());
128+
ProgressWrapper p = new ProgressWrapper(progressListener);
129+
while (stream.hasNext()) {
130+
UpdateLibrariesIndexResp resp = stream.next();
131+
DownloadProgress progress = resp.getDownloadProgress().toBuilder()
132+
.setFile(tr("Downloading libraries index...")).build();
133+
p.update(progress);
134+
}
135+
} catch (StatusRuntimeException e) {
136+
throw e.getStatus().asException();
137+
}
138+
}
139+
114140
// Lib functions
115141

116142
public List<SearchedLibrary> searchLibrary(String query)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2019 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.cli;
31+
32+
import static processing.app.I18n.format;
33+
import static processing.app.I18n.tr;
34+
35+
import cc.arduino.cli.commands.Common.DownloadProgress;
36+
import cc.arduino.contributions.ProgressListener;
37+
import cc.arduino.utils.MultiStepProgress;
38+
import cc.arduino.utils.Progress;
39+
40+
class ProgressWrapper {
41+
final ProgressListener progressListener;
42+
String file = "";
43+
String url = "";
44+
private long downloaded, totalSize;
45+
Progress progress = new MultiStepProgress(1);
46+
47+
public ProgressWrapper(ProgressListener progressListener) {
48+
this.progressListener = progressListener;
49+
}
50+
51+
public void update(DownloadProgress d) {
52+
if (d == null) {
53+
return;
54+
}
55+
if (!d.getFile().isEmpty()) {
56+
file = d.getFile();
57+
}
58+
if (!d.getUrl().isEmpty()) {
59+
url = d.getUrl();
60+
}
61+
if (d.getTotalSize() > 0) {
62+
totalSize = d.getTotalSize();
63+
}
64+
if (d.getDownloaded() > 0) {
65+
downloaded = d.getDownloaded();
66+
}
67+
68+
String msg = format(tr("Downloaded {0}kb of {1}kb."), downloaded, totalSize);
69+
if (d.getCompleted()) {
70+
file = "";
71+
url = "";
72+
totalSize = 0;
73+
downloaded = 0;
74+
} else {
75+
progress.setStatus("Downloading " + file + url + " (" + downloaded
76+
+ " of " + totalSize + ")");
77+
progress.setProgress(((float) downloaded / totalSize) * 100);
78+
}
79+
progressListener.onProgress(progress);
80+
}
81+
}

0 commit comments

Comments
 (0)