]> SALOME platform Git repositories - modules/kernel.git/blobdiff - src/Launcher/SALOME_ExternalServerLauncher.cxx
Salome HOME
Compilation under Windows: add missing header
[modules/kernel.git] / src / Launcher / SALOME_ExternalServerLauncher.cxx
index 6ef363251df58058263b02bce276b0fdf858011d..2fb3ab142a9dda213d8e84bf97ed13dd4869a01f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2019-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 
 #include CORBA_CLIENT_HEADER(SALOME_ExternalServerLauncher)
 
+#ifndef WIN32
 #include <unistd.h>
+#else
+#include <windows.h>
+#include <Basics_Utils.hxx>
+#endif
 
 #include <sstream>
+#include <fstream>
 #include <algorithm>
+#include <memory>
+#include <functional>
 
-constexpr char NAME_IN_NS[]="/ExternalServers";
+const char SALOME_ExternalServerLauncher::NAME_IN_NS[]="/ExternalServers";
 
 unsigned SALOME_ExternalServerLauncher::CNT = 0;
 
@@ -53,8 +61,23 @@ SALOME_ExternalServerLauncher::~SALOME_ExternalServerLauncher()
 class ChdirRAII
 {
 public:
+#ifndef WIN32
   ChdirRAII(const std::string& wd):_wd(wd) { if(_wd.empty()) return ; char *pwd(get_current_dir_name()); _od = pwd; free(pwd); chdir(_wd.c_str()); }
   ~ChdirRAII() { if(_od.empty()) return ; chdir(_od.c_str()); }
+#else
+       ChdirRAII(const std::string& wd) : _wd(wd) { 
+               if (_wd.empty()) 
+                       return;
+               TCHAR pwd[MAX_PATH];
+               GetCurrentDirectory(sizeof(pwd), pwd);
+               _od = Kernel_Utils::utf8_encode_s(pwd);
+               SetCurrentDirectory(Kernel_Utils::utf8_decode_s(_wd).c_str());
+       }
+       ~ChdirRAII() { 
+               if (_od.empty()) return; 
+               SetCurrentDirectory(Kernel_Utils::utf8_decode_s(_od).c_str());
+       }
+#endif 
 private:
   std::string _wd;
   std::string _od;
@@ -69,8 +92,7 @@ SALOME::ExternalServerHandler_ptr SALOME_ExternalServerLauncher::launchServer(co
       throw SALOME_LauncherException(oss2.str());
     }
   std::vector<std::string> cmd(command_list.length());
-  const char *toto(command_list[0]);
-  for(auto i=0;i<command_list.length();i++)
+  for(size_t i=0;i<command_list.length();i++)
     cmd[i] = command_list[i];
   long pid(0);
   try
@@ -98,6 +120,7 @@ void SALOME_ExternalServerLauncher::registerToKill(const char *server_name, CORB
   std::ostringstream oss;
   oss << "Custom_"<< server_name << "_" << CNT++;
   _pyHelper->registerToSalomePiDict(oss.str(),PID);
+  _list_of_pids_to_kill.push_back(PID);
 }
 
 void SALOME_ExternalServerLauncher::cleanServersInNS()
@@ -115,6 +138,11 @@ void SALOME_ExternalServerLauncher::cleanServersInNS()
 
 void SALOME_ExternalServerLauncher::shutdownServers()
 {
+  for(auto pid : this->_list_of_pids_to_kill)
+    {
+      SALOME_ExternalServerHandler::KillPID(pid);
+    }
+  //
   std::vector<std::string> lioes(ListOfExternalServersCpp(_NS));
   for(auto servName : lioes)
     {
@@ -137,7 +165,7 @@ SALOME::StringVec *SALOME_ExternalServerLauncher::listServersInNS()
   std::vector<std::string> loes(ListOfExternalServersCpp(_NS));
   std::size_t sz(loes.size());
   ret->length(sz);
-  for(auto i = 0; i<sz ; i++)
+  for(size_t i=0; i<sz; i++)
     {
       (*ret)[i]=CORBA::string_dup(loes[i].c_str());
     }
@@ -156,6 +184,33 @@ char *SALOME_ExternalServerLauncher::gethostname()
   return CORBA::string_dup(ret.c_str());
 }
 
+SALOME::ByteVec *SALOME_ExternalServerLauncher::fetchContentOfFileAndRm(const char *file_name)
+{
+  std::ifstream t(file_name);
+  if(!t.good())
+    {
+      std::ostringstream oss; oss << "SALOME_ExternalServerLauncher::fetchContentOfFileAndRm : Error when trying to open \"" <<  file_name << "\" file !";
+      throw SALOME_LauncherException(oss.str());
+    }
+  t.seekg(0, std::ios::end);
+  size_t size(t.tellg());
+  std::unique_ptr<char,std::function<void(char *)> > buffer(new char[size],[](char *pt) { delete [] pt; });
+  t.seekg(0);
+  t.read(buffer.get(),size);
+  //
+  std::unique_ptr<SALOME::ByteVec> ret(new SALOME::ByteVec);
+  ret->length(size);
+  for(size_t i=0;i<size;++i)
+    (*ret)[i] = buffer.get()[i];
+  //
+  if( unlink(file_name)!=0 )
+    {
+      std::cerr << "Error when trying to remove \"" << file_name << "\" !";
+    }
+  //
+  return ret.release();
+}
+
 std::vector<std::string> SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService *ns)
 {
   ns->Change_Directory(NAME_IN_NS);