Salome HOME
eeed8aed1cbadafd5ed1200199f722cf7306fb5a
[modules/kernel.git] / src / Container / Component_i.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : Component_i.cxx
4 // Created   : jeu jui 12 08:04:40 CEST 2001
5 // Author    : Paul RASCLE, EDF - MARC TAJCHMAN, CEA
6 // Project   : SALOME
7 // Copyright : EDF 2001 - CEA 2001
8 // $Header$
9 //=============================================================================
10
11 #include "SALOME_Component_i.hxx"
12 #include "RegistryConnexion.hxx"
13 #include "OpUtil.hxx"
14 #include <stdio.h>
15 #include <dlfcn.h>
16 #include "utilities.h"
17
18 extern bool _Sleeping ;
19
20 Engines_Component_i::Engines_Component_i()
21 {
22 //  MESSAGE("Component constructor");
23 }
24
25 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
26                                          PortableServer::POA_ptr poa, 
27                                          PortableServer::ObjectId * contId, 
28                                          const char *instanceName,
29                                          const char *interfaceName,
30                                          bool notif) :
31   _instanceName(instanceName), _interfaceName(interfaceName),
32   _myConnexionToRegistry(0), _ThreadId(0) , _graphName("") , _nodeName("") {
33   MESSAGE("Component constructor with instanceName "<< _instanceName);
34   _orb = CORBA::ORB::_duplicate(orb);
35   _poa = PortableServer::POA::_duplicate(poa);
36   _contId = contId ;
37   CORBA::Object_var o = _poa->id_to_reference(*contId); // container ior...
38   const CORBA::String_var ior = _orb->object_to_string(o);
39   _myConnexionToRegistry = new RegistryConnexion(0, 0, ior, "theSession", _instanceName.c_str());
40
41   _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
42 }
43
44 // Constructeur pour composant parallele: ne pas faire appel au registry!!
45 Engines_Component_i::Engines_Component_i(CORBA::ORB_ptr orb,
46                                          PortableServer::POA_ptr poa, 
47                                          PortableServer::ObjectId * contId, 
48                                          const char *instanceName,
49                                          const char *interfaceName,
50                                          int flag,
51                                          bool notif )
52   : _instanceName(instanceName),
53     _interfaceName(interfaceName),
54     _myConnexionToRegistry(0),
55     _ThreadId(0)
56 {
57 //  MESSAGE("Component constructor with instanceName "<< _instanceName);
58   _orb = CORBA::ORB::_duplicate(orb);
59   _poa = PortableServer::POA::_duplicate(poa);
60   _contId = contId ;
61   //  CORBA::Object_var myself = this->_this(); //appel a _this = increment reference
62
63   _notifSupplier = new NOTIFICATION_Supplier(instanceName, notif);
64 }
65
66 Engines_Component_i::~Engines_Component_i()
67 {
68   MESSAGE("Component destructor");
69 //   delete _myConnexionToRegistry;
70 //   _myConnexionToRegistry = 0 ;
71 }
72
73 char* Engines_Component_i::instanceName() {
74    return CORBA::string_dup(_instanceName.c_str()) ;
75 }
76
77 char* Engines_Component_i::interfaceName() {
78    return CORBA::string_dup(_interfaceName.c_str()) ;
79 }
80
81 void Engines_Component_i::ping()
82 {
83   MESSAGE("Engines_Component_i::ping() pid "<< getpid() << " threadid "
84           << pthread_self());
85 }
86
87 void Engines_Component_i::destroy()
88 {
89   MESSAGE("Engines_Component_i::destroy()");
90
91   delete _notifSupplier;
92   _notifSupplier = 0;
93
94   delete _myConnexionToRegistry;
95   _myConnexionToRegistry = 0 ;
96   _poa->deactivate_object(*_id) ;
97   CORBA::release(_poa) ;
98   delete(_id) ;
99   _thisObj->_remove_ref();
100   MESSAGE("Engines_Component_i::destroyed") ;
101 }
102
103 Engines::Container_ptr Engines_Component_i::GetContainerRef()
104 {
105   MESSAGE("Engines_Component_i::GetContainerRef");
106   CORBA::Object_ptr o = _poa->id_to_reference(*_contId) ;
107   return Engines::Container::_narrow(o);
108 }
109
110 PortableServer::ObjectId * Engines_Component_i::getId()
111 {
112 //  MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
113   return _id ;
114 }
115
116 void Engines_Component_i::beginService(const char *serviceName)
117 {
118   MESSAGE("Send BeginService notification for " << serviceName << endl
119           << "Component instance : " << _instanceName << endl << endl);
120   _ThreadId = pthread_self() ;
121   _serviceName = serviceName ;
122   if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) ) {
123     perror("pthread_setcanceltype ") ;
124     exit(0) ;
125   }
126   if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) ) {
127     perror("pthread_setcancelstate ") ;
128     exit(0) ;
129   }
130   MESSAGE("Return from BeginService for " << serviceName
131           << " ThreadId " << _ThreadId
132           << " _graphName " << _graphName << " _nodeName " << _nodeName );
133 }
134
135 void Engines_Component_i::endService(const char *serviceName)
136 {
137   MESSAGE("Send EndService notification for " << serviceName << endl
138           << "Component instance : " << _instanceName << endl << endl);
139   _ThreadId = 0 ;
140 }
141
142 void Engines_Component_i::Names( const char * graphName ,
143                                  const char * nodeName ) {
144   _graphName = graphName ;
145   _nodeName = nodeName ;
146 //  MESSAGE("Engines_Component_i::Names( '" << _graphName << "' , '"
147 //          << _nodeName << "' )");
148 }
149
150 char* Engines_Component_i::graphName() {
151   return CORBA::string_dup( _graphName.c_str() ) ;
152 }
153
154 char* Engines_Component_i::nodeName() {
155   return CORBA::string_dup( _nodeName.c_str() ) ;
156 }
157
158 bool Killer( int ThreadId , int signum ) {
159   if ( ThreadId ) {
160     if ( signum == 0 ) {
161       if ( pthread_cancel( ThreadId ) ) {
162         perror("Killer pthread_cancel error") ;
163         return false ;
164       }
165       else {
166         MESSAGE("Killer : ThreadId " << ThreadId << " pthread_cancel") ;
167       }
168     }
169     else {
170       if ( pthread_kill( ThreadId , signum ) == -1 ) {
171         perror("Killer pthread_kill error") ;
172         return false ;
173       }
174       else {
175         MESSAGE("Killer : ThreadId " << ThreadId << " pthread_killed("
176                 << signum << ")") ;
177       }
178     }
179   }
180   return true ;
181 }
182
183 bool Engines_Component_i::Kill_impl() {
184   MESSAGE("Engines_Component_i::Kill_i() pthread_t "<< pthread_self()
185           << " pid " << getpid() << " instanceName "
186           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
187           << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
188           << dec << " _ThreadId " << _ThreadId << " this " << hex << this
189           << dec ) ;
190   bool RetVal = false ;
191   if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
192     RetVal = Killer( _ThreadId , 0 ) ;
193     _ThreadId = (pthread_t ) -1 ;
194   }
195   return RetVal ;
196 }
197
198 bool Engines_Component_i::Stop_impl() {
199   MESSAGE("Engines_Component_i::Stop_i() pthread_t "<< pthread_self()
200           << " pid " << getpid() << " instanceName "
201           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
202           << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
203           << dec << " _ThreadId " << _ThreadId );
204   bool RetVal = false ;
205   if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
206     RetVal = Killer( _ThreadId , 0 ) ;
207     _ThreadId = (pthread_t ) -1 ;
208   }
209   return RetVal ;
210 }
211
212 bool Engines_Component_i::Suspend_impl() {
213   MESSAGE("Engines_Component_i::Suspend_i() pthread_t "<< pthread_self()
214           << " pid " << getpid() << " instanceName "
215           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
216           << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
217           << dec << " _ThreadId " << _ThreadId );
218   bool RetVal = false ;
219   if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
220     if ( _Sleeping ) {
221       return false ;
222     }
223     else {
224       RetVal = Killer( _ThreadId ,SIGINT ) ;
225     }
226   }
227   return RetVal ;
228 }
229
230 bool Engines_Component_i::Resume_impl() {
231   MESSAGE("Engines_Component_i::Resume_i() pthread_t "<< pthread_self()
232           << " pid " << getpid() << " instanceName "
233           << _instanceName.c_str() << " interface " << _interfaceName.c_str()
234           << " machineName " << GetHostname().c_str()<< " _id " << hex << _id
235           << dec << " _ThreadId " << _ThreadId );
236   bool RetVal = false ;
237   if ( _ThreadId > 0 && pthread_self() != _ThreadId ) {
238     if ( _Sleeping ) {
239       _Sleeping = false ;
240       RetVal = true ;
241     }
242     else {
243       RetVal = false ;
244     }
245   }
246   return RetVal ;
247
248 }
249
250 // Send message to event channel
251
252 void Engines_Component_i::sendMessage(const char *event_type, const char *message) {
253     _notifSupplier->Send(graphName(), nodeName(), event_type, message);
254 }