void PlugIn::load_library( const std::string &library_file_name ) { try { this->release_library(); m_libraryHandle = this->do_load_library( library_file_name ); if ( m_libraryHandle == NULL ) { THROW_YAT_ERROR("SHAREDLIBRARY_ERROR", "Unable to load the specified shared library", "PlugIn::load_library"); } Symbol sym = this->find_symbol(kOnLoadSymbol); OnLoadFunc_t load_func = (OnLoadFunc_t)(sym); load_func(); } catch(yat::Exception& ex) { RETHROW_YAT_ERROR(ex, "SHAREDLIBRARY_ERROR", "Error while loading library", "PlugIn::load_library"); } catch (...) { THROW_YAT_ERROR("SHAREDLIBRARY_ERROR", "Unknown error while loading library", "PlugIn::load_library"); } }
IPlugInInfo* PlugIn::info() { Symbol symbol; try { symbol = this->find_symbol(kGetInfoSymbol); } catch(yat::Exception& ex) { RETHROW_YAT_ERROR(ex, "SHAREDLIBRARY_ERROR", "Unable to find the 'GetInfo' symbol", "PlugIn::info"); } GetInfoFunc_t get_info_func = (GetInfoFunc_t)(symbol); IPlugInInfo* info = 0; try { info = (*get_info_func)(); } catch(...) { THROW_YAT_ERROR("UNKNOWN_ERROR", "Unknown error during IPlugInInfo instantiation", "PlugIn::info"); } return info; }
void PlugIn::release_library() { try { if ( m_libraryHandle != NULL ) { Symbol sym = this->find_symbol(kOnUnLoadSymbol); OnUnLoadFunc_t unload_func = (OnUnLoadFunc_t)(sym); unload_func(); this->do_release_library(); m_libraryHandle = NULL; } } catch(yat::Exception& ex) { RETHROW_YAT_ERROR(ex, "SHAREDLIBRARY_ERROR", "Error while releasing library", "PlugIn::release_library"); } catch (...) { THROW_YAT_ERROR("SHAREDLIBRARY_ERROR", "Unknown error while releasing library", "PlugIn::release_library"); } }
IPlugInFactory* PlugIn::factory() { Symbol symbol; try { symbol = this->find_symbol(kGetFactorySymbol); } catch(yat::Exception& ex) { RETHROW_YAT_ERROR(ex, "SHAREDLIBRARY_ERROR", "Unable to find the 'GetFactory' symbol", "PlugIn::factory"); } GetFactoryFunc_t get_factory_func = (GetFactoryFunc_t)(symbol); IPlugInFactory* factory = 0; try { factory = (*get_factory_func)(); } catch(...) { THROW_YAT_ERROR("UNKNOWN_ERROR", "Unknown error during factory instantiation", "PlugIn::factory"); } return factory; }
/*virtual*/ void IBASourceBypass::get_image(ImageAndInfo & imginf) throw (yat::Exception) { if (!this->image_grabber_->is_Image_allowed(Tango::READ_REQ)) THROW_YAT_ERROR("ATTR_NOT_ALLOWED", "Image attribute cannot be read in this state.", "IBASourceBypass::get_image"); GrabAPI::SharedImage* si = 0; // From now on: act as if it was a Tango request: { Tango::AutoTangoMonitor guard(this->image_grabber_); // Ask for the attributes to be updated... std::vector<long> attr_list; /// @todo fill attr_list... this->image_grabber_->read_attr_hardware(attr_list); // Get the last image si = this->image_grabber_->get_last_image(); } const GrabAPI::Image* gimage = si->get_image(); if (gimage == NULL) { SAFE_RELEASE(si); THROW_YAT_ERROR("EMPTY_IMAGE", "The image is empty.", "IBASourceBypass::get_image"); } // So we got the shared image. Now copy it to an image // in the format expected by ImageBeamAnalyzer try { imginf.bit_depth = gimage->bit_depth; imginf.image = this->convert_image_grabber_to_isl(gimage); } catch (yat::Exception &) { SAFE_RELEASE(si); throw; } catch (...) { SAFE_RELEASE(si); THROW_YAT_ERROR( "UNKNOWN_ERROR", "Unknown problem when converting ImgGrabber image to isl::Image", "IBASourceBypass::get_image" ); } SAFE_RELEASE(si); }
/*static*/ IBASource* IBASourceFactory::create(const std::string & name) { if (!IBASourceFactory::s_first_) THROW_YAT_ERROR( "NO_FACTORY", "No IBASource factory has been defined, so it is not possible to create" " IBASource objects -> Can't connect to a source of images.", "IBASourceFactory::create"); return IBASourceFactory::s_first_->create_object(name); }
IBASource* IBASourceFactory::create_next(const std::string & name) { if (!this->next_) THROW_YAT_ERROR( "NO_NEXT_FACTORY", "No IBASource factory has been been able to create" " a IBASource object -> Can't connect to a source of images.", "IBASourceFactory::create_next"); return this->next_->create_object(name); }
//----------------------------------------------------------------------------- // PlugIn::do_load_library //----------------------------------------------------------------------------- PlugIn::LibraryHandle PlugIn::do_load_library( const std::string &library_file_name ) { PlugIn::LibraryHandle handle = ::dlopen( library_file_name.c_str(), RTLD_NOW | RTLD_GLOBAL ); if( NULL == handle ) { THROW_YAT_ERROR( std::string("SHAREDLIBRARY_ERROR"), std::string(::dlerror()), std::string("PlugIn::do_load_library") ); } return handle; }
/*static*/ isl::Image* IBASourceBypass::convert_image_grabber_to_isl(const GrabAPI::Image* gimage) throw (yat::Exception) { try { isl::Image* image = new isl::Image(gimage->width(), gimage->height(), isl::ISL_STORAGE_USHORT); image->unserialize(gimage->base()); return image; } catch(isl::Exception & ex) { GrabAPI::ISL2YATException yat_exc(ex); isl::ErrorHandler::reset(); RETHROW_YAT_ERROR(yat_exc, "SOFTWARE_FAILURE", "Unable to convert from GrabAPI::Image to isl::Image.", "IBASourceBypass::convert_image_grabber_to_isl."); } catch(std::bad_alloc &) { THROW_YAT_ERROR("OUT_OF_MEMORY", "Allocation of isl::Image failed [std::bad_alloc]", "IBASourceBypass::convert_image_grabber_to_isl"); } catch(...) { THROW_YAT_ERROR("UNKNOWN_ERROR", "Unable to convert from GrabAPI::Image to isl::Image. [Unknown exception caught]", "IBASourceBypass::convert_image_grabber_to_isl"); } }
PlugIn::Symbol PlugIn::find_symbol( const std::string &symbol ) { try { Symbol symbol_pointer = this->do_find_symbol( symbol ); if ( symbol_pointer != NULL ) return symbol_pointer; } catch ( ... ) { } THROW_YAT_ERROR("SHAREDLIBRARY_ERROR", "Unable to find the requested symbol", "PlugIn::find_symbol"); return NULL; // keep compiler happy }
// ---------------------------------------------------------------------------- // Socket::init // ---------------------------------------------------------------------------- void Socket::init () throw (SocketException) { if (Socket::init_done) return; #ifdef WIN32 //- windows hell: magic initialization call WSADATA wsdata; if (::WSAStartup(MAKEWORD(2,2), &wsdata)) { THROW_YAT_ERROR("YAT_INTERNAL_ERROR", "win32::socket2 library initialization failed", "Address::ns_lookup"); } #endif Socket::init_done = true; }
// ---------------------------------------------------------------------------- // Address::ns_lookup // ---------------------------------------------------------------------------- void Address::ns_lookup (const std::string& _host_ipaddr_or_name) throw (Exception) { YAT_TRACE("Address::ns_lookup"); //- is _host_ipaddr_or_name an IP address or a host name? unsigned int ip_addr = ::inet_addr(_host_ipaddr_or_name.c_str()); //- in case it's an IP address if (ip_addr != INADDR_NONE) { //- store it into the local member this->m_ip_addr = _host_ipaddr_or_name; //- get associated host name struct hostent * host = ::gethostbyaddr((char*)&ip_addr, sizeof(ip_addr), AF_INET); //- if host as a valid hostname then store it localy if (host) this->m_host_name = host->h_name; else this->m_host_name = _host_ipaddr_or_name; } //- in case it's a host name else { //- store it into the local member this->m_host_name = _host_ipaddr_or_name; //- get associated ip address struct hostent * host = ::gethostbyname(_host_ipaddr_or_name.c_str()); //- got a valid ip addr? if (! host) { yat::OSStream oss; oss << "could not resolve IP address for host " << _host_ipaddr_or_name << std::endl; THROW_YAT_ERROR("INVALID_HOST", oss.str().c_str(), "Address::ns_lookup"); } //- store it into the local member this->m_ip_addr = ::inet_ntoa(*((struct in_addr*)host->h_addr)); } }
// ============================================================================ // Task::go // ============================================================================ void Task::go (Message * _msg, size_t _tmo_ms) throw (Exception) { YAT_TRACE("Task::go"); this->start_undetached(); if ( (_msg == 0) || (_msg->type() != TASK_INIT) || (_msg->waitable() == false) ) THROW_YAT_ERROR("PROGRAMMING_ERROR", "invalid INIT message [null, wrong type or not waitable]", "Task::go"); this->wait_msg_handled (_msg, _tmo_ms); }
// ====================================================================== // Task::wait_msg_handled // ====================================================================== void Task::wait_msg_handled (Message * _msg, size_t _tmo_ms) throw (Exception) { YAT_TRACE("Task::wait_msg_handled"); //- check input if (! _msg || ! _msg->waitable()) { if (_msg) _msg->release(); THROW_YAT_ERROR("INVALID_ARGUMENT", "invalid message [either null or not waitable]", "Task::wait_msg_handled"); } try { //- post a shallow copy of the msg this->msg_q_.post(_msg->duplicate()); } catch (...) { _msg->release(); THROW_YAT_ERROR("INTERNAL_ERROR", "message could not be posted", "Task::wait_msg_handled"); } YAT_LOG("Task::wait_msg_handled::waiting for msg [" << std::hex << (void*)_msg << std::dec << "] to be handled"); //- wait for the msg to be handled or tmo expiration if (_msg->wait_processed(_tmo_ms)) { YAT_LOG("Task::wait_msg_handled::msg [" << std::hex << (void*)_msg << std::dec << "] handled [gave error::" << (_msg->has_error() ? "yes" : "no") << "]" ); Exception msg_exception; bool msg_gave_error = _msg->has_error(); //- to store error localy before releasing msg if (msg_gave_error) msg_exception = _msg->get_error(); //- release msg _msg->release(); //- throw an exception if msg gave error if (msg_gave_error) throw msg_exception; //- msg did not gave error, just return return; } //- too bad, timeout expired... YAT_LOG("Task::wait_msg_handled::timeout expired while waiting for msg [" << std::hex << (void*)_msg << std::dec << "] to be handled"); //- release msg _msg->release(); //- throw timeout exception THROW_YAT_ERROR("TIMEOUT_EXPIRED", "timeout expired while waiting for message to be handled", "Task::wait_msg_handled"); }
// ====================================================================== // Task::exit // ====================================================================== void Task::exit () throw (Exception) { YAT_TRACE("Task::exit"); //- we may have to implicitly delete the thread bool delete_self = false; //- enter critical section this->m_lock.lock(); //- get underlying thread state Thread::State ts = this->state_i(); //- if the thread is running, then ask it to exit if (ts == yat::Thread::STATE_RUNNING) { yat::Message * msg = 0; try { msg = Message::allocate (yat::TASK_EXIT, EXIT_MSG_PRIORITY, true); } catch (Exception &ex) { this->m_lock.unlock(); RETHROW_YAT_ERROR(ex, "SOFTWARE_ERROR", "Could not stop task [yat::Message allocation failed]", "Task::exit"); } catch (...) { this->m_lock.unlock(); THROW_YAT_ERROR("UNKNOWN_ERROR", "Could not stop task [yat::Message allocation failed]", "Task::exit"); } //- unlock the thread lock (avoid deadlock during message handling) this->m_lock.unlock(); try { //- ... then wait for TASK_EXIT msg to be handled //- TODO: change kINFINITE_WAIT to a more flexible TIMEOUT YAT_LOG("Task::exit - waiting for the TASK_EXIT msg to be handled"); this->wait_msg_handled (msg, kINFINITE_WAIT); } catch (...) { //- ignore any error } //- wait for the thread to actually quit try { Thread::IOArg dummy = 0; YAT_LOG("Task::exit - about to join with the underlying thread"); this->join (&dummy); } catch (...) { //- ignore any error } } else if (ts == yat::Thread::STATE_NEW) { //- delete the thread (instanciated but never been started) YAT_LOG("Task::exit - about to delete the thread [has never been started]"); delete_self = true; //- leave critical section this->m_lock.unlock(); } else { //- nothing to do... YAT_LOG("Task::exit - do nothing"); //- leave critical section this->m_lock.unlock(); } //- delete (if required) if (delete_self) { YAT_LOG("Task::exit - deleting <this> Task instance"); delete this; } }