Salome HOME
import SalomePro v1.2c
[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 using namespace std;
30 #include "utilities.h"
31 #include <iostream>
32 #include <unistd.h>
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 "OpUtil.hxx"
39 # include "Utils_ORB_INIT.hxx"
40 # include "Utils_SINGLETON.hxx"
41 # include "Utils_SALOME_Exception.hxx"
42 # include "Utils_CommException.hxx"
43 int main (int argc, char * argv[])
44 {
45
46   try
47     {
48       // Initializing omniORB
49       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
50     
51       // use IOR to find container
52       //if (argc != 2) { return 1; }
53       //CORBA::Object_var obj = orb->string_to_object(argv[1]);
54       //Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
55
56       // Obtain a reference to the root POA
57       //
58      long TIMESleep = 250000000;
59      int NumberOfTries = 40;
60      int a;
61      timespec ts_req;
62      ts_req.tv_nsec=TIMESleep;
63      ts_req.tv_sec=0;
64      timespec ts_rem;
65      ts_rem.tv_nsec=0;
66      ts_rem.tv_sec=0;
67      CosNaming::NamingContext_var inc;
68      PortableServer::POA_var poa;
69      CORBA::Object_var theObj;
70      CORBA::Object_var obj;
71      CORBA::Object_var object;
72      SALOME_Logger::Logger_var log;
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        if (i!=1) 
85          a=nanosleep(&ts_req,&ts_rem);
86        try{ 
87          obj = orb->resolve_initial_references("RootPOA");
88          if(!CORBA::is_nil(obj))
89            poa = PortableServer::POA::_narrow(obj);
90          if(!CORBA::is_nil(poa))
91            manager = poa->the_POAManager();
92          if(!CORBA::is_nil(orb)) 
93            theObj = orb->resolve_initial_references("NameService");
94          if (!CORBA::is_nil(theObj))
95            inc = CosNaming::NamingContext::_narrow(theObj);
96        }
97        catch( CORBA::COMM_FAILURE& )
98          {
99            MESSAGE( "Test Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
100              }
101        if(!CORBA::is_nil(inc)) {
102          MESSAGE( "Test Container: Naming Service was found" )
103            if(EnvL==1){
104              for(int j=1; j<=NumberOfTries; j++){
105                if (j!=1) 
106                  a=nanosleep(&ts_req, &ts_rem);
107                try{
108                  object = inc->resolve(name);
109                }
110                catch(CosNaming::NamingContext::NotFound){ MESSAGE( "Test Container: Logger Server wasn't found" ) }
111                catch(...){ MESSAGE( "Test Container: Unknown exception" ) }
112                if (!CORBA::is_nil(object))
113                  log = SALOME_Logger::Logger::_narrow(object);
114                if (!CORBA::is_nil(log)){
115                  MESSAGE( "Test Container: Loger Server was found" )
116                    log->ping();
117                  TEST_CONTAINER=1;
118                  break;
119                   }
120                 }}
121           }
122           if ((TEST_CONTAINER==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
123             break;
124               }
125       //
126       // Use Name Service to find container
127       SALOME_NamingService _NS(orb) ;
128       string containerName = "/Containers/" ;
129       string hostName = GetHostname();
130       containerName += hostName + "/FactoryServer";
131
132       obj = _NS.Resolve(containerName.c_str()) ;
133       Engines::Container_var iGenFact = Engines::Container::_narrow(obj);
134
135       Engines::TestComponent_var m1;
136     
137       for (int iter = 0; iter < 3 ; iter++)
138         {
139           INFOS("----------------------------------------------------" << iter);   
140           string dirn = getenv("SALOME_ROOT_DIR");
141           dirn += "/lib/libSalomeTestComponentEngine.so";
142           obj = iGenFact->load_impl("SalomeTestComponent",dirn.c_str());
143           m1 = Engines::TestComponent::_narrow(obj);
144           INFOS("recup m1");
145           SCRUTE(m1->instanceName());
146           INFOS("Coucou " << m1->Coucou(1L));
147           iGenFact->remove_impl(m1) ;
148           //iGenFact->finalize_removal() ; // unpredictable results ...
149           sleep(5);
150         }    
151       // Clean-up.
152       iGenFact->finalize_removal() ;
153       orb->destroy();
154     }
155   catch(CORBA::COMM_FAILURE& ex) {
156     INFOS("Caught system exception COMM_FAILURE -- unable to contact the object.")
157       }
158   catch(CORBA::SystemException&) {
159     INFOS("Caught a CORBA::SystemException.")
160       }
161   catch(CORBA::Exception&) {
162     INFOS("Caught CORBA::Exception.")
163       }
164   catch(...) {
165     INFOS("Caught unknown exception.")
166       }
167
168   return 0;
169 }
170