Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / Session / Session_ServerLauncher.cxx
1 //  SALOME Session : implementation of Session_ServerLauncher.cxx
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   : Session_ServerLauncher.xx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include "Session_ServerLauncher.hxx"
30
31 #include "Utils_SALOME_Exception.hxx"
32 #include "utilities.h"
33 using namespace std;
34 /*! 
35    default constructor not for use
36  */
37 Session_ServerLauncher::Session_ServerLauncher()
38 {
39   ASSERT(0); // must not be called
40 }
41
42 /*! 
43   constructor
44 */
45 Session_ServerLauncher::Session_ServerLauncher(int argc,
46                                                char ** argv, 
47                                                CORBA::ORB_ptr orb, 
48                                                PortableServer::POA_ptr poa,
49                                                QMutex *GUIMutex,
50                                                QWaitCondition *ServerLaunch,
51                                                QMutex *SessionMutex,
52                                                QWaitCondition *SessionStarted)
53 {
54   _argc = argc;
55   _argv = argv;
56   _orb = CORBA::ORB::_duplicate(orb);
57   _root_poa = PortableServer::POA::_duplicate(poa);
58   _GUIMutex = GUIMutex;
59   _ServerLaunch = ServerLaunch;
60   _SessionMutex = SessionMutex;
61   _SessionStarted = SessionStarted;
62
63   // start thread
64   start();
65 }
66
67 /*! 
68   destructor
69 */
70 Session_ServerLauncher::~Session_ServerLauncher()
71 {
72 }
73
74 /*! 
75   Check args and activate servers
76 */
77 void Session_ServerLauncher::run()
78 {
79   // wait until main thread is ready
80   _GUIMutex->lock();          // ... lock mutex (it is unlocked my calling thread 
81                               // wait condition's wait(mutex)
82   _GUIMutex->unlock();        // ... and unlock it 'cause it is not more needed
83
84   // wake main thread
85   _ServerLaunch->wakeAll();
86
87   CheckArgs();
88   ActivateAll();
89
90   // wait until main thread is ready
91   _GUIMutex->lock();          // ... lock mutex (it is unlocked my calling thread 
92                               // wait condition's wait(mutex)
93   _GUIMutex->unlock();        // ... and unlock it 'cause it is not more needed
94
95   // wake main thread
96   _ServerLaunch->wakeAll();
97
98   // run ORB
99   _orb->run(); // this thread waits, during omniORB process events
100 }
101
102 /*! 
103   controls and dispatchs arguments given with command
104 */
105 void Session_ServerLauncher::CheckArgs()
106 {
107   int argState = 0;
108   ServArg aServArg(0,0,0);
109   _argCopy.reserve(_argc);
110   for (int iarg=0; iarg <_argc; iarg++)
111     {
112       SCRUTE(iarg);
113       SCRUTE(_argv[iarg]);
114       _argCopy.push_back(_argv[iarg]);
115       switch (argState)
116         {
117         case 0: // looking for "--with"
118           {
119             if (strcmp(_argv[iarg],"--with")==0)
120               argState = 1;
121             break;
122           }
123         case 1: // looking for server type
124           {
125             for (int i=0; i<Session_ServerThread::NB_SRV_TYP; i++)
126                 if (strcmp(_argv[iarg],Session_ServerThread::_serverTypes[i])==0)
127                   {
128                     aServArg._servType = i;
129                     argState = 2;
130                     break;
131                   }
132             break;
133           }
134         case 2: // looking for "("
135           {
136             if (strcmp(_argv[iarg],"(")!=0)
137               {
138                 INFOS("parenthesis '(' is required here...");
139                 for (int i=0; i<iarg; i++)
140                   cerr << _argv[i] << " ";
141                 cerr << endl;
142                 throw SALOME_Exception(LOCALIZED("Error in command arguments, missing prenthesis"));
143               } 
144             else
145               {
146                 aServArg._firstArg=iarg+1;    // arg after '('
147                 argState = 3;
148               }
149             break;
150           }
151         case 3: // looking for arguments
152           {
153             if (strcmp(_argv[iarg],")")==0)   // end of arguments = ')'
154               {
155                 aServArg._lastArg=iarg-1;     // arg before ')'
156                 MESSAGE("server : "<< Session_ServerThread::_serverTypes[aServArg._servType]);
157                 for (int i=aServArg._firstArg; i<=aServArg._lastArg; i++)
158                   MESSAGE("  arg : " << _argCopy[i]);
159                 _argServToLaunch.push_back(aServArg);
160                 argState = 0;
161               } 
162             break;
163           }
164         default:
165           {
166             ASSERT(0);
167             break;
168           }
169         }
170     }
171   if (argState == 1)
172     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing server type"));
173   if (argState == 2)
174     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing parenthesis '('"));
175   if (argState == 3)
176     throw SALOME_Exception(LOCALIZED("Error in command arguments, missing parenthesis ')'"));
177 }
178
179 void Session_ServerLauncher::ActivateAll()
180 {
181   list<ServArg>::iterator itServ;
182   for (itServ = _argServToLaunch.begin(); itServ !=_argServToLaunch.end(); itServ++)
183   {
184     int argc = 2 + (*itServ)._lastArg - (*itServ)._firstArg;
185     char** argv = new char*[argc+1];
186     argv[argc]=0; // for Engines_Container_i constructor...
187     int servType = (*itServ)._servType;
188     argv[0]=strdup(Session_ServerThread::_serverTypes[servType]);
189     if (argc>1)
190     {
191       for (int i=0; i<argc-1; i++)
192         //argv[i+1] = _argCopy[(*itServ)._firstArg + i].c_str();
193         argv[i+1] = _argv[(*itServ)._firstArg + i];
194     }
195
196     std::cout << "*** activating [" << argc << "] : " << argv[0] << std::endl;
197
198     Session_ServerThread* aServerThread
199       = new Session_ServerThread(argc, argv, _orb,_root_poa);
200     _serverThreads.push_front(aServerThread);
201     
202     aServerThread->Init();
203   }
204
205   // Always launch Session Server
206   std::cout << "*** activating [ SESSION ] " << std::endl;
207
208   int argc=1;
209   char** argv = new char*[argc];
210   argv[0] = "Session";
211   Session_SessionThread* aServerThread
212     = new Session_SessionThread(argc, argv, _orb,_root_poa,_SessionMutex,_SessionStarted);
213   _serverThreads.push_front(aServerThread);
214
215   aServerThread->Init();
216 }
217
218 /*! 
219   Destruction des classes serveur dans l'ordre inverse de creation
220 */
221 void Session_ServerLauncher::KillAll()
222 {
223   MESSAGE("Session_ServerLauncher::KillAll()");
224   list<Session_ServerThread*>::reverse_iterator itServ;
225   for (itServ = _serverThreads.rbegin(); itServ !=_serverThreads.rend(); itServ++)
226   {
227     delete (*itServ);
228   }
229 }