Salome HOME
PR: mergefrom_PAL_OCC_21Oct04
[modules/kernel.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 using namespace std;
43
44 static ostream& operator<<(ostream& os, const CORBA::Exception& e)
45 {
46   CORBA::Any tmp;
47   tmp<<= e;
48   CORBA::TypeCode_var tc = tmp.type();
49   const char *p = tc->name();
50   os<<"Test blocking exception was catch of the kind : ";
51   if ( *p != '\0' ) {
52     os<<p;
53   } 
54   else  { 
55     os << tc->id();
56   }
57   
58   return os;
59 }
60
61
62 int main (int argc, char * argv[])
63 {
64
65   try
66     {
67       // Initializing omniORB
68       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
69     
70       // use IOR to find container
71       //if (argc != 2) { return 1; }
72       //CORBA::Object_var obj = orb->string_to_object(argv[1]);
73       //Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
74
75       // Obtain a reference to the root POA
76       //
77       long TIMESleep = 250000000;
78       int NumberOfTries = 40;
79       int a;
80       timespec ts_req;
81       ts_req.tv_nsec=TIMESleep;
82       ts_req.tv_sec=0;
83       timespec ts_rem;
84       ts_rem.tv_nsec=0;
85       ts_rem.tv_sec=0;
86       CosNaming::NamingContext_var inc;
87       PortableServer::POA_var poa;
88       CORBA::Object_var theObj;
89       CORBA::Object_var obj;
90       CORBA::Object_var object;
91       SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
92       int TEST_CONTAINER=0;
93       const char * Env = getenv("USE_LOGGER"); 
94       int EnvL =0;
95       if ((Env!=NULL) && (strlen(Env)))
96         EnvL=1;
97       CosNaming::Name name;
98       name.length(1);
99       name[0].id=CORBA::string_dup("Logger");    
100       PortableServer::POAManager_var manager; 
101       for (int i = 1; i<=NumberOfTries; i++)
102         {
103           if (i!=1) 
104             a=nanosleep(&ts_req,&ts_rem);
105           try
106             { 
107               obj = orb->resolve_initial_references("RootPOA");
108               if(!CORBA::is_nil(obj))
109                 poa = PortableServer::POA::_narrow(obj);
110               if(!CORBA::is_nil(poa))
111                 manager = poa->the_POAManager();
112               if(!CORBA::is_nil(orb)) 
113                 theObj = orb->resolve_initial_references("NameService");
114               if (!CORBA::is_nil(theObj))
115                 inc = CosNaming::NamingContext::_narrow(theObj);
116             }
117           catch( CORBA::COMM_FAILURE& )
118             {
119               INFOS( "Test Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
120                 }
121           if(!CORBA::is_nil(inc))
122             {
123               MESSAGE( "Test Container: Naming Service was found" )
124                 if(EnvL==1)
125                   {
126                     for(int j=1; j<=NumberOfTries; j++)
127                       {
128                         if (j!=1) 
129                           a=nanosleep(&ts_req, &ts_rem);
130                         try
131                           {
132                             object = inc->resolve(name);
133                           }
134                         catch(CosNaming::NamingContext::NotFound)
135                           {
136                             INFOS( "Test Container: Logger Server wasn't found" );
137                           }
138                         catch(...)
139                           {
140                             INFOS( "Test Container: Unknown exception" );
141                           }
142                         if (!CORBA::is_nil(object))
143                           {
144                             MESSAGE( "Test Container: Loger Server was found" );
145                             TEST_CONTAINER=1;
146                             break;
147                           }
148                       }
149                   }
150             }
151           if ((TEST_CONTAINER==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
152             break;
153         }
154
155       // Use Name Service to find container
156       SALOME_NamingService _NS(orb) ;
157       string containerName = "/Containers/" ;
158       string hostName = GetHostname();
159       containerName += hostName + "/FactoryServer";
160
161       obj = _NS.Resolve(containerName.c_str()) ;
162       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
163
164       Engines::TestComponent_var m1;
165     
166       for (int iter = 0; iter < 3 ; iter++)
167         {
168           MESSAGE("----------------------------------------------------" << iter);   
169           string dirn = getenv("KERNEL_ROOT_DIR");
170           dirn += "/lib/salome/libSalomeTestComponentEngine.so";
171           obj = iGenFact->load_impl("SalomeTestComponent",dirn.c_str());
172           m1 = Engines::TestComponent::_narrow(obj);
173           MESSAGE("recup m1");
174           SCRUTE(m1->instanceName());
175
176           Engines::FieldsDict_var dico = new Engines::FieldsDict;
177           dico->length(3);
178           dico[0].key=CORBA::string_dup("key_0");
179           dico[0].value <<="value_0";
180           dico[1].key=CORBA::string_dup("key_1");
181           dico[1].value <<=(CORBA::UShort)72;
182           dico[2].key=CORBA::string_dup("key_2");
183           dico[2].value <<="value_2";
184           m1->setProperties(dico);
185
186           MESSAGE("Coucou " << m1->Coucou(1L));
187
188           m1->Setenv();
189
190           Engines::FieldsDict_var dico2 =  m1->getProperties();
191           for (CORBA::ULong i=0; i<dico2->length(); i++)
192             {
193               MESSAGE("dico2["<<i<<"].key="<<dico2[i].key);
194               MESSAGE("dico2["<<i<<"].value type ="<<dico2[i].value.type()->kind());
195               if (dico2[i].value.type()->kind() == CORBA::tk_string)
196                 {
197                   const char* value;
198                   dico2[i].value >>= value;
199                   MESSAGE("dico2["<<i<<"].value="<<value);
200                 }
201             }
202
203           iGenFact->remove_impl(m1) ;
204           //iGenFact->finalize_removal() ; // unpredictable results ...
205           sleep(5);
206         }    
207       // Clean-up.
208       iGenFact->finalize_removal() ;
209       orb->destroy();
210     }
211   catch(CORBA::COMM_FAILURE& ex) {
212     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
213       }
214   catch(CORBA::SystemException& e) {
215     INFOS("Caught a CORBA::SystemException."<<e)
216       }
217   catch(CORBA::Exception& e) {
218     INFOS("Caught CORBA::Exception."<<e)
219       }
220   catch(...) {
221     INFOS("Caught unknown exception.")
222       }
223
224   return 0;
225 }
226