Salome HOME
Fixed incorrect check in method DefineComponentInstance
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_Server.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 //  File   : SALOMEDS_Server.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28
29 #include "utilities.h"
30 #include "Utils_SINGLETON.hxx"
31
32 #include "SALOME_NamingService.hxx"
33 #include "SALOMEDS_StudyManager_i.hxx"
34
35 #include <SALOMEconfig.h>
36 #include CORBA_SERVER_HEADER(SALOMEDS)
37
38 #ifdef CHECKTIME
39 #include <Utils_Timer.hxx>
40 #endif
41 using namespace std;
42
43 // extern "C"
44 // { // for ccmalloc memory debug
45 // void ccmalloc_static_initialization(void);
46 // }
47
48 ///////////////////////////// MAIN ///////////////////////////////////////
49 //////////////////////////////////////////////////////////////////////////
50 int main(int argc, char** argv)
51 {
52   //   ccmalloc_static_initialization();
53
54   MESSAGE("SALOMEDS_Server - main");
55   try 
56     {
57       // Initialise the ORB.
58 #if OMNIORB_VERSION >= 4
59       const char* options[][2] = { { "giopMaxMsgSize", "104857600" }, { 0, 0 } };
60       CORBA::ORB_var orb = CORBA::ORB_init( argc , argv , "omniORB4", options) ;
61 #else
62       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB3");
63       omniORB::MaxMessageSize(100 * 1024 * 1024);
64 #endif      
65       // Obtain a reference to the root POA.
66       long TIMESleep = 500000000;
67       int NumberOfTries = 40;
68       int a;
69       timespec ts_req;
70       ts_req.tv_nsec=TIMESleep;
71       ts_req.tv_sec=0;
72       timespec ts_rem;
73       ts_rem.tv_nsec=0;
74       ts_rem.tv_sec=0;
75       CosNaming::NamingContext_var inc;
76       PortableServer::POA_var poa;
77       CORBA::Object_var theObj;
78       CORBA::Object_var obj;
79       CORBA::Object_var object;
80       int SALOMEDS=0;
81       const char * Env = getenv("USE_LOGGER");
82       int EnvL =0;
83       if ((Env!=NULL) && (strlen(Env)))
84         EnvL=1;
85       CosNaming::Name name;
86       name.length(1);
87       name[0].id=CORBA::string_dup("Logger");    
88       PortableServer::POAManager_var pman; 
89       for (int i = 1; i<=NumberOfTries; i++)
90         {
91           if (i!=1) 
92 #ifndef WNT
93             a=nanosleep(&ts_req,&ts_rem);
94 #else
95                 Sleep(TIMESleep/1000000);
96 #endif
97           try
98             { 
99               obj = orb->resolve_initial_references("RootPOA");
100               if(!CORBA::is_nil(obj))
101                 poa = PortableServer::POA::_narrow(obj);
102               if(!CORBA::is_nil(poa))
103                 pman = poa->the_POAManager();
104               if(!CORBA::is_nil(orb)) 
105                 theObj = orb->resolve_initial_references("NameService"); 
106               if (!CORBA::is_nil(theObj)){
107                 inc = CosNaming::NamingContext::_narrow(theObj);
108                 if(!CORBA::is_nil(inc))
109                   {
110                     MESSAGE( "SalomeDS Server: Naming Service was found" );
111                     if(EnvL==1)
112                       {
113                         CORBA::ORB_var orb1 = CORBA::ORB_init(argc,argv) ;
114                         SALOME_NamingService &NS = *SINGLETON_<SALOME_NamingService>::Instance() ;
115                         NS.init_orb( orb1 ) ;
116                         for(int j=1; j<=NumberOfTries; j++)
117                           {
118                             if (j!=1) 
119 #ifndef WNT
120                               a=nanosleep(&ts_req, &ts_rem);
121 #else
122                               Sleep(TIMESleep/1000000);
123 #endif
124                             try
125                               {
126                                 object = inc->resolve(name);
127                               }
128                             catch(CosNaming::NamingContext::NotFound)
129                               { 
130                                 MESSAGE( "SalomeDS Server: Logger Server wasn't found" ); }
131
132                             catch(...)
133                               {
134                                 MESSAGE( "SalomeDS Server: Unknown exception" );
135                               }
136                             if (!CORBA::is_nil(object))
137                               {
138                                 MESSAGE( "SalomeDS Server: Logger Server was found" );
139                                 SALOMEDS=1;
140                                 break;
141                               }
142                           }
143                       }
144                   }
145               }
146         
147             }
148           catch( const SALOME_Exception &ex )
149             {
150               MESSAGE( "Communication Error : " << ex.what() );
151               return EXIT_FAILURE ;
152             }
153           if ((SALOMEDS==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
154             break;
155         }
156     
157       // We allocate the objects on the heap.  Since these are reference
158       // counted objects, they will be deleted by the POA when they are no
159       // longer needed.    
160       SALOMEDS_StudyManager_i * myStudyManager_i = new  SALOMEDS_StudyManager_i(orb,poa);
161  
162       // Activate the objects.  This tells the POA that the objects are
163       // ready to accept requests.
164       PortableServer::ObjectId_var myStudyManager_iid = poa->activate_object(myStudyManager_i);
165       myStudyManager_i->register_name("/myStudyManager");
166        
167       // Obtain a POAManager, and tell the POA to start accepting
168       // requests on its objects.
169       pman = poa->the_POAManager();
170       pman->activate();
171
172 #ifdef CHECKTIME
173       Utils_Timer timer;
174       timer.Start();
175       timer.Stop();
176       MESSAGE("SALOME_Registry_Server.cxx - orb->run()");
177       timer.ShowAbsolute();
178 #endif
179       orb->run();
180       orb->destroy();
181     }
182   catch(CORBA::SystemException&)
183     {
184       MESSAGE( "Caught CORBA::SystemException." );
185     }
186   catch(CORBA::Exception&)
187     {
188       MESSAGE( "Caught CORBA::Exception." );
189     }
190   catch(omniORB::fatalException& fe)
191     {
192       MESSAGE( "Caught omniORB::fatalException:" );
193       MESSAGE( "  file: " << fe.file() );
194       MESSAGE( "  line: " << fe.line() );
195       MESSAGE( "  mesg: " << fe.errmsg() );
196     }
197   catch(...) 
198     {
199       MESSAGE( "Caught unknown exception." );
200     }
201   return 0;
202 }