Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b2'.
[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 int main (int argc, char * argv[])
45 {
46
47   try
48     {
49       // Initializing omniORB
50       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
51     
52       // use IOR to find container
53       //if (argc != 2) { return 1; }
54       //CORBA::Object_var obj = orb->string_to_object(argv[1]);
55       //Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
56
57       // Obtain a reference to the root POA
58       //
59       long TIMESleep = 250000000;
60       int NumberOfTries = 40;
61       int a;
62       timespec ts_req;
63       ts_req.tv_nsec=TIMESleep;
64       ts_req.tv_sec=0;
65       timespec ts_rem;
66       ts_rem.tv_nsec=0;
67       ts_rem.tv_sec=0;
68       CosNaming::NamingContext_var inc;
69       PortableServer::POA_var poa;
70       CORBA::Object_var theObj;
71       CORBA::Object_var obj;
72       CORBA::Object_var object;
73       SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
74       int TEST_CONTAINER=0;
75       const char * Env = getenv("USE_LOGGER"); 
76       int EnvL =0;
77       if ((Env!=NULL) && (strlen(Env)))
78         EnvL=1;
79       CosNaming::Name name;
80       name.length(1);
81       name[0].id=CORBA::string_dup("Logger");    
82       PortableServer::POAManager_var manager; 
83       for (int i = 1; i<=NumberOfTries; i++)
84         {
85           if (i!=1) 
86             a=nanosleep(&ts_req,&ts_rem);
87           try
88             { 
89               obj = orb->resolve_initial_references("RootPOA");
90               if(!CORBA::is_nil(obj))
91                 poa = PortableServer::POA::_narrow(obj);
92               if(!CORBA::is_nil(poa))
93                 manager = poa->the_POAManager();
94               if(!CORBA::is_nil(orb)) 
95                 theObj = orb->resolve_initial_references("NameService");
96               if (!CORBA::is_nil(theObj))
97                 inc = CosNaming::NamingContext::_narrow(theObj);
98             }
99           catch( CORBA::COMM_FAILURE& )
100             {
101               MESSAGE( "Test Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
102                 }
103           if(!CORBA::is_nil(inc))
104             {
105               MESSAGE( "Test Container: Naming Service was found" )
106                 if(EnvL==1)
107                   {
108                     for(int j=1; j<=NumberOfTries; j++)
109                       {
110                         if (j!=1) 
111                           a=nanosleep(&ts_req, &ts_rem);
112                         try
113                           {
114                             object = inc->resolve(name);
115                           }
116                         catch(CosNaming::NamingContext::NotFound)
117                           {
118                             MESSAGE( "Test Container: Logger Server wasn't found" );
119                           }
120                         catch(...)
121                           {
122                             MESSAGE( "Test Container: Unknown exception" );
123                           }
124                         if (!CORBA::is_nil(object))
125                           {
126                             MESSAGE( "Test Container: Loger Server was found" );
127                             TEST_CONTAINER=1;
128                             break;
129                           }
130                       }
131                   }
132             }
133           if ((TEST_CONTAINER==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
134             break;
135         }
136
137       // Use Name Service to find container
138       SALOME_NamingService _NS(orb) ;
139       string containerName = "/Containers/" ;
140       string hostName = GetHostname();
141       containerName += hostName + "/FactoryServer";
142
143       obj = _NS.Resolve(containerName.c_str()) ;
144       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
145
146       Engines::TestComponent_var m1;
147     
148       for (int iter = 0; iter < 3 ; iter++)
149         {
150           INFOS("----------------------------------------------------" << iter);   
151           string dirn = getenv("SALOME_ROOT_DIR");
152           dirn += "/lib/salome/libSalomeTestComponentEngine.so";
153           obj = iGenFact->load_impl("SalomeTestComponent",dirn.c_str());
154           m1 = Engines::TestComponent::_narrow(obj);
155           INFOS("recup m1");
156           SCRUTE(m1->instanceName());
157           INFOS("Coucou " << m1->Coucou(1L));
158           iGenFact->remove_impl(m1) ;
159           //iGenFact->finalize_removal() ; // unpredictable results ...
160           sleep(5);
161         }    
162       // Clean-up.
163       iGenFact->finalize_removal() ;
164       orb->destroy();
165     }
166   catch(CORBA::COMM_FAILURE& ex) {
167     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
168       }
169   catch(CORBA::SystemException&) {
170     INFOS("Caught a CORBA::SystemException.")
171       }
172   catch(CORBA::Exception&) {
173     INFOS("Caught CORBA::Exception.")
174       }
175   catch(...) {
176     INFOS("Caught unknown exception.")
177       }
178
179   return 0;
180 }
181