+ else
+ {
+ // cout<< pthread_self()<<"Engines_Component_i::CpuUsed_impl _ThreadId "
+ // <<_ThreadId <<" "<<_serviceName<<" _StartUsed "<<_StartUsed<<endl;
+ }
+ return cpu ;
+}
+
+
+//=============================================================================
+/*!
+ * C++ method: return Container Servant
+ */
+//=============================================================================
+
+Engines_Container_i *Engines_Component_i::GetContainerPtr()
+{
+ PortableServer::ObjectId_var contId=_poa->reference_to_id(_container);
+ return dynamic_cast<Engines_Container_i*>(_poa->id_to_servant(contId)) ;
+}
+
+//=============================================================================
+/*!
+ * C++ method: return CORBA instance id, the id is set in derived class
+ * constructor, when instance is activated.
+ */
+//=============================================================================
+
+PortableServer::ObjectId * Engines_Component_i::getId()
+{
+// MESSAGE("PortableServer::ObjectId * Engines_Component_i::getId()");
+ return _id ;
+}
+
+//=============================================================================
+/*!
+ * C++ method: used by derived classes for supervision
+ */
+//=============================================================================
+
+void Engines_Component_i::beginService(const char *serviceName)
+{
+ std::cerr << "beginService for " << serviceName << " Component instance : " << _instanceName << std::endl;
+
+#ifndef WIN32
+ _ThreadId = pthread_self() ;
+#else
+ _ThreadId = new pthread_t;
+ _ThreadId->p = pthread_self().p ;
+ _ThreadId->x = pthread_self().x ;
+#endif
+ _StartUsed = 0 ;
+ _StartUsed = CpuUsed_impl() ;
+ _ThreadCpuUsed = 0 ;
+ _Executed = true ;
+ _serviceName = serviceName ;
+ theEngines_Component = this ;
+ if ( pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS , NULL ) )
+ {
+ perror("pthread_setcanceltype ") ;
+ exit(0) ;
+ }
+ if ( pthread_setcancelstate( PTHREAD_CANCEL_ENABLE , NULL ) )
+ {
+ perror("pthread_setcancelstate ") ;
+ exit(0) ;
+ }
+
+ // --- all strings given with setProperties are set in environment
+ std::map<std::string,CORBA::Any>::iterator it;
+ for (it = _fieldsDict.begin(); it != _fieldsDict.end(); it++)
+ {
+ std::string cle((*it).first);
+ if ((*it).second.type()->kind() == CORBA::tk_string)
+ {
+ const char* value;
+ (*it).second >>= value;
+ std::string s(cle);
+ s+='=';
+ s+=value;
+ putenv((char *)s.c_str());
+ MESSAGE("--- setenv: "<<cle<<" = "<< value);
+ }
+ }
+}
+
+//=============================================================================
+/*!
+ * C++ method: used by derived classes for supervision
+ */
+//=============================================================================
+
+void Engines_Component_i::endService(const char *serviceName)
+{
+ if ( !_CanceledThread )
+ _ThreadCpuUsed = CpuUsed_impl() ;
+
+ float cpus=_ThreadCpuUsed/1000.;
+ std::cerr << "endService for " << serviceName << " Component instance : " << _instanceName ;
+ std::cerr << " Cpu Used: " << cpus << " (s) " << std::endl;
+ MESSAGE("Send EndService notification for " << serviceName
+ << std::endl << " Component instance : " << _instanceName << " StartUsed "
+ << _StartUsed << " _ThreadCpuUsed "<< _ThreadCpuUsed << std::endl <<std::endl);
+ _ThreadId = 0 ;
+}
+
+//=============================================================================
+/*!
+ * C++ method: -- CHECK IF USED --
+ */
+//=============================================================================
+
+char* Engines_Component_i::graphName()
+{
+ return CORBA::string_dup( _graphName.c_str() ) ;
+}
+
+//=============================================================================
+/*!
+ * C++ method: -- CHECK IF USED --
+ */
+//=============================================================================
+
+char* Engines_Component_i::nodeName()
+{
+ return CORBA::string_dup( _nodeName.c_str() ) ;
+}
+
+//=============================================================================
+/*!
+ * C++ method: used in Supervision (see kill_impl)
+ */
+//=============================================================================
+
+bool Engines_Component_i::Killer( pthread_t ThreadId , int signum )
+{
+#ifndef WIN32
+ if ( ThreadId )
+#else
+ if ( ThreadId.p )
+#endif
+ {
+ if ( signum == 0 )
+ {
+ if ( pthread_cancel( ThreadId ) )
+ {
+ perror("Killer pthread_cancel error") ;
+ return false ;
+ }
+ else
+ {
+#ifdef WIN32
+ MESSAGE("Killer : ThreadId " << ThreadId.p << " pthread_canceled") ;
+#else
+ MESSAGE("Killer : ThreadId " << ThreadId << " pthread_canceled") ;
+#endif
+ }
+ }
+ else
+ {
+ if ( pthread_kill( ThreadId , signum ) == -1 )
+ {
+ perror("Killer pthread_kill error") ;
+ return false ;
+ }
+ else
+ {
+#ifdef WIN32
+ MESSAGE("Killer : ThreadId " << ThreadId.p << " pthread_killed(" << signum << ")") ;
+#else
+ MESSAGE("Killer : ThreadId " << ThreadId << " pthread_killed(" << signum << ")") ;
+#endif
+ }
+ }
+ }
+ return true ;
+}
+
+void SetCpuUsed();
+void CallCancelThread();
+
+//=============================================================================
+/*!
+ * C++ method:
+ */
+//=============================================================================
+
+void SetCpuUsed()
+{
+ if ( theEngines_Component )
+ theEngines_Component->SetCurCpu() ;
+}
+
+//=============================================================================
+/*!
+ * C++ method:
+ */
+//=============================================================================
+
+void Engines_Component_i::SetCurCpu()
+{
+ _ThreadCpuUsed = CpuUsed() ;
+ // MESSAGE(pthread_self() <<
+ // " Engines_Component_i::SetCurCpu() _ThreadCpuUsed " << _ThreadCpuUsed) ;
+}
+
+//=============================================================================
+/*!
+ * C++ method:
+ */
+//=============================================================================
+
+long Engines_Component_i::CpuUsed()
+{
+ long cpu = 0 ;
+#ifndef WIN32
+ struct rusage usage ;
+ if ( _ThreadId || _Executed )
+ {
+ if ( getrusage( RUSAGE_SELF , &usage ) == -1 )
+ {
+ perror("Engines_Component_i::CpuUsed") ;
+ return 0 ;
+ }
+ //cpu time is calculated in millisecond (user+system times)
+ cpu = usage.ru_utime.tv_sec*1000 +usage.ru_utime.tv_usec/1000;
+ cpu = cpu+ usage.ru_stime.tv_sec*1000 +usage.ru_stime.tv_usec/1000;
+ cpu=cpu-_StartUsed ;
+ // std::cout << pthread_self() << " Engines_Component_i::CpuUsed " << " "
+ // << _serviceName << usage.ru_utime.tv_sec << " - " << _StartUsed
+ // << " = " << cpu << std::endl ;
+ }
+ else
+ {
+ // std::cout << pthread_self() << "Engines_Component_i::CpuUsed _ThreadId "
+ // << _ThreadId << " " << _serviceName<< " _StartUsed "
+ // << _StartUsed << std::endl ;
+ }
+#else
+ // NOT implementet yet
+#endif
+
+
+ return cpu ;
+}
+
+void CallCancelThread()
+{
+ if ( theEngines_Component )
+ theEngines_Component->CancelThread() ;
+}
+
+//=============================================================================
+/*!
+ * C++ method:
+ */
+//=============================================================================
+
+void Engines_Component_i::CancelThread()
+{
+ _CanceledThread = true;
+}
+
+//=============================================================================
+/*!
+ * C++ method: Send message to event channel
+ */
+//=============================================================================
+
+void Engines_Component_i::sendMessage(const char *event_type,
+ const char *message)
+{
+ _notifSupplier->Send(_graphName.c_str(), _nodeName.c_str(), event_type, message);
+}
+
+//=============================================================================
+/*!
+ * C++ method: return standard library name built on component name
+ */
+//=============================================================================
+
+std::string Engines_Component_i::GetDynLibraryName(const char *componentName)
+{
+ std::string prefix, suffix;
+ std::string cname = componentName;
+#if !defined(WIN32)
+ prefix = "lib";
+#endif
+#if defined(WIN32)
+ suffix = "dll";
+#elif defined(__APPLE__)
+ suffix = "dylib";
+#else
+ suffix = "so";
+#endif
+ std::string ret = prefix + cname + std::string("Engine.") + suffix;
+ return ret;
+}
+
+//=============================================================================
+/*!
+ * C++ method: DumpPython default implementation
+ */
+//=============================================================================
+
+Engines::TMPFile* Engines_Component_i::DumpPython(CORBA::Boolean isPublished,
+ CORBA::Boolean isMultiFile,
+ CORBA::Boolean& isValidScript)
+{
+ const char* aScript = isMultiFile ? "def RebuildData(): pass" : "";
+ char* aBuffer = new char[strlen(aScript)+1];
+ strcpy(aBuffer, aScript);
+ CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
+ int aBufferSize = strlen(aBuffer)+1;
+ Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aBufferSize, aBufferSize, anOctetBuf, 1);
+ isValidScript = true;
+ return aStreamFile._retn();
+}
+
+Engines::Salome_file_ptr
+Engines_Component_i::getInputFileToService(const char* service_name,
+ const char* Salome_file_name)
+{
+ // Try to find the service, if it doesn't exist, we throw an exception.
+ _Service_file_map_it = _Input_Service_file_map.find(service_name);
+ if (_Service_file_map_it == _Input_Service_file_map.end()) {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "service doesn't have salome files";
+ throw SALOME::SALOME_Exception(es);
+ }
+ _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
+
+ // Try to find the Salome_file ...
+ _Salome_file_map_it = _map->find(Salome_file_name);
+ if (_Salome_file_map_it == _map->end()) {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "service doesn't have this Salome_file";
+ throw SALOME::SALOME_Exception(es);
+ }
+ Salome_file_i * Sfile = (*_map)[Salome_file_name];
+
+ return Sfile->_this();
+}
+
+Engines::Salome_file_ptr
+Engines_Component_i::setInputFileToService(const char* service_name,
+ const char* Salome_file_name)
+{
+ // Try to find the service, if it doesn't exist, we add it.
+ _Service_file_map_it = _Input_Service_file_map.find(service_name);
+ if (_Service_file_map_it == _Input_Service_file_map.end()) {
+ _t_Salome_file_map * _map = new _t_Salome_file_map();
+ _Input_Service_file_map[service_name] = _map;
+ }
+ _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
+
+ // Try to find the Salome_file ...
+ _Salome_file_map_it = _map->find(Salome_file_name);
+ if (_Salome_file_map_it == _map->end()) {
+ Salome_file_i * Sfile = new Salome_file_i();
+ Engines::Container_ptr container = this->GetContainerRef();
+ Sfile->setContainer(Engines::Container::_duplicate(container));
+ (*_map)[Salome_file_name] = Sfile;
+ }
+
+ Salome_file_i * Sfile = (*_map)[Salome_file_name];
+ return Sfile->_this();
+}
+
+void
+Engines_Component_i::checkInputFilesToService(const char* service_name)
+{
+ // Try to find the service, if it doesn't exist, nothing to do.
+ _Service_file_map_it = _Input_Service_file_map.find(service_name);
+ if (_Service_file_map_it != _Input_Service_file_map.end()) {
+ _t_Salome_file_map * _map = _Input_Service_file_map[service_name];
+ _t_Salome_file_map::iterator begin = _map->begin();
+ _t_Salome_file_map::iterator end = _map->end();
+
+ for(;begin!=end;begin++) {
+ Salome_file_i * file = begin->second;
+ std::string file_port_name = begin->first;
+ configureSalome_file(service_name, file_port_name, file);
+ file->recvFiles();
+ }
+ }
+}
+
+Engines::Salome_file_ptr
+Engines_Component_i::getOutputFileToService(const char* service_name,
+ const char* Salome_file_name)
+{
+ // Try to find the service, if it doesn't exist, we throw an exception.
+ _Service_file_map_it = _Output_Service_file_map.find(service_name);
+ if (_Service_file_map_it == _Output_Service_file_map.end()) {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "service doesn't have salome files";
+ throw SALOME::SALOME_Exception(es);
+ }
+ _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
+
+ // Try to find the Salome_file ...
+ _Salome_file_map_it = _map->find(Salome_file_name);
+ if (_Salome_file_map_it == _map->end()) {
+ SALOME::ExceptionStruct es;
+ es.type = SALOME::INTERNAL_ERROR;
+ es.text = "service doesn't have this Salome_file";
+ throw SALOME::SALOME_Exception(es);
+ }
+ Salome_file_i * Sfile = (*_map)[Salome_file_name];
+
+ return Sfile->_this();
+}
+
+Engines::Salome_file_ptr
+Engines_Component_i::setOutputFileToService(const char* service_name,
+ const char* Salome_file_name)
+{
+ // Try to find the service, if it doesn't exist, we add it.
+ _Service_file_map_it = _Output_Service_file_map.find(service_name);
+ if (_Service_file_map_it == _Output_Service_file_map.end()) {
+ _t_Salome_file_map * _map = new _t_Salome_file_map();
+ _Output_Service_file_map[service_name] = _map;
+ }
+ _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
+
+ // Try to find the Salome_file ...
+ _Salome_file_map_it = _map->find(Salome_file_name);
+ if (_Salome_file_map_it == _map->end()) {
+ Salome_file_i * Sfile = new Salome_file_i();
+ Engines::Container_ptr container = this->GetContainerRef();
+ Sfile->setContainer(Engines::Container::_duplicate(container));
+ (*_map)[Salome_file_name] = Sfile;
+ }
+
+ Salome_file_i * Sfile = (*_map)[Salome_file_name];
+ return Sfile->_this();
+}
+
+void
+Engines_Component_i::checkOutputFilesToService(const char* service_name)
+{
+ // Try to find the service, if it doesn't exist, nothing to do.
+ _Service_file_map_it = _Output_Service_file_map.find(service_name);
+ if (_Service_file_map_it != _Output_Service_file_map.end()) {
+ _t_Salome_file_map * _map = _Output_Service_file_map[service_name];
+ _t_Salome_file_map::iterator begin = _map->begin();
+ _t_Salome_file_map::iterator end = _map->end();
+
+ for(;begin!=end;begin++) {
+ Salome_file_i * file = begin->second;
+ std::string file_port_name = begin->first;
+ configureSalome_file(service_name, file_port_name, file);
+ file->recvFiles();