]> SALOME platform Git repositories - modules/kernel.git/blob - src/NamingService/NamingService.i
Salome HOME
YACS in SSL mode
[modules/kernel.git] / src / NamingService / NamingService.i
1 // Copyright (C) 2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 %module NamingService
21
22 %{
23 #include "SALOME_Fake_NamingService.hxx"
24 #include "SALOME_Embedded_NamingService.hxx"
25 #include "SALOME_KernelORB.hxx"
26 #include "Utils_SALOME_Exception.hxx"
27 %}
28
29 %include "std_string.i"
30 %include "std_vector.i"
31
32 %rename(NamingService) SALOME_Fake_NamingService ;
33
34 %template(svec) std::vector<std::string>;
35
36 %exceptionclass SALOME_Exception;
37
38 class SALOME_Exception
39 {
40 public:
41   SALOME_Exception(const std::string& text);
42   ~SALOME_Exception() noexcept;
43   const char *what() const noexcept;
44   %extend
45   {
46     std::string __str__() const
47     {
48       return std::string(self->what());
49     }
50   }
51 };
52
53 %exception {
54   try {
55     $action
56   }
57   catch (SALOME_Exception& _e) {
58     // Reraise with SWIG_Python_Raise
59     SWIG_Python_Raise(SWIG_NewPointerObj((new SALOME_Exception(static_cast< const SALOME_Exception& >(_e))),SWIGTYPE_p_SALOME_Exception,SWIG_POINTER_OWN), "SALOME_Exception", SWIGTYPE_p_SALOME_Exception);
60     SWIG_fail;
61   }
62 }
63
64 class SALOME_Fake_NamingService
65 {
66 public:
67   SALOME_Fake_NamingService();
68   std::vector< std::string > repr();
69   static std::string GetLogContainersFile();
70   static void FlushLogContainersFile();
71   %extend {
72     static void SetLogContainersFileInternal(const std::string& logFileName)
73     {
74       SALOME_Fake_NamingService::SetLogContainersFile(logFileName);
75     }
76     std::string _ResolveInternal(const char *Path)
77     {
78       CORBA::Object_var obj = self->Resolve(Path);
79       CORBA::ORB_ptr orb = KERNEL::getORB();
80       CORBA::String_var ior = orb->object_to_string(obj);
81       return std::string(ior);
82     }
83     void _RegisterInternal(const char *ior, const char* Path)
84     {
85       CORBA::ORB_ptr orb = KERNEL::getORB();
86       CORBA::Object_var obj = orb->string_to_object(ior);
87       self->Register(obj,Path);
88     }
89     static std::string IOROfNS()
90     {
91       CORBA::ORB_ptr orb = KERNEL::getORB();
92       Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService();
93       CORBA::String_var ior = orb->object_to_string(ns);
94       return std::string(ior);
95     }
96   }
97 };
98
99 %pythoncode %{
100 def NamingService_Resolve(self,Path):
101   ret = self._ResolveInternal(Path)
102   import CORBA
103   orb=CORBA.ORB_init([''])
104   return orb.string_to_object(ret)
105 def NamingService_Register(self,obj,Path):
106   import CORBA
107   orb=CORBA.ORB_init([''])
108   self._RegisterInternal( orb.object_to_string(obj) , Path)
109 NamingService.Resolve = NamingService_Resolve
110 NamingService.Register = NamingService_Register
111 def NamingService_SetLogContainersFile(cls,logFileName = None):
112   if logFileName is None:
113     import tempfile
114     with tempfile.NamedTemporaryFile() as f:
115       logFileName = f.name
116   cls.SetLogContainersFileInternal(logFileName)
117 NamingService.SetLogContainersFile = classmethod(NamingService_SetLogContainersFile)
118 def NamingService_RefOfNS(cls):
119   ret = cls.IOROfNS()
120   import Engines
121   import CORBA
122   orb=CORBA.ORB_init([''])
123   return orb.string_to_object(ret)
124 NamingService.RefOfNS = classmethod(NamingService_RefOfNS)
125 def NamingService_KillContainersInFile(cls,logFileName):
126   import Engines
127   import CORBA
128   orb=CORBA.ORB_init([''])
129   with open(logFileName) as f:
130     cont_to_kill = [elt.split(" : ") for elt in f]
131     for name,ior in cont_to_kill:
132       try:
133         ref = orb.string_to_object(ior)
134         ref.Shutdown()
135       except Exception as e:
136         print("Failed to kill container remotely \"{}\"".format(name))
137 NamingService.KillContainersInFile = classmethod(NamingService_KillContainersInFile)
138 %}