Salome HOME
8cdab3ecb4d6bbfe32ef7d797da11dac01cf3ded
[modules/kernel.git] / src / NamingService / NamingService.i
1 // Copyright (C) 2021-2024  CEA, EDF
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   void Destroy_Name(const char* Path);
72   void Destroy_FullDirectory(const char* Path);
73   %extend {
74     static void SetLogContainersFileInternal(const std::string& logFileName)
75     {
76       SALOME_Fake_NamingService::SetLogContainersFile(logFileName);
77     }
78     std::string _ResolveInternal(const char *Path)
79     {
80       CORBA::Object_var obj = self->Resolve(Path);
81       CORBA::ORB_ptr orb = KERNEL::getORB();
82       CORBA::String_var ior = orb->object_to_string(obj);
83       return std::string(ior);
84     }
85     void _RegisterInternal(const char *ior, const char* Path)
86     {
87       CORBA::ORB_ptr orb = KERNEL::getORB();
88       CORBA::Object_var obj = orb->string_to_object(ior);
89       self->Register(obj,Path);
90     }
91     std::string _Resolve_DirInternal(const char *Path)
92     {
93       CORBA::ORB_ptr orb = KERNEL::getORB();
94       CORBA::Object_var ret = self->Resolve(Path);
95       CORBA::String_var ior = orb->object_to_string(ret);
96       return std::string(ior.in());
97     }
98     static std::string IOROfNS()
99     {
100       CORBA::ORB_ptr orb = KERNEL::getORB();
101       Engines::EmbeddedNamingService_var ns = GetEmbeddedNamingService();
102       CORBA::String_var ior = orb->object_to_string(ns);
103       return std::string(ior);
104     }
105   }
106 };
107
108 %pythoncode %{
109 def NamingService_Resolve(self,Path):
110   ret = self._ResolveInternal(Path)
111   import CORBA
112   orb=CORBA.ORB_init([''])
113   return orb.string_to_object(ret)
114 def NamingService_Register(self,obj,Path):
115   import CORBA
116   orb=CORBA.ORB_init([''])
117   self._RegisterInternal( orb.object_to_string(obj) , Path)
118 def NamingService_Resolve_Dir(self,Path):
119   ret = self._Resolve_DirInternal( Path )
120   import CORBA
121   orb=CORBA.ORB_init([''])
122   return orb.string_to_object(ret)
123 NamingService.Resolve = NamingService_Resolve
124 NamingService.Register = NamingService_Register
125 NamingService.Resolve_Dir = NamingService_Resolve_Dir
126 def NamingService_SetLogContainersFile(cls,logFileName = None):
127   if logFileName is None:
128     import tempfile
129     with tempfile.NamedTemporaryFile() as f:
130       logFileName = f.name
131   cls.SetLogContainersFileInternal(logFileName)
132 NamingService.SetLogContainersFile = classmethod(NamingService_SetLogContainersFile)
133 def NamingService_RefOfNS(cls):
134   ret = cls.IOROfNS()
135   import Engines
136   import CORBA
137   orb=CORBA.ORB_init([''])
138   return orb.string_to_object(ret)
139 NamingService.RefOfNS = classmethod(NamingService_RefOfNS)
140 def NamingService_KillContainersInFile(cls,logFileName):
141   import Engines
142   import CORBA
143   orb=CORBA.ORB_init([''])
144   with open(logFileName) as f:
145     cont_to_kill = [elt.split(" : ") for elt in f]
146     for name,ior in cont_to_kill:
147       try:
148         ref = orb.string_to_object(ior)
149         ref.Shutdown()
150       except Exception as e:
151         print("Failed to kill container remotely \"{}\"".format(name))
152 NamingService.KillContainersInFile = classmethod(NamingService_KillContainersInFile)
153 %}