From ce9fb3929f7a38653b07c8a397818d0494c3b268 Mon Sep 17 00:00:00 2001
From: Francois Henrotte <francois.henrotte@ulg.ac.be>
Date: Mon, 19 Jan 2015 10:59:12 +0000
Subject: [PATCH] Handles the Gmsh GUI Stop button for nonnative clients,
 function call

---
 Fltk/onelabGroup.cpp            |  2 +-
 contrib/onelab/python/onelab.py | 33 ++++++++++++++++++++++++++-------
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/Fltk/onelabGroup.cpp b/Fltk/onelabGroup.cpp
index 9c51ef7d52..a32f646826 100644
--- a/Fltk/onelabGroup.cpp
+++ b/Fltk/onelabGroup.cpp
@@ -860,7 +860,7 @@ void onelab_cb(Fl_Widget *w, void *data)
     onelab::server::instance()->get(pn,"0Metamodel/9Use restored solution");
     if(pn.size()){
       restoreMode=pn[0].getValue();
-      pn[0].setValue(2);
+      pn[0].setValue(2); // special value 
       onelab::server::instance()->set(pn[0]);
     }
 
diff --git a/contrib/onelab/python/onelab.py b/contrib/onelab/python/onelab.py
index d5949135b0..98268c857c 100755
--- a/contrib/onelab/python/onelab.py
+++ b/contrib/onelab/python/onelab.py
@@ -27,7 +27,7 @@ Please report all bugs and problems to the public mailing list
 <gmsh@geuz.org>.
 """
 
-import socket, struct, os, sys, subprocess
+import socket, struct, os, sys, subprocess, time
 _VERSION = '1.1'
 
 def file_exist(filename):
@@ -424,7 +424,7 @@ class client :
   def __del__(self):
     self.finalize()
 
-  def call(self, name, cmdline, remote='', rundir='', logfile=''):
+  def call(self, name, cmdline, remote='', rundir='', logfile='', poll=0):
     cwd = None
     if not remote :
       argv = cmdline.rsplit(' ')
@@ -433,24 +433,43 @@ class client :
     else :
       argv=['ssh', remote , "cd %s ; %s" %(rundir,cmdline) ]
 
+    if poll and not logfile :
+        logfile = os.devnull
     if logfile:
       call = subprocess.Popen(argv, bufsize=1, cwd=cwd,
                               stdout=open(logfile,"w"),
-                              stderr=subprocess.PIPE)
+                              stderr=subprocess.STDOUT)
+      while call.poll() == None :
+        self.action = self.getString(self.name + '/Action', False)
+        if self.action == 'stop':
+          call.terminate()
+          self._send(self._GMSH_WARNING, 'client killed')
+          sys.exit(1)
+        time.sleep(1) # check every second for the stop signal from Gmsh
     else:
       call = subprocess.Popen(argv, bufsize=1, cwd=cwd,
                               stdout=subprocess.PIPE,
-                              stderr=subprocess.PIPE)
+                              stderr=subprocess.STDOUT)
+      count = 0
       for line in iter(call.stdout.readline, b''):
         print(line.rstrip())
+        count += 1
+        if count == 50 : # check every 50 lines for the stop signal from Gmsh
+          count = 0
+          self.action = self.getString(self.name + '/Action', False)
+          if self.action == 'stop':
+            call.terminate()
+            self._send(self._GMSH_WARNING, 'client killed')
+            sys.exit(1)
+      
     result = call.wait()
     if result == 0 :
-      self._send(self._GMSH_INFO, 'call \"' + ' '.join(argv) + '\"')
+      self._send(self._GMSH_INFO, 'done \"' + ' '.join(argv) + '\"')
       if self.action == 'compute':
         self.setChanged(name, False)
     else :
-      for line in iter(call.stderr.readline, b''):
-        self._send(self._GMSH_ERROR, line.rstrip().encode('utf-8'))
+      self._send(self._GMSH_WARNING,
+        'Error report in ' + self.name + '_stderr.log')
       sys.exit(1)
   
   def copy(self, here, there):
-- 
GitLab