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