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