Back to index

salome-gui  6.5.0
Session_ServerThread.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Session : implementation of Session_ServerThread.cxx
00024 //  File   : Session_ServerThread.cxx
00025 //  Author : Paul RASCLE, EDF
00026 
00027 #include "Session_ServerThread.hxx"
00028 
00029 #include <SALOME_NamingService.hxx>
00030 #include <SALOME_Container_i.hxx>
00031 #include <SALOME_Launcher.hxx>
00032 #include <SALOMEDSClient_ClientFactory.hxx>
00033 #include <SALOME_ModuleCatalog_impl.hxx>
00034 #include <RegistryService.hxx>
00035 
00036 #include "Session_Session_i.hxx"
00037 
00038 #include <Utils_ORB_INIT.hxx>
00039 #include <Utils_SINGLETON.hxx>
00040 #include <Utils_SALOME_Exception.hxx>
00041 #include <Basics_Utils.hxx>
00042 #include <NamingService_WaitForServerReadiness.hxx>
00043 #include <utilities.h>
00044 
00045 #include <cstdlib>
00046 #include <ctime>
00047 
00048 #include <QMutex>
00049 #include <QWaitCondition>
00050 
00051 const int Session_ServerThread::NB_SRV_TYP = 6;
00052 const char* Session_ServerThread::_serverTypes[NB_SRV_TYP] = {"Container",
00053                                                               "ModuleCatalog",
00054                                                               "Registry",
00055                                                               "SALOMEDS",
00056                                                               "Session",
00057                                                               "ContainerManager"};
00058 
00062 Session_ServerThread::Session_ServerThread()
00063 {
00064   ASSERT(0); // must not be called
00065 }
00066 
00070 Session_ServerThread::Session_ServerThread(int argc,
00071                                            char ** argv, 
00072                                            CORBA::ORB_ptr orb, 
00073                                            PortableServer::POA_ptr poa)
00074 {
00075   //MESSAGE("Session_ServerThread Constructor " << argv[0]);
00076   _argc = argc;
00077   _argv = new char*[ _argc + 1 ];
00078   _argv[_argc] = 0;
00079   for (int i = 0; i < _argc; i++ )
00080     _argv[i] = strdup( argv[i] );
00081 
00082   _orb = CORBA::ORB::_duplicate(orb);
00083   _root_poa = PortableServer::POA::_duplicate(poa);
00084   _servType =-1;
00085   _NS = new SALOME_NamingService(_orb); // one instance per server to limit
00086                                         // multi thread coherence problems
00087 }
00088 
00092 Session_ServerThread::~Session_ServerThread()
00093 {
00094   //MESSAGE("~Session_ServerThread "<< _argv[0]);
00095   delete _NS;
00096   for (int i = 0; i <_argc ; i++ )
00097     free( _argv[i] );
00098   delete[] _argv;
00099 }
00100 
00105 void Session_ServerThread::Init()
00106 {
00107   MESSAGE("Session_ServerThread::Init "<< _argv[0]); 
00108 
00109   int i;
00110   for (i=0; i<_argc; i++) SCRUTE(_argv[i]);
00111 
00112   for (i=0; i<NB_SRV_TYP; i++) {
00113     if (strcmp(_argv[0],_serverTypes[i])==0) {
00114       _servType = i;
00115       MESSAGE("Server Thread type : "<<_serverTypes[i]);
00116       switch (_servType) {
00117       case 0:  // Container
00118         {
00119           NamingService_WaitForServerReadiness(_NS,"/Registry");
00120           NamingService_WaitForServerReadiness(_NS,"/ContainerManager");
00121           ActivateContainer(_argc, _argv);
00122           break;
00123         }
00124       case 1:  // ModuleCatalog
00125         {
00126           NamingService_WaitForServerReadiness(_NS,"/Registry");
00127           ActivateModuleCatalog(_argc, _argv);
00128           break;
00129         }
00130       case 2:  // Registry
00131         {
00132           NamingService_WaitForServerReadiness(_NS,"");
00133           ActivateRegistry(_argc, _argv);
00134           break;
00135         }
00136       case 3:  // SALOMEDS
00137         {
00138           NamingService_WaitForServerReadiness(_NS,"/Kernel/ModulCatalog");
00139           ActivateSALOMEDS(_argc, _argv);
00140           break;
00141         }
00142       case 4:  // Session
00143         {
00144           NamingService_WaitForServerReadiness(_NS,"/myStudyManager");
00145           std::string containerName = "/Containers/";
00146           containerName = containerName + Kernel_Utils::GetHostname();
00147           containerName = containerName + "/FactoryServer";
00148           NamingService_WaitForServerReadiness(_NS,containerName);
00149           ActivateSession(_argc, _argv);
00150           break;
00151         }
00152       case 5: // Container Manager
00153         {
00154           NamingService_WaitForServerReadiness(_NS,"");
00155           ActivateContainerManager(_argc, _argv);
00156           break;
00157         }
00158       default:
00159         {
00160           ASSERT(0);
00161           break;
00162         }
00163       }
00164     }
00165   }
00166 }
00167 
00168 void Session_ServerThread::ActivateModuleCatalog(int argc,
00169                                                  char ** argv)
00170 {
00171   try {
00172     MESSAGE("ModuleCatalog thread started");
00173     // allocation on heap to allow destruction by POA
00174     
00175     SALOME_ModuleCatalogImpl* Catalogue_i
00176       = new SALOME_ModuleCatalogImpl(argc, argv);
00177     
00178     // Tell the POA that the objects are ready to accept requests.
00179     
00180     PortableServer::ObjectId_var id = _root_poa->activate_object (Catalogue_i);
00181     Catalogue_i->_remove_ref();
00182     
00183     CORBA::Object_var myCata = Catalogue_i->_this();
00184     _NS->Register(myCata ,"/Kernel/ModulCatalog");
00185   }
00186   catch(CORBA::SystemException&) {
00187     INFOS( "Caught CORBA::SystemException." );
00188   }
00189   catch(CORBA::Exception&) {
00190     INFOS( "Caught CORBA::Exception." );
00191   }
00192   catch(omniORB::fatalException& fe) {
00193     INFOS( "Caught omniORB::fatalException:" );
00194     INFOS( "  file: " << fe.file() );
00195     INFOS( "  line: " << fe.line() );
00196     INFOS( "  mesg: " << fe.errmsg() );
00197   }
00198   catch(...) {
00199     INFOS( "Caught unknown exception." );
00200   }
00201 }
00202 
00203 void Session_ServerThread::ActivateSALOMEDS(int argc,
00204                                             char ** argv)
00205 {
00206   try {
00207     MESSAGE("SALOMEDS thread started");
00208     // We allocate the objects on the heap.  Since these are reference
00209     // counted objects, they will be deleted by the POA when they are no
00210     // longer needed.    
00211     
00212     ClientFactory::createStudyManager(_orb,_root_poa);
00213   }
00214   catch(CORBA::SystemException&) {
00215     INFOS( "Caught CORBA::SystemException." );
00216   }
00217   catch(CORBA::Exception&) {
00218     INFOS( "Caught CORBA::Exception." );
00219   }
00220   catch(omniORB::fatalException& fe) {
00221     INFOS( "Caught omniORB::fatalException:" );
00222     INFOS( "  file: " << fe.file() );
00223     INFOS( "  line: " << fe.line() );
00224     INFOS( "  mesg: " << fe.errmsg() );
00225   }
00226   catch(...) {
00227     INFOS( "Caught unknown exception." );
00228   }
00229 }
00230 
00231 void Session_ServerThread::ActivateRegistry(int argc,
00232                                             char ** argv)
00233 {
00234   MESSAGE("Registry thread started");
00235   SCRUTE(argc); 
00236   if ( argc<3 ) {
00237     INFOS("you must provide the Salome session name when you call SALOME_Registry_Server");
00238     throw CommException("you must provide the Salome session name when you call SALOME_Registry_Server");
00239   }
00240   const char *ptrSessionName=0;
00241 
00242   int k=0 ;
00243   for ( k=1 ; k<argc ; k++ ) {
00244     if ( strcmp(argv[k],"--salome_session")==0 ) {
00245       ptrSessionName=argv[k+1];
00246       break;
00247     }
00248   }
00249   ASSERT(ptrSessionName) ;
00250   ASSERT(strlen( ptrSessionName )>0);
00251   const char *registryName = "Registry";
00252   Registry::Components_var varComponents;
00253   try {
00254     RegistryService *ptrRegistry = new RegistryService;
00255     ptrRegistry->SessionName( ptrSessionName );
00256     ptrRegistry->SetOrb(_orb);
00257     varComponents = ptrRegistry->_this();
00258     ptrRegistry->_remove_ref(); //let poa manage registry service deletion
00259     // The RegistryService must not already exist.
00260     
00261     try {
00262       CORBA::Object_var pipo = _NS->Resolve( registryName );
00263       if (CORBA::is_nil(pipo) )  throw ServiceUnreachable();
00264       INFOS("RegistryService servant already existing" );
00265       ASSERT(0);
00266     }
00267     catch( const ServiceUnreachable &/*ex*/ ) {
00268     }
00269     catch( const CORBA::Exception &/*exx*/ ) {
00270     }
00271     std::string absoluteName = std::string("/") + registryName;
00272     _NS->Register( varComponents , absoluteName.c_str() );
00273   }
00274   catch( const SALOME_Exception &ex ) {
00275     INFOS( "Communication Error : " << ex.what() );
00276     ASSERT(0);
00277   }
00278 }
00279 
00280 void Session_ServerThread::ActivateContainerManager(int argc,
00281                                              char ** argv)
00282 {
00283   try {
00284     PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
00285     std::cout << "Activate SalomeLauncher ......!!!! " << std::endl;
00286     new SALOME_Launcher(_orb,root_poa);
00287   }
00288   catch(CORBA::SystemException&) {
00289     INFOS("Caught CORBA::SystemException.");
00290   }
00291   catch(PortableServer::POA::WrongPolicy&) {
00292     INFOS("Caught CORBA::WrongPolicyException.");
00293   }
00294   catch(PortableServer::POA::ServantAlreadyActive&) {
00295     INFOS("Caught CORBA::ServantAlreadyActiveException");
00296   }
00297   catch(CORBA::Exception&) {
00298     INFOS("Caught CORBA::Exception.");
00299   }
00300   catch(...) {
00301     INFOS("Caught unknown exception.");
00302   }
00303 }
00304 
00305 void Session_ServerThread::ActivateContainer(int argc,
00306                                              char ** argv)
00307 {
00308   try {
00309     MESSAGE("Container thread started");
00310     
00311     // get or create the child POA
00312     
00313     PortableServer::POA_var factory_poa;
00314     try {
00315       factory_poa = _root_poa->find_POA("factory_poa",0);
00316       // 0 = no activation (already done if exists)
00317     }
00318     catch (PortableServer::POA::AdapterNonExistent&) {
00319       MESSAGE("factory_poa does not exists, create...");
00320       // define policy objects     
00321       PortableServer::ImplicitActivationPolicy_var implicitActivation =
00322         _root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION);
00323       // default = NO_IMPLICIT_ACTIVATION
00324       PortableServer::ThreadPolicy_var threadPolicy =
00325         _root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL);
00326       // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
00327       
00328       // create policy list
00329       CORBA::PolicyList policyList;
00330       policyList.length(2);
00331       policyList[0] = PortableServer::ImplicitActivationPolicy::
00332         _duplicate(implicitActivation);
00333       policyList[1] = PortableServer::ThreadPolicy::
00334         _duplicate(threadPolicy);
00335       
00336       PortableServer::POAManager_var nil_mgr
00337         = PortableServer::POAManager::_nil();
00338       factory_poa = _root_poa->create_POA("factory_poa",
00339                                           nil_mgr,
00340                                           policyList);
00341       //with nil_mgr instead of pman,
00342       //a new POA manager is created with the new POA
00343       
00344       // destroy policy objects
00345       implicitActivation->destroy();
00346       threadPolicy->destroy();
00347       
00348       // obtain the factory poa manager
00349       PortableServer::POAManager_var pmanfac = factory_poa->the_POAManager();
00350       pmanfac->activate();
00351       MESSAGE("pmanfac->activate()");
00352     }
00353     
00354     char *containerName = (char*)"";
00355     if (argc >1) {
00356       containerName = argv[1];
00357     }
00358     
00359     new Engines_Container_i(_orb, _root_poa, containerName , argc , argv , true , false);
00360   }
00361   catch(CORBA::SystemException&) {
00362     INFOS("Caught CORBA::SystemException.");
00363   }
00364   catch(PortableServer::POA::WrongPolicy&) {
00365     INFOS("Caught CORBA::WrongPolicyException.");
00366   }
00367   catch(PortableServer::POA::ServantAlreadyActive&) {
00368     INFOS("Caught CORBA::ServantAlreadyActiveException");
00369   }
00370   catch(CORBA::Exception&) {
00371     INFOS("Caught CORBA::Exception.");
00372   }
00373   catch(...) {
00374     INFOS("Caught unknown exception.");
00375   }
00376 }
00377 
00378 void Session_ServerThread::ActivateSession(int argc,
00379                                            char ** argv)
00380 {
00381   MESSAGE("Session_ServerThread::ActivateSession() not implemented!");
00382 }
00383 
00387 Session_SessionThread::Session_SessionThread(int argc,
00388                                              char** argv, 
00389                                              CORBA::ORB_ptr orb, 
00390                                              PortableServer::POA_ptr poa,
00391                                              QMutex* GUIMutex,
00392                                              QWaitCondition* GUILauncher)
00393 : Session_ServerThread(argc, argv, orb, poa),
00394   _GUIMutex( GUIMutex ),
00395   _GUILauncher( GUILauncher )
00396 {
00397 }
00398 
00402 Session_SessionThread::~Session_SessionThread()
00403 {
00404 }
00405 
00406 void Session_SessionThread::ActivateSession(int argc,
00407                                             char ** argv)
00408 {
00409   try {
00410     MESSAGE("Session thread started");
00411     SALOME_Session_i * mySALOME_Session
00412       = new SALOME_Session_i(argc, argv, _orb, _root_poa, _GUIMutex, _GUILauncher) ;
00413     PortableServer::ObjectId_var mySALOME_Sessionid
00414       = _root_poa->activate_object(mySALOME_Session);
00415     MESSAGE("poa->activate_object(mySALOME_Session)");
00416     
00417     CORBA::Object_var obj = mySALOME_Session->_this();
00418     CORBA::String_var sior(_orb->object_to_string(obj));
00419     mySALOME_Session->_remove_ref();
00420     
00421     mySALOME_Session->NSregister();
00422   }
00423   catch (CORBA::SystemException&) {
00424     INFOS("Caught CORBA::SystemException.");
00425   }
00426   catch (CORBA::Exception&) {
00427     INFOS("Caught CORBA::Exception.");
00428   }
00429   catch (...) {
00430     INFOS("Caught unknown exception.");
00431   }
00432 }