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