From 5689a67607a0b837f982af1d7da9a67acd54f3e3 Mon Sep 17 00:00:00 2001 From: abn Date: Fri, 5 Sep 2014 13:54:01 +0200 Subject: [PATCH] More robust PVServer startup mechanism. Path to the PVServer registered at CMake time. Temporary log function to a file for Cedric. --- src/ENGINE/CMakeLists.txt | 3 +- .../no_wrap/{PARAVIS.py => PARAVIS.py.in} | 83 +++++++++++-------- src/PV_SWIG/no_wrap/pvsimple.py | 2 +- 3 files changed, 51 insertions(+), 37 deletions(-) rename src/ENGINE/no_wrap/{PARAVIS.py => PARAVIS.py.in} (78%) diff --git a/src/ENGINE/CMakeLists.txt b/src/ENGINE/CMakeLists.txt index 3bca3aac..d851edfb 100644 --- a/src/ENGINE/CMakeLists.txt +++ b/src/ENGINE/CMakeLists.txt @@ -36,8 +36,9 @@ IF(NOT SALOME_PARAVIS_MINIMAL_CORBA) INSTALL(FILES PARAVIS_Engine_i.hh DESTINATION ${SALOME_INSTALL_HEADERS}) ELSE() + CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/no_wrap/PARAVIS.py.in ${CMAKE_CURRENT_BINARY_DIR}/PARAVIS.py @ONLY) SET(_bin_SCRIPTS - no_wrap/PARAVIS.py + ${CMAKE_CURRENT_BINARY_DIR}/PARAVIS.py no_wrap/PARAVIS_utils.py ) diff --git a/src/ENGINE/no_wrap/PARAVIS.py b/src/ENGINE/no_wrap/PARAVIS.py.in similarity index 78% rename from src/ENGINE/no_wrap/PARAVIS.py rename to src/ENGINE/no_wrap/PARAVIS.py.in index c7f8a3d1..e236c6ad 100644 --- a/src/ENGINE/no_wrap/PARAVIS.py +++ b/src/ENGINE/no_wrap/PARAVIS.py.in @@ -26,37 +26,53 @@ import PARAVIS_ORB__POA import SALOME_ComponentPy import SALOME_DriverPy import SALOMEDS +import SALOME import PARAVIS_utils import subprocess as subp +import socket from time import sleep import os -from SALOME_utilities import MESSAGE +#from SALOME_utilities import MESSAGE + +def MESSAGE(m): + os.system("echo \"%s\" >> /tmp/paravis_log.txt" % m) class PARAVIS_Impl: """ The core implementation (non CORBA, or Study related). See the IDL for the documentation. """ MAX_PVSERVER_PORT_TRIES = 10 + PARAVIEW_ROOT_DIR = "@PARAVIEW_ROOT_DIR@" + PVSERVER_DEFAULT_PORT = 11111 def __init__(self): self.pvserverPort = -1 self.pvserverPop = None # Popen object from subprocess module self.liveConnection = None self.lastTrace = "" - + """ - Private. Keeps a connection to the PVServer to prevent - it from stopping because all connections from GUI or scripts have been closed. + Private. Identify a free port to launch the PVServer. """ - def __startKeepAlive(self, host, port): - print "[PARAVIS] 0 __startKeepAlive", host, port - from paraview import simple as pvs - self.liveConnection = pvs.Connect(host, port) - print "[PARAVIS] 1 __startKeepAlive", self.liveConnection + def __getFreePort(self, startPort): + cnt = 0 + currPort = startPort + while cnt < self.MAX_PVSERVER_PORT_TRIES: + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(('', currPort)) + s.close() + return currPort + except socket.error as e: + cnt += 1 + currPort += 1 + pass + raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, + "[PARAVIS] maximum number of tries to retrieve a free port for the PVServer", + "PARAVIS.py", 0)) def FindOrStartPVServer( self, port ): - print "[PARAVIS] FindOrStartPVServer" - MESSAGE("[PARAVIS] FindOrStartPVServer") + MESSAGE("[PARAVIS] FindOrStartPVServer ...") host = "localhost" alive = True if self.pvserverPop is None: @@ -71,37 +87,35 @@ class PARAVIS_Impl: return "cs://%s:%d" % (host, self.pvserverPort) # (else) Server not alive, start it: - pvRootDir = os.getenv("PARAVIEW_ROOT_DIR") - pvServerPath = os.path.join(pvRootDir, 'bin', 'pvserver') + pvServerPath = os.path.join(self.PARAVIEW_ROOT_DIR, 'bin', 'pvserver') opt = [] if port <= 0: - port = 11111 - currPort = port - success = False - while not success and (currPort - port) < self.MAX_PVSERVER_PORT_TRIES: - self.pvserverPop = subp.Popen([pvServerPath, "--multi-clients", "--server-port=%d" % currPort]) - sleep(2) - # Is PID still alive? If yes, consider that the launch was successful - self.pvserverPop.poll() - if self.pvserverPop.returncode is None: - success = True - self.pvserverPort = currPort - MESSAGE("[PARAVIS] pvserver successfully launched on port %d" % currPort) - currPort += 1 - if (currPort - port) == self.MAX_PVSERVER_PORT_TRIES: - self.pvserverPop = None - raise SalomeException("Unable to start PVServer after %d tries!" % self.MAX_PVSERVER_PORT_TRIES) -# self.__startKeepAlive(host, self.pvserverPort) + port = self.__getFreePort(self.PVSERVER_DEFAULT_PORT) + self.pvserverPop = subp.Popen([pvServerPath, "--multi-clients", "--server-port=%d" % port]) + sleep(1) + # Is PID still alive? If yes, consider that the launch was successful + self.pvserverPop.poll() + if self.pvserverPop.returncode is None: + success = True + self.pvserverPort = port + MESSAGE("[PARAVIS] pvserver successfully launched on port %d" % port) + else: + raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.INTERNAL_ERROR, + "[PARAVIS] Unable to start PVServer on port %d!" % port, + "PARAVIS.py", 0)) return "cs://%s:%d" % (host, self.pvserverPort) def StopPVServer( self ): MESSAGE("[PARAVIS] Trying to stop PVServer (sending KILL) ...") if not self.pvserverPop is None: self.pvserverPop.poll() - if not self.pvserverPop.returncode is None: - # Send KILL if still running: - self.pvserverPop.kill() - self.liveConnection = None + if self.pvserverPop.returncode is None: + # Terminate if still running: + self.pvserverPop.terminate() + MESSAGE("[PARAVIS] KILL signal sent.") + return True + MESSAGE("[PARAVIS] Nothing to kill.") + return False def PutPythonTraceStringToEngine( self, t ): self.lastTrace = t @@ -113,7 +127,6 @@ class PARAVIS(PARAVIS_ORB__POA.PARAVIS_Gen, SALOME_ComponentPy.SALOME_ComponentPy_i, SALOME_DriverPy.SALOME_DriverPy_i, PARAVIS_Impl): - """ Construct an instance of PARAVIS module engine. The class PARAVIS implements CORBA interface PARAVIS_Gen (see PARAVIS_Gen.idl). diff --git a/src/PV_SWIG/no_wrap/pvsimple.py b/src/PV_SWIG/no_wrap/pvsimple.py index 4694bae6..788c2eaf 100644 --- a/src/PV_SWIG/no_wrap/pvsimple.py +++ b/src/PV_SWIG/no_wrap/pvsimple.py @@ -48,5 +48,5 @@ def SalomeConnectToPVServer(): __my_log("Connected to %s!" % server_url) # Automatically connect to the right PVServer when not inside SALOME GUI: -if not paraview.servermanager.fromGUI: +if not paraview.fromGUI: SalomeConnectToPVServer() -- 2.39.2