Salome HOME
[EDF29852] : Mecanism of fault tolerant in SALOME_Container to resist against emitted...
[modules/kernel.git] / src / Logger / SALOME_Logger_Server_main.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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
23 //  SALOME Logger : CORBA server managing trace output
24 //  File   : SALOME_Logger_Server.cxx
25 //  Author : Vasily Rusyaev
26 //  Module : SALOME
27 //
28 #include <iostream>
29 #include "SALOME_Logger_Server.hxx"
30 #include "OpUtil.hxx"
31 #include "ArgvKeeper.hxx"
32 #include <SALOMEconfig.h>
33 #include <sys/types.h>
34 #include <stdlib.h>
35 #include "KernelBasis.hxx"
36
37 #ifndef WIN32
38 # include <unistd.h>
39 #else
40 # include "utilities.h" // for compilation type "timespec"
41 #endif
42
43 int main(int argc, char **argv)
44 {
45   if (argc > 2)
46     {
47       std::cout << "usage: SALOME_Logger_Server [output_file]" << std::endl;
48       exit(1);
49     }
50   try
51     {
52       //Initialize the ORB
53       const long TIMESleep = 250000000;
54       const int NumberOfTries = 40;
55       int i;
56       timespec ts_req = {0, TIMESleep};
57       timespec ts_rem = {0, 0};
58       CosNaming::NamingContext_var inc;
59       SALOME_Logger::Logger_var myLoggerRef;
60       CORBA::Object_var theObj;
61       Logger* myLogger;
62       CORBA::Object_var obj;
63       PortableServer::POA_var poa;
64       PortableServer::POAManager_var pman;   
65       setSSLMode(false);
66       SetArgcArgv(argc, argv);
67       CORBA::ORB_var orb = KERNEL::GetRefToORB() ;
68
69       for (i = 1; i <= NumberOfTries; i++)
70           {
71 #ifndef WIN32
72                   if (i != 1) nanosleep(&ts_req, &ts_rem);
73 #else
74                   if (i != 1) Sleep(TIMESleep / 1000000);
75 #endif
76                   try 
77                   {
78                           obj = orb->resolve_initial_references("RootPOA") ;
79                           if(!CORBA::is_nil(obj))
80                                   poa = PortableServer::POA::_narrow(obj) ;
81                           pman = poa->the_POAManager();
82                           // NB. You can activate the POA before or after
83                           // activating objects in that POA.
84                           
85                           // This activates the object in the root POA (by default), and
86                           // returns a reference to it.
87                           //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
88                           //Otherwise, you will get segmentation fault.   
89                           //Get initial naming context
90                           if(!CORBA::is_nil(orb)) 
91                                   theObj = orb->resolve_initial_references("NameService");
92                           //Narrow to NamingContext
93                           if (!CORBA::is_nil(theObj))
94                                   inc = CosNaming::NamingContext::_narrow(theObj);
95                   } catch(CORBA::COMM_FAILURE&) {
96                           //cout<<"Logger Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service"<<endl;
97                   } catch(...) {
98                           //cout<<"Logger Server: Unknown exception dealed with Naming Service" <<endl;
99                   }
100                   
101                   if (!CORBA::is_nil(inc)) {
102                           //      cout<<"Logger Server: Naming Service was found"<<endl; 
103                           break;
104                   }
105       }
106       if (argc == 1)
107                   myLogger = new Logger();
108       else
109                   myLogger = new Logger(argv[1]);
110
111       myLogger->SetOrb(orb);
112       myLoggerRef = myLogger->_this();
113       CosNaming::Name name;
114       name.length(1);
115       name[0].id = CORBA::string_dup("Logger");
116       inc->bind(name,myLoggerRef);
117       myLogger->_remove_ref();
118       pman->activate();   
119       orb->run() ;
120       orb->destroy() ;
121     }  
122   catch(CORBA::COMM_FAILURE&)
123         {
124       std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
125            << "object." << std::endl;
126     }
127   catch(CORBA::SystemException&) 
128     {
129       std::cerr << "Caught CORBA::SystemException." << std::endl;
130     }
131   catch(CORBA::Exception&) 
132     {
133       std::cerr << "Caught CORBA::Exception." << std::endl;
134     }
135   catch(omniORB::fatalException& fe) 
136     {
137       std::cerr << "Caught omniORB::fatalException:" << std::endl;
138       std::cerr << "  file: " << fe.file() << std::endl;
139       std::cerr << "  line: " << fe.line() << std::endl;
140       std::cerr << "  mesg: " << fe.errmsg() << std::endl;
141     }
142   catch(...) 
143     {
144       std::cerr << "Caught unknown exception." << std::endl;
145     }
146   return 0;
147 }