Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / TestContainer / TestContainer.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SALOME TestContainer : test of container creation and its life cycle
23 //  File   : TestContainer.cxx
24 //  Author : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
25 //  Module : SALOME
26 //  $Header$
27 //
28 #include "utilities.h"
29 #include <iostream>
30 #ifndef WIN32
31 #include <unistd.h>
32 #endif
33 #include <SALOMEconfig.h>
34 #include CORBA_CLIENT_HEADER(SALOME_Component)
35 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
36
37 #include "SALOME_NamingService.hxx"
38 #include "NamingService_WaitForServerReadiness.hxx"
39 #include "Basics_Utils.hxx"
40 #include "Utils_ORB_INIT.hxx"
41 #include "Utils_SINGLETON.hxx"
42 #include "Utils_SALOME_Exception.hxx"
43 #include "Utils_CommException.hxx"
44
45 static std::ostream& operator<<(std::ostream& os, const CORBA::Exception& e)
46 {
47   CORBA::Any tmp;
48   tmp<<= e;
49   CORBA::TypeCode_var tc = tmp.type();
50   const char *p = tc->name();
51   os<<"Test blocking exception was catch of the kind : ";
52   if ( *p != '\0' ) {
53     os<<p;
54   } 
55   else  { 
56     os << tc->id();
57   }
58   
59   return os;
60 }
61
62 Engines::TestComponent_ptr create_instance(Engines::Container_ptr iGenFact,
63                                            std::string componenttName)
64 {
65 #if defined(_DEBUG_) || defined(_DEBUG)
66   bool isLib =
67     iGenFact->load_component_Library(componenttName.c_str());
68   //    iGenFact->load_component_Library("SalomeTestComponent");
69   ASSERT(isLib);
70 #else
71   iGenFact->load_component_Library(componenttName.c_str());
72 #endif
73   CORBA::Object_var obj =
74     //    iGenFact->create_component_instance("SalomeTestComponent",
75     iGenFact->create_component_instance(componenttName.c_str(),
76                                         0);
77   Engines::TestComponent_var anInstance = Engines::TestComponent::_narrow(obj);
78   MESSAGE("create anInstance");
79   SCRUTE(anInstance->instanceName());
80   return anInstance._retn();
81 }
82
83 int main (int argc, char * argv[])
84 {
85   // Initializing omniORB
86   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
87   CORBA::ORB_ptr orb = init( argc , argv ) ;
88   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
89
90   try
91     {
92       SALOME_NamingService _NS(orb) ;
93       std::string containerName = "/Containers/" ;
94       std::string hostName = Kernel_Utils::GetHostname();
95       containerName += hostName + "/FactoryServer";
96       NamingService_WaitForServerReadiness(&_NS,containerName);
97
98       CORBA::Object_var obj = _NS.Resolve(containerName.c_str()) ;
99       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
100       iGenFact->ping() ;
101
102       int nbInstances = 5;
103
104       std::vector<Engines::TestComponent_var> instances(nbInstances);
105     
106       MESSAGE("------------------------------- create instances ");
107       for (int iter = 0; iter < nbInstances ; iter++)
108         {
109           instances[iter] = create_instance(iGenFact,"SalomeTestComponent");
110         }
111
112       MESSAGE("------------------------------ set env instances ");
113       for (int iter = 0; iter < nbInstances ; iter++)
114         {
115           Engines::TestComponent_var anInstance = instances[iter];
116           SCRUTE(anInstance->instanceName());
117           Engines::FieldsDict_var dico = new Engines::FieldsDict;
118           dico->length(3);
119           dico[0].key=CORBA::string_dup("key_0");
120           dico[0].value <<="value_0";
121           dico[1].key=CORBA::string_dup("key_1");
122           dico[1].value <<=(CORBA::UShort)72;
123           dico[2].key=CORBA::string_dup("key_2");
124           dico[2].value <<=(CORBA::ULong)iter;
125           anInstance->setProperties(dico);
126           MESSAGE("Coucou " << anInstance->Coucou(iter));
127           anInstance->Setenv();
128         }
129
130       MESSAGE("---------------------------------- get instances ");
131       for (int iter = 0; iter < nbInstances ; iter++)
132         {
133           Engines::TestComponent_var anInstance = instances[iter];
134           SCRUTE(anInstance->instanceName());
135           Engines::FieldsDict_var dico2 =  anInstance->getProperties();
136           for (CORBA::ULong i=0; i<dico2->length(); i++)
137             {
138               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
139               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
140               if (dico2[i].value.type()->kind() == CORBA::tk_string)
141                 {
142                   const char* value;
143                   dico2[i].value >>= value;
144                   MESSAGE("dico2["<<i<<"].value="<<value);
145                 }
146             }
147         }
148
149       MESSAGE("------------------------------- remove instances ");
150       for (int iter = 0; iter < nbInstances ; iter++)
151         {
152           Engines::TestComponent_var anInstance = instances[iter];
153           SCRUTE(anInstance->instanceName());
154           iGenFact->remove_impl(anInstance) ;
155           //iGenFact->finalize_removal() ; // unpredictable results ...
156         } 
157       MESSAGE("------------------------------- PYTHON ");
158       {
159 //      bool isLib =
160 //        iGenFact->load_component_Library("SALOME_TestComponentPy");
161 //      ASSERT(isLib);
162 //      CORBA::Object_var obj =
163 //        iGenFact->create_component_instance("SALOME_TestComponentPy",
164 //                                            0);
165 //      Engines::TestComponent_var anInstance =
166 //        Engines::TestComponent::_narrow(obj);
167 //      MESSAGE("create anInstance");
168 //      SCRUTE(anInstance->instanceName());
169       MESSAGE("------------------------------- create instances ");
170       for (int iter = 0; iter < nbInstances ; iter++)
171         {
172           instances[iter] = create_instance(iGenFact,"SALOME_TestComponentPy");
173         }
174
175       MESSAGE("---------------------------------- get instances ");
176       for (int iter = 0; iter < nbInstances ; iter++)
177         {
178           Engines::TestComponent_var anInstance = instances[iter];
179           SCRUTE(anInstance->instanceName());
180           MESSAGE("Coucou " << anInstance->Coucou(iter));
181         }
182       }
183    
184       // Clean-up.
185       iGenFact->finalize_removal() ;
186       orb->shutdown(0);
187     }
188   catch(CORBA::COMM_FAILURE& ex) {
189     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
190       }
191   catch(CORBA::SystemException& e) {
192     INFOS("Caught a CORBA::SystemException."<<e)
193       }
194   catch(CORBA::Exception& e) {
195     INFOS("Caught CORBA::Exception."<<e)
196       }
197   catch(ServiceUnreachable& e) {
198     INFOS("Caught Exception. "<<e)
199       }
200   catch(...) {
201     INFOS("Caught unknown exception.")
202       }
203
204   //  delete myThreadTrace;
205   return 0;
206 }