Salome HOME
Update copyrights
[modules/kernel.git] / src / TestContainer / TestContainer.cxx
1 // Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SALOME TestContainer : test of container creation and its life cycle
24 //  File   : TestContainer.cxx
25 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
26 //  Module : SALOME
27 //  $Header$
28 //
29 #include "utilities.h"
30 #include <iostream>
31 #ifndef WIN32
32 #include <unistd.h>
33 #endif
34 #include <SALOMEconfig.h>
35 #include CORBA_CLIENT_HEADER(SALOME_Component)
36 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
37
38 #include "SALOME_NamingService.hxx"
39 #include "NamingService_WaitForServerReadiness.hxx"
40 #include "Basics_Utils.hxx"
41 #include "Utils_ORB_INIT.hxx"
42 #include "Utils_SINGLETON.hxx"
43 #include "Utils_SALOME_Exception.hxx"
44 #include "Utils_CommException.hxx"
45
46 static std::ostream& operator<<(std::ostream& os, const CORBA::Exception& e)
47 {
48   CORBA::Any tmp;
49   tmp<<= e;
50   CORBA::TypeCode_var tc = tmp.type();
51   const char *p = tc->name();
52   os<<"Test blocking exception was catch of the kind : ";
53   if ( *p != '\0' ) {
54     os<<p;
55   } 
56   else  { 
57     os << tc->id();
58   }
59   
60   return os;
61 }
62
63 Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact,
64                                            std::string componenttName)
65 {
66   char* reason;
67 #if defined(_DEBUG_) || defined(_DEBUG)
68   bool isLib =
69     iGenFact->load_component_Library(componenttName.c_str(),reason);
70   ASSERT(isLib);
71 #else
72   iGenFact->load_component_Library(componenttName.c_str(),reason);
73 #endif
74   CORBA::string_free(reason);
75   CORBA::Object_var obj = iGenFact->create_component_instance(componenttName.c_str());
76   Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
77   MESSAGE("create anInstance");
78   SCRUTE(anInstance->instanceName());
79   return anInstance._retn();
80 }
81
82 int main (int argc, char * argv[])
83 {
84   // Initializing omniORB
85   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
86   CORBA::ORB_ptr orb = init( argc , argv ) ;
87   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
88
89   try
90     {
91       SALOME_NamingService _NS(orb) ;
92       std::string containerName = "/Containers/" ;
93       std::string hostName = Kernel_Utils::GetHostname();
94       containerName += hostName + "/FactoryServer";
95       NamingService_WaitForServerReadiness(&_NS,containerName);
96
97       CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
98       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
99       iGenFact->ping() ;
100
101       int nbInstances = 5;
102
103       std::vector<Engines::TestComponent_var> instances(nbInstances);
104     
105       MESSAGE("------------------------------- create instances ");
106       for (int iter = 0; iter < nbInstances ; iter++)
107         {
108           instances[iter] = create_instance(iGenFact,"SalomeTestComponent");
109         }
110
111       MESSAGE("------------------------------ set env instances ");
112       for (int iter = 0; iter < nbInstances ; iter++)
113         {
114           Engines::TestComponent_var anInstance = instances[iter];
115           SCRUTE(anInstance->instanceName());
116           Engines::FieldsDict_var dico = new Engines::FieldsDict;
117           dico->length(3);
118           dico[0].key=CORBA::string_dup("key_0");
119           dico[0].value <<="value_0";
120           dico[1].key=CORBA::string_dup("key_1");
121           dico[1].value <<=(CORBA::UShort)72;
122           dico[2].key=CORBA::string_dup("key_2");
123           dico[2].value <<=(CORBA::ULong)iter;
124           anInstance->setProperties(dico);
125           MESSAGE("Coucou " << anInstance->Coucou(iter));
126           anInstance->Setenv();
127         }
128
129       MESSAGE("---------------------------------- get instances ");
130       for (int iter = 0; iter < nbInstances ; iter++)
131         {
132           Engines::TestComponent_var anInstance = instances[iter];
133           SCRUTE(anInstance->instanceName());
134           Engines::FieldsDict_var dico2 =  anInstance->getProperties();
135           for (CORBA::ULong i=0; i<dico2->length(); i++)
136             {
137               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
138               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
139               if (dico2[i].value.type()->kind() == CORBA::tk_string)
140                 {
141                   const char* value;
142                   dico2[i].value >>= value;
143                   MESSAGE("dico2["<<i<<"].value="<<value);
144                 }
145             }
146         }
147
148       MESSAGE("------------------------------- remove instances ");
149       for (int iter = 0; iter < nbInstances ; iter++)
150         {
151           Engines::TestComponent_var anInstance = instances[iter];
152           SCRUTE(anInstance->instanceName());
153           iGenFact->remove_impl(anInstance) ;
154           //iGenFact->finalize_removal() ; // unpredictable results ...
155         } 
156       MESSAGE("------------------------------- PYTHON ");
157       {
158 //      bool isLib =
159 //        iGenFact->load_component_Library("SALOME_TestComponentPy");
160 //      ASSERT(isLib);
161 //      CORBA::Object_var obj =
162 //        iGenFact->create_component_instance("SALOME_TestComponentPy",
163 //                                            0);
164 //      Engines::TestComponent_var anInstance =
165 //        Engines::TestComponent::_narrow(obj);
166 //      MESSAGE("create anInstance");
167 //      SCRUTE(anInstance->instanceName());
168       MESSAGE("------------------------------- create instances ");
169       for (int iter = 0; iter < nbInstances ; iter++)
170         {
171           instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy");
172         }
173
174       MESSAGE("---------------------------------- get instances ");
175       for (int iter = 0; iter < nbInstances ; iter++)
176         {
177           Engines::TestComponent_var anInstance = instances[iter];
178           SCRUTE(anInstance->instanceName());
179           MESSAGE("Coucou " << anInstance->Coucou(iter));
180         }
181       }
182    
183       // Clean-up.
184       iGenFact->finalize_removal() ;
185       orb->shutdown(0);
186     }
187   catch(CORBA::COMM_FAILURE& ex) {
188     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
189       }
190   catch(CORBA::SystemException& e) {
191     INFOS("Caught a CORBA::SystemException."<<e)
192       }
193   catch(CORBA::Exception& e) {
194     INFOS("Caught CORBA::Exception."<<e)
195       }
196   catch(ServiceUnreachable& e) {
197     INFOS("Caught Exception. "<<e)
198       }
199   catch(...) {
200     INFOS("Caught unknown exception.")
201       }
202
203   //  delete myThreadTrace;
204   return 0;
205 }