Back to index

salome-gui  6.5.0
Session_ServerLauncher.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_ServerLauncher.cxx
00024 //  File   : Session_ServerLauncher.xx
00025 //  Author : Paul RASCLE, EDF
00026 
00027 #include "Session_ServerLauncher.hxx"
00028 #include "Session_ServerThread.hxx"
00029 
00030 #include "Utils_SALOME_Exception.hxx"
00031 #include "utilities.h"
00032 
00033 #include <QMutex>
00034 #include <QWaitCondition>
00035 
00039 Session_ServerLauncher::Session_ServerLauncher()
00040 {
00041   ASSERT(0); // must not be called
00042 }
00043 
00047 Session_ServerLauncher::Session_ServerLauncher(int argc,
00048                                                char ** argv, 
00049                                                CORBA::ORB_ptr orb, 
00050                                                PortableServer::POA_ptr poa,
00051                                                QMutex *GUIMutex,
00052                                                QWaitCondition *ServerLaunch,
00053                                                QMutex *SessionMutex,
00054                                                QWaitCondition *SessionStarted)
00055 {
00056   _argc = argc;
00057   _argv = argv;
00058   _orb = CORBA::ORB::_duplicate(orb);
00059   _root_poa = PortableServer::POA::_duplicate(poa);
00060   _GUIMutex = GUIMutex;
00061   _ServerLaunch = ServerLaunch;
00062   _SessionMutex = SessionMutex;
00063   _SessionStarted = SessionStarted;
00064 
00065   // start thread
00066   start();
00067 }
00068 
00072 Session_ServerLauncher::~Session_ServerLauncher()
00073 {
00074 }
00075 
00079 void Session_ServerLauncher::run()
00080 {
00081   // wait until main thread is ready
00082   _GUIMutex->lock();          // ... lock mutex (it is unlocked my calling thread 
00083                               // wait condition's wait(mutex)
00084   _GUIMutex->unlock();        // ... and unlock it 'cause it is not more needed
00085 
00086   // wake main thread
00087   _ServerLaunch->wakeAll();
00088 
00089   CheckArgs();
00090   ActivateAll();
00091 
00092   // wait until main thread is ready
00093   _GUIMutex->lock();          // ... lock mutex (it is unlocked my calling thread 
00094                               // wait condition's wait(mutex)
00095   _GUIMutex->unlock();        // ... and unlock it 'cause it is not more needed
00096 
00097   // wake main thread
00098   _ServerLaunch->wakeAll();
00099 
00100   // run ORB
00101   //_orb->run(); // No need to call orb->run() : it waits on a lock. Qt is already waiting in the mainloop.
00102 }
00103 
00107 void Session_ServerLauncher::CheckArgs()
00108 {
00109   int argState = 0;
00110   ServArg aServArg(0,0,0);
00111   _argCopy.reserve(_argc);
00112   for (int iarg=0; iarg <_argc; iarg++)
00113     {
00114       SCRUTE(iarg);
00115       SCRUTE(_argv[iarg]);
00116       _argCopy.push_back(_argv[iarg]);
00117       switch (argState)
00118         {
00119         case 0: // looking for "--with"
00120           {
00121             if (strcmp(_argv[iarg],"--with")==0)
00122               argState = 1;
00123             break;
00124           }
00125         case 1: // looking for server type
00126           {
00127            // Temporary solution
00128            // Issue 21337 - no more SalomeApp_Engine_i activation here
00129            // TODO: To be removed as soon as any trace of SalomeAppEngine
00130            // has been eliminated from KERNEL scripts
00131            if (strcmp(_argv[iarg], "SalomeAppEngine")==0){
00132              argState = 0;
00133              iarg += 2; // skipping "()" 
00134              break;
00135            }
00136            // Temporary solution
00137 
00138             for (int i=0; i<Session_ServerThread::NB_SRV_TYP; i++)
00139                 if (strcmp(_argv[iarg],Session_ServerThread::_serverTypes[i])==0)
00140                   {
00141                     aServArg._servType = i;
00142                     argState = 2;
00143                     break;
00144                   }
00145             break;
00146           }
00147         case 2: // looking for "("
00148           {
00149             if (strcmp(_argv[iarg],"(")!=0)
00150               {
00151                 INFOS("parenthesis '(' is required here...");
00152                 for (int i=0; i<iarg; i++)
00153                   std::cerr << _argv[i] << " ";
00154                 std::cerr << std::endl;
00155                 throw SALOME_Exception(LOCALIZED("Error in command arguments, missing prenthesis"));
00156               } 
00157             else
00158               {
00159                 aServArg._firstArg=iarg+1;    // arg after '('
00160                 argState = 3;
00161               }
00162             break;
00163           }
00164         case 3: // looking for arguments
00165           {
00166             if (strcmp(_argv[iarg],")")==0)   // end of arguments = ')'
00167               {
00168                 aServArg._lastArg=iarg-1;     // arg before ')'
00169                 MESSAGE("server : "<< Session_ServerThread::_serverTypes[aServArg._servType]);
00170                 for (int i=aServArg._firstArg; i<=aServArg._lastArg; i++)
00171                   MESSAGE("  arg : " << _argCopy[i]);
00172                 _argServToLaunch.push_back(aServArg);
00173                 argState = 0;
00174               } 
00175             break;
00176           }
00177         default:
00178           {
00179             ASSERT(0);
00180             break;
00181           }
00182         }
00183     }
00184   if (argState == 1)
00185     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing server type"));
00186   if (argState == 2)
00187     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing parenthesis '('"));
00188   if (argState == 3)
00189     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing parenthesis ')'"));
00190 }
00191 
00192 void Session_ServerLauncher::ActivateAll()
00193 {
00194   std::list<ServArg>::iterator itServ;
00195   for (itServ = _argServToLaunch.begin(); itServ !=_argServToLaunch.end(); itServ++)
00196   {
00197     int argc = 2 + (*itServ)._lastArg - (*itServ)._firstArg;
00198     char** argv = new char*[argc+1];
00199     argv[argc]=0; // for Engines_Container_i constructor...
00200     int servType = (*itServ)._servType;
00201     argv[0]=strdup(Session_ServerThread::_serverTypes[servType]);
00202     if (argc>1)
00203     {
00204       for (int i=0; i<argc-1; i++)
00205         //argv[i+1] = _argCopy[(*itServ)._firstArg + i].c_str();
00206               argv[i+1] = _argv[(*itServ)._firstArg + i];
00207     }
00208 
00209     MESSAGE("*** activating [" << argc << "] : " << argv[0]);
00210 
00211     Session_ServerThread* aServerThread
00212       = new Session_ServerThread(argc, argv, _orb,_root_poa);
00213     _serverThreads.push_front(aServerThread);
00214     
00215     aServerThread->Init();
00216     free( argv[0] );
00217     delete[] argv;
00218   }
00219 
00220   // Always launch Session Server
00221   MESSAGE("*** activating [ SESSION ] ");
00222 
00223   int argc=1;
00224   char** argv = new char*[argc];
00225   argv[0] = (char*)"Session";
00226   Session_SessionThread* aServerThread
00227     = new Session_SessionThread(argc, argv, _orb,_root_poa,_SessionMutex,_SessionStarted);
00228   _serverThreads.push_front(aServerThread);
00229   aServerThread->Init();
00230   delete[] argv;
00231 }
00232 
00236 void Session_ServerLauncher::KillAll()
00237 {
00238   MESSAGE("Session_ServerLauncher::KillAll()");
00239   std::list<Session_ServerThread*>::reverse_iterator itServ;
00240   for (itServ = _serverThreads.rbegin(); itServ !=_serverThreads.rend(); itServ++)
00241   {
00242     delete (*itServ);
00243   }
00244 }