Salome HOME
PR: merge from tag BR_CCRT2_mergeto_V2_1_0b1
[modules/yacs.git] / src / TestContainer / TestContainer.cxx
1 //  SALOME TestContainer : test of container creation and its life cycle
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
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 #include <unistd.h>
32 #include <SALOMEconfig.h>
33 #include CORBA_CLIENT_HEADER(SALOME_Component)
34 #include CORBA_CLIENT_HEADER(SALOME_TestComponent)
35
36 #include "SALOME_NamingService.hxx"
37 #include "OpUtil.hxx"
38 #include "Utils_ORB_INIT.hxx"
39 #include "Utils_SINGLETON.hxx"
40 #include "Utils_SALOME_Exception.hxx"
41 #include "Utils_CommException.hxx"
42 #include "LocalTraceCollector.hxx"
43 using namespace std;
44
45 static ostream& operator<<(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
63 int main (int argc, char * argv[])
64 {
65
66   // Initializing omniORB
67   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
68   CORBA::ORB_var &orb = init( argc , argv ) ;
69   LocalTraceCollector *myThreadTrace = LocalTraceCollector::instance(orb);
70
71   try
72     {
73
74
75     
76       // use IOR to find container
77       //if (argc != 2) { return 1; }
78       //CORBA::Object_var obj = orb->string_to_object(argv[1]);
79       //Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
80
81       // Obtain a reference to the root POA
82       //
83       long TIMESleep = 250000000;
84       int NumberOfTries = 40;
85       int a;
86       timespec ts_req;
87       ts_req.tv_nsec=TIMESleep;
88       ts_req.tv_sec=0;
89       timespec ts_rem;
90       ts_rem.tv_nsec=0;
91       ts_rem.tv_sec=0;
92       CosNaming::NamingContext_var inc;
93       PortableServer::POA_var poa;
94       CORBA::Object_var theObj;
95       CORBA::Object_var obj;
96       CORBA::Object_var object;
97       SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
98       int TEST_CONTAINER=0;
99       const char * Env = getenv("USE_LOGGER"); 
100       int EnvL =0;
101       if ((Env!=NULL) && (strlen(Env)))
102         EnvL=1;
103       CosNaming::Name name;
104       name.length(1);
105       name[0].id=CORBA::string_dup("Logger");    
106       PortableServer::POAManager_var manager; 
107       for (int i = 1; i<=NumberOfTries; i++)
108         {
109           if (i!=1) 
110             a=nanosleep(&ts_req,&ts_rem);
111           try
112             { 
113               obj = orb->resolve_initial_references("RootPOA");
114               if(!CORBA::is_nil(obj))
115                 poa = PortableServer::POA::_narrow(obj);
116               if(!CORBA::is_nil(poa))
117                 manager = poa->the_POAManager();
118               if(!CORBA::is_nil(orb)) 
119                 theObj = orb->resolve_initial_references("NameService");
120               if (!CORBA::is_nil(theObj))
121                 inc = CosNaming::NamingContext::_narrow(theObj);
122             }
123           catch( CORBA::COMM_FAILURE& )
124             {
125               INFOS( "Test Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
126                 }
127           if(!CORBA::is_nil(inc))
128             {
129               MESSAGE( "Test Container: Naming Service was found" )
130                 if(EnvL==1)
131                   {
132                     for(int j=1; j<=NumberOfTries; j++)
133                       {
134                         if (j!=1) 
135                           a=nanosleep(&ts_req, &ts_rem);
136                         try
137                           {
138                             object = inc->resolve(name);
139                           }
140                         catch(CosNaming::NamingContext::NotFound)
141                           {
142                             INFOS( "Test Container: Logger Server wasn't found" );
143                           }
144                         catch(...)
145                           {
146                             INFOS( "Test Container: Unknown exception" );
147                           }
148                         if (!CORBA::is_nil(object))
149                           {
150                             MESSAGE( "Test Container: Loger Server was found" );
151                             TEST_CONTAINER=1;
152                             break;
153                           }
154                       }
155                   }
156             }
157           if ((TEST_CONTAINER==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
158             break;
159         }
160
161       // Use Name Service to find container
162       SALOME_NamingService _NS(orb) ;
163       string containerName = "/Containers/" ;
164       string hostName = GetHostname();
165       containerName += hostName + "/FactoryServer";
166
167       obj = _NS.Resolve(containerName.c_str()) ;
168       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
169
170       Engines::TestComponent_var m1;
171     
172       for (int iter = 0; iter < 3 ; iter++)
173         {
174           MESSAGE("----------------------------------------------------" << iter);   
175           string dirn = getenv("KERNEL_ROOT_DIR");
176           dirn += "/lib/salome/libSalomeTestComponentEngine.so";
177           obj = iGenFact->load_impl("SalomeTestComponent",dirn.c_str());
178           m1 = Engines::TestComponent::_narrow(obj);
179           MESSAGE("recup m1");
180           SCRUTE(m1->instanceName());
181
182           Engines::FieldsDict_var dico = new Engines::FieldsDict;
183           dico->length(3);
184           dico[0].key=CORBA::string_dup("key_0");
185           dico[0].value <<="value_0";
186           dico[1].key=CORBA::string_dup("key_1");
187           dico[1].value <<=(CORBA::UShort)72;
188           dico[2].key=CORBA::string_dup("key_2");
189           dico[2].value <<="value_2";
190           m1->setProperties(dico);
191
192           MESSAGE("Coucou " << m1->Coucou(1L));
193
194           m1->Setenv();
195
196           Engines::FieldsDict_var dico2 =  m1->getProperties();
197           for (CORBA::ULong i=0; i<dico2->length(); i++)
198             {
199               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
200               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
201               if (dico2[i].value.type()->kind() == CORBA::tk_string)
202                 {
203                   const char* value;
204                   dico2[i].value >>= value;
205                   MESSAGE("dico2["<<i<<"].value="<<value);
206                 }
207             }
208
209           iGenFact->remove_impl(m1) ;
210           //iGenFact->finalize_removal() ; // unpredictable results ...
211           sleep(5);
212         }    
213       // Clean-up.
214       iGenFact->finalize_removal() ;
215       orb->destroy();
216     }
217   catch(CORBA::COMM_FAILURE& ex) {
218     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
219       }
220   catch(CORBA::SystemException& e) {
221     INFOS("Caught a CORBA::SystemException."<<e)
222       }
223   catch(CORBA::Exception& e) {
224     INFOS("Caught CORBA::Exception."<<e)
225       }
226   catch(...) {
227     INFOS("Caught unknown exception.")
228       }
229
230   delete myThreadTrace;
231   return 0;
232 }
233