From baeed1283942218806ef496b6c960d98faee0cad Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@kit.edu>
Date: Thu, 10 Oct 2019 16:22:06 +0200
Subject: [PATCH] Poll for updates asynchronously - No network calls in the GUI
 thread

---
 .../components/ArViz/Coin/Visualizer.cpp      | 27 ++++++++++++-------
 .../components/ArViz/Coin/Visualizer.h        |  1 +
 .../components/ArViz/Coin/VisualizerMesh.h    |  6 ++---
 3 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
index 952038fd1..174f55a60 100644
--- a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
@@ -227,10 +227,26 @@ namespace armarx
                 }
             }
 
-            LayerUpdates pulledUpdates;
             try
             {
-                pulledUpdates = storage->pullUpdatesSince(updateCounter);
+                if (pullUpdateResult)
+                {
+                    // We never block the GUI thread with Ice calls: Only get result when call is completed
+                    if (pullUpdateResult->isCompleted())
+                    {
+                        LayerUpdates pulledUpdates = storage->end_pullUpdatesSince(pullUpdateResult);
+                        for (LayerUpdate const& update : pulledUpdates.updates)
+                        {
+                            apply(update);
+                        }
+                        updateCounter = pulledUpdates.updateCounter;
+                    }
+                    else
+                    {
+                        return;
+                    }
+                }
+                pullUpdateResult = storage->begin_pullUpdatesSince(updateCounter);
             }
             catch (Ice::LocalException const& ex)
             {
@@ -242,13 +258,6 @@ namespace armarx
                 state = CoinVisualizerState::STOPPED;
                 return;
             }
-
-            for (LayerUpdate const& update : pulledUpdates.updates)
-            {
-                apply(update);
-            }
-
-            updateCounter = pulledUpdates.updateCounter;
         }
 
     }
diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.h b/source/RobotAPI/components/ArViz/Coin/Visualizer.h
index c7ee581c3..a40c4bf19 100644
--- a/source/RobotAPI/components/ArViz/Coin/Visualizer.h
+++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.h
@@ -58,6 +58,7 @@ namespace armarx
 
             std::mutex storageMutex;
             viz::StorageInterfacePrx storage;
+            Ice::AsyncResultPtr pullUpdateResult;
             long updateCounter = 0;
 
             SoTimerSensor* timerSensor = nullptr;
diff --git a/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h b/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
index bb91542ef..e7a858e6c 100644
--- a/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
+++ b/source/RobotAPI/components/ArViz/Coin/VisualizerMesh.h
@@ -133,9 +133,9 @@ namespace armarx::viz::coin
                 matInx[i * 4 + 3] = SO_END_FACE_INDEX;
             }
 
-            SoIndexedFaceSet* myFaceSet = data->faceSet;
-            myFaceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
-            myFaceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
+            SoIndexedFaceSet* faceSet = data->faceSet;
+            faceSet->coordIndex.setValuesPointer(faces.size(), faces.data());
+            faceSet->materialIndex.setValuesPointer(matInx.size(), matInx.data());
         }
     };
 }
-- 
GitLab