Salome HOME
import SalomePro v1.2c
[modules/kernel.git] / src / Session / SALOME_Session_Server.cxx
1 //  SALOME Session : implementation of Session.idl
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   : SALOME_Session_Server.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 # include "Utils_ORB_INIT.hxx"
30 # include "Utils_SINGLETON.hxx"
31 # include "SALOME_NamingService.hxx"
32 #include <iostream>
33 #include <unistd.h>
34 using namespace std;
35
36 // #include <qapplication.h>
37 // #include "Qtappl_example.hxx"
38
39 #include <SALOMEconfig.h>
40 #include CORBA_SERVER_HEADER(SALOME_Session)
41
42 #include "utilities.h"
43
44 #include "SALOME_Session_i.hxx"
45
46 //! CORBA server for SALOME Session
47 /*!
48  *  SALOME_Session Server launches a SALOME session servant.
49  *  The servant registers to the Naming Service.
50  *  See SALOME_Session.idl for interface specification.
51  *
52  *  Main services offered by the servant are:
53  *   - launch GUI
54  *   - stop Session (must be idle)
55  *   - get session state
56  */
57
58 int main(int argc, char **argv)
59 {
60
61   try
62     {
63     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
64     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
65     CORBA::ORB_var &orb = init( argc , argv ) ;
66
67     //
68     long TIMESleep = 250000000;
69         int NumberOfTries = 40;
70         int a;
71         timespec ts_req;
72         ts_req.tv_nsec=TIMESleep;
73         ts_req.tv_sec=0;
74         timespec ts_rem;
75         ts_rem.tv_nsec=0;
76         ts_rem.tv_sec=0;
77         CosNaming::NamingContext_var inc;
78         PortableServer::POA_var poa;
79         CORBA::Object_var theObj;
80         CORBA::Object_var obj;
81         CORBA::Object_var object;
82         SALOME_Logger::Logger_var log;
83         SALOME_NamingService &naming = *SINGLETON_<SALOME_NamingService>::Instance() ;
84         int SESSION=0;
85         const char * Env = getenv("USE_LOGGER"); 
86         int EnvL =0;
87         if ((Env!=NULL) && (strlen(Env)))
88           EnvL=1;
89         CosNaming::Name name;
90         name.length(1);
91         name[0].id=CORBA::string_dup("Logger");    
92         PortableServer::POAManager_var pman; 
93         for (int i = 1; i<=NumberOfTries; i++){
94           if (i!=1) 
95             a=nanosleep(&ts_req,&ts_rem);
96           try{ 
97             obj = orb->resolve_initial_references("RootPOA");
98             if(!CORBA::is_nil(obj))
99             poa = PortableServer::POA::_narrow(obj);
100             if(!CORBA::is_nil(poa))
101             pman = poa->the_POAManager();
102              if(!CORBA::is_nil(orb)) 
103             theObj = orb->resolve_initial_references("NameService");
104             if (!CORBA::is_nil(theObj))
105               inc = CosNaming::NamingContext::_narrow(theObj);
106             }
107           catch( CORBA::COMM_FAILURE& )
108             {
109               MESSAGE( "Session Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service" )
110                 }
111           if(!CORBA::is_nil(inc)) {
112             MESSAGE( "Session Server: Naming Service was found" )
113               if(EnvL==1){
114                 for(int j=1; j<=NumberOfTries; j++){
115                   if (j!=1) 
116                     a=nanosleep(&ts_req, &ts_rem);
117                   try{
118                     object = inc->resolve(name);
119                   }
120                   catch(CosNaming::NamingContext::NotFound){ MESSAGE( "Session Server: Logger Server wasn't found" ) }
121                   catch(...){ MESSAGE( "Session Server: Unknown exception" ) }
122                   if (!CORBA::is_nil(object))
123                     log = SALOME_Logger::Logger::_narrow(object);
124                   if (!CORBA::is_nil(log)){
125                     MESSAGE( "Session Server: Loger Server was found" )
126                       log->ping();
127                     SESSION=1;
128                     break;
129                   }
130                 }}
131           }
132           if ((SESSION==1)||((EnvL==0)&&(!CORBA::is_nil(inc))))
133             break;
134               }
135     //
136    
137     // servant
138
139     SALOME_Session_i * mySALOME_Session = new SALOME_Session_i(argc, argv, orb, poa) ;
140     PortableServer::ObjectId_var mySALOME_Sessionid = poa->activate_object(mySALOME_Session) ;
141     MESSAGE("poa->activate_object(mySALOME_Session)")
142
143     obj = mySALOME_Session->_this() ;
144     CORBA::String_var sior(orb->object_to_string(obj)) ;
145     // MESSAGE("sior(orb->object-to-string(obj))")
146     // cerr << "'" << (char*) sior << "'" << endl ;
147
148     mySALOME_Session->NSregister();
149
150     mySALOME_Session->_remove_ref() ;
151     //MESSAGE("mySALOME_Session->_remove_ref()")
152
153     //DECOMMENT PortableServer::POAManager_var pman = poa->the_POAManager() ;
154     pman->activate() ;
155     MESSAGE("pman->activate()")
156
157     orb->run() ;
158     orb->destroy() ;
159     }
160     catch (CORBA::SystemException&)
161     {
162       INFOS("Caught CORBA::SystemException.")
163     }
164     catch (CORBA::Exception&)
165     {
166       INFOS("Caught CORBA::Exception.")
167     }
168     catch (...)
169     {
170       INFOS("Caught unknown exception.")
171     }
172   return 0 ;
173 }