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