Skip to content

Commit 49f957e

Browse files
committed
Update client_example to reflect new gRPC changes
1 parent c1b9a59 commit 49f957e

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

client_example/main.go

+36-24
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ func main() {
113113
// Before we can do anything with the CLI, an "instance" must be created.
114114
// We keep a reference to the created instance because we will need it to
115115
// run subsequent commands.
116+
log.Println("calling Create")
117+
instance := createInstance(client)
118+
116119
log.Println("calling Init")
117-
instance := initInstance(client)
120+
initInstance(client, instance)
118121

119122
// We set up the proxy and then run the update to verify that the proxy settings are currently used
120123
log.Println("calling setProxy")
@@ -129,6 +132,11 @@ func main() {
129132
log.Println("calling UpdateIndex")
130133
callUpdateIndex(client, instance)
131134

135+
// Indexes are not implicitly detected after an update
136+
// so we must initialize again explicitly
137+
log.Println("calling Init")
138+
initInstance(client, instance)
139+
132140
// Let's search for a platform (also known as 'core') called 'samd'.
133141
log.Println("calling PlatformSearch(samd)")
134142
callPlatformSearch(client, instance)
@@ -198,6 +206,11 @@ func main() {
198206
log.Println("calling UpdateLibrariesIndex()")
199207
callUpdateLibraryIndex(client, instance)
200208

209+
// Indexes are not implicitly detected after an update
210+
// so we must initialize again explicitly
211+
log.Println("calling Init")
212+
initInstance(client, instance)
213+
201214
// Download a library
202215
log.Println("calling LibraryDownload([email protected])")
203216
callLibDownload(client, instance)
@@ -326,47 +339,46 @@ func callWrite(client settings.SettingsServiceClient) {
326339
}
327340
}
328341

329-
func initInstance(client rpc.ArduinoCoreServiceClient) *rpc.Instance {
330-
// The configuration for this example client only contains the path to
331-
// the data folder.
332-
initRespStream, err := client.Init(context.Background(), &rpc.InitRequest{})
342+
func createInstance(client rpc.ArduinoCoreServiceClient) *rpc.Instance {
343+
res, err := client.Create(context.Background(), &rpc.CreateRequest{})
333344
if err != nil {
334345
log.Fatalf("Error creating server instance: %s", err)
346+
}
347+
return res.Instance
348+
}
335349

350+
func initInstance(client rpc.ArduinoCoreServiceClient, instance *rpc.Instance) {
351+
stream, err := client.Init(context.Background(), &rpc.InitRequest{
352+
Instance: instance,
353+
})
354+
if err != nil {
355+
log.Fatalf("Error initializing server instance: %s", err)
336356
}
337357

338-
var instance *rpc.Instance
339-
// Loop and consume the server stream until all the setup procedures are done.
340358
for {
341-
initResp, err := initRespStream.Recv()
342-
// The server is done.
359+
res, err := stream.Recv()
360+
// Server has finished sending
343361
if err == io.EOF {
344362
break
345363
}
346364

347-
// There was an error.
348365
if err != nil {
349366
log.Fatalf("Init error: %s", err)
350367
}
351368

352-
// The server sent us a valid instance, let's print its ID.
353-
if initResp.GetInstance() != nil {
354-
instance = initResp.GetInstance()
355-
log.Printf("Got a new instance with ID: %v", instance.GetId())
356-
}
357-
358-
// When a download is ongoing, log the progress
359-
if initResp.GetDownloadProgress() != nil {
360-
log.Printf("DOWNLOAD: %s", initResp.GetDownloadProgress())
369+
if status := res.GetError(); status != nil {
370+
log.Printf("Init error %s", status.String())
361371
}
362372

363-
// When an overall task is ongoing, log the progress
364-
if initResp.GetTaskProgress() != nil {
365-
log.Printf("TASK: %s", initResp.GetTaskProgress())
373+
if progress := res.GetInitProgress(); progress != nil {
374+
if downloadProgress := progress.GetDownloadProgress(); downloadProgress != nil {
375+
log.Printf("DOWNLOAD: %s", downloadProgress)
376+
}
377+
if taskProgress := progress.GetTaskProgress(); taskProgress != nil {
378+
log.Printf("TASK: %s", taskProgress)
379+
}
366380
}
367381
}
368-
369-
return instance
370382
}
371383

372384
func callUpdateIndex(client rpc.ArduinoCoreServiceClient, instance *rpc.Instance) {

0 commit comments

Comments
 (0)