00001 #ifndef maciSimpleClient_H
00002 #define maciSimpleClient_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __cplusplus
00018 #error This is a C++ include file and cannot be used from plain C
00019 #endif
00020
00021 #include <acsutil.h>
00022 #include <maciExport.h>
00023
00024 #include <logging.h>
00025
00026 #include <maciS.h>
00027 #include <ace/SString.h>
00028
00029 #include "maciSimpleClientThreadHook.h"
00030 #include <ACSErrTypeCommon.h>
00031 #include <maciErrType.h>
00032 #include <ACSErrTypeCORBA.h>
00033
00034 #include <acsComponentSmartPtr.h>
00035
00036 namespace maci {
00037
00038 class SimpleClient;
00039
00040
00041 template<typename T, class H = SimpleClient>
00042 class ComponentSmartPtr : public SmartPtr<T, H, Loki::RefCountedMTAdj<Loki::ObjectLevelLockable>::RefCountedMT,
00043 Loki::DisallowConversion, Loki::NoCheck, ComponentStorage, Loki::LOKI_DEFAULT_CONSTNESS> {
00044 public:
00045
00049 ComponentSmartPtr()
00050 {}
00051
00061 ComponentSmartPtr(H* h, bool s, T* p)
00062 {
00063 setValues(h, s, p);
00064 }
00065
00066
00067 };
00068
00069
00077 class maci_EXPORT SimpleClient :
00078 public virtual POA_maci::Client,
00079 public virtual PortableServer::RefCountServantBase
00080 {
00081
00082 public:
00083
00087 SimpleClient ();
00088
00092 virtual ~SimpleClient ();
00093
00098 int destroy();
00099
00104 int initCORBA(int argc, char * argv[]);
00105
00109 CORBA::ORB_ptr getORB();
00110
00115 int doneCORBA();
00116
00122 int login();
00123
00128 int logout();
00129
00141 int init(int argc, char *argv[]);
00142
00148 int run (ACE_Time_Value &tv
00149 );
00150
00155 int run ();
00156
00157 static void initThread(const char * threadName);
00158 static void doneThread();
00159
00164 maci::Manager_ptr manager();
00165
00170 maci::Handle handle();
00171
00183 CORBA::Object_ptr getComponent(const char *name, const char *domain, bool activate);
00184
00190 CORBA::Object_ptr get_object(const char *name, const char *domain, bool activate)
00191 {
00192 return getComponent(name, domain, activate);
00193 }
00194
00195
00212 template<class T>
00213 T* getComponent(const char *name, const char *domain, bool activate);
00214
00222 CORBA::Object* getDynamicComponent(maci::ComponentSpec compSpec, bool markAsDefault);
00223
00231 template<class T>
00232 T* getDynamicComponent(maci::ComponentSpec compSpec, bool markAsDefault);
00249 template<class T>
00250 ComponentSmartPtr<T> getComponentSmartPtr(const char *name, const char *domain, bool activate);
00251
00259 template<class T>
00260 ComponentSmartPtr<T> getDynamicComponentSmartPtr(maci::ComponentSpec compSpec, bool markAsDefault);
00266 template<class T>
00267 T* get_object(const char *name, const char *domain, bool activate)
00268 {
00269 return getComponent<T>(name, domain, activate);
00270 }
00271
00279 CORBA::Object* getComponentNonSticky(const char *name);
00280
00289 template <class T>
00290 T* getComponentNonSticky(const char *name);
00291
00300 template <class T>
00301 ComponentSmartPtr<T> getComponentNonStickySmartPtr(const char *name);
00302
00310 long releaseComponent(const char* name);
00311
00312
00313
00314
00315
00319 virtual char * name ();
00320
00325 virtual void disconnect ();
00326
00336 virtual ::maci::AuthenticationData * authenticate (
00337 ::maci::ExecutionId execution_id, const char * question);
00338
00344 virtual void message (CORBA::Short type,
00345 const char * message
00346 );
00347
00354 virtual void taggedmessage (CORBA::Short type,
00355 CORBA::Short tag,
00356 const char * message
00357 );
00358
00363 virtual void components_available (const maci::ComponentInfoSeq & cobs
00364 );
00365
00370 virtual void components_unavailable (const maci::stringSeq & cob_names
00371 );
00372
00373
00382 virtual CORBA::Boolean ping ();
00383
00386 static LoggingProxy * getLoggerProxy()
00387 {
00388 return m_logger;
00389 }
00390
00393 static const char * getProcessName()
00394 {
00395 return m_processName.c_str();
00396 }
00397
00398 private:
00399
00401 maci::Manager_var m_manager;
00402
00404 maci::Handle m_handle;
00405
00407 bool m_initialized;
00408
00410 bool m_loggedin;
00411
00413 PortableServer::POA_var m_poaRoot;
00414
00416 PortableServer::POA_var m_poaPersistent;
00417
00419 PortableServer::POA_var m_poaTransient;
00420
00422 CORBA::ORB_var m_orb;
00423
00425 static LoggingProxy* m_logger;
00426
00428 static ACE_CString m_processName;
00429
00431 maci::SimpleClientThreadHook m_simpleClientThreadHook;
00432
00434 maci::ExecutionId m_executionId;
00435
00437 ACS::Time m_startTime;
00438 };
00439
00440
00441
00442
00443
00444
00445
00446
00447 template<class T>
00448 T* SimpleClient::getComponent(const char *name, const char *domain, bool activate)
00449 {
00450 if(!m_initialized)
00451 {
00452 ACSErrTypeCommon::NotInitializedExImpl notInitEx( __FILE__, __LINE__,
00453 "maci::SimpleClient::getComponent<>");
00454 notInitEx.setName("SimpleClient");
00455 maciErrType::CannotGetComponentExImpl ex( notInitEx, __FILE__, __LINE__,
00456 "maci::SimpleClient::getComponent<>");
00457 name ? ex.setCURL(name) : ex.setCURL("NULL");
00458 throw ex;
00459 }
00460
00461 if(!name)
00462 {
00463 ACSErrTypeCommon::NullPointerExImpl nullEx(__FILE__, __LINE__,
00464 "maci::SimpleClient::getComponent<>");
00465 nullEx.setVariable("(parameter) name");
00466 maciErrType::CannotGetComponentExImpl ex(nullEx, __FILE__, __LINE__,
00467 "maci::SimpleClient::getComponent<>");
00468 ex.setCURL("NULL");
00469 throw ex;
00470 }
00471
00476 char *curl_str = "curl://";
00477
00478 ACE_CString curl = "";
00479 if(strncmp(name, curl_str, strlen(curl_str)) != 0 )
00480 {
00481 curl += curl_str;
00482 if (domain)
00483 curl += domain;
00484
00485 curl += ACE_CString("/");
00486 }
00487 curl += name;
00488
00489 ACS_SHORT_LOG((LM_DEBUG, "Getting component: '%s'. Creating it...", curl.c_str()));
00490
00491 try
00492 {
00494 CORBA::Object_var obj = manager()->get_service(m_handle, curl.c_str(), activate);
00495 T* tmpRef = T::_narrow(obj);
00496 if (CORBA::is_nil(tmpRef))
00497 {
00498 releaseComponent(name);
00499 ACSErrTypeCORBA::NarrowFailedExImpl ex(__FILE__, __LINE__, "maci::SimpleClient<>::getComponent");
00500 ex.setNarrowType(typeid(T).name());
00501 throw ex;
00502 }
00503 return tmpRef;
00504 }
00505 catch(maciErrType::NoPermissionEx &_ex)
00506 {
00507 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00508 "maci::SimpleClient::getComponent<>");
00509 ex.setCURL(name);
00510 throw ex;
00511 }
00512 catch(maciErrType::CannotGetComponentEx &_ex)
00513 {
00514 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00515 "maci::SimpleClient::getComponent<>");
00516 ex.setCURL(name);
00517 throw ex;
00518 }
00519 catch(maciErrType::ComponentNotAlreadyActivatedEx &_ex)
00520 {
00521 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00522 "maci::SimpleClient::getComponent<>");
00523 ex.setCURL(name);
00524 throw ex;
00525 }
00526 catch(maciErrType::ComponentConfigurationNotFoundEx &_ex)
00527 {
00528 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00529 "maci::SimpleClient::getComponent<>");
00530 ex.setCURL(name);
00531 throw ex;
00532 }
00533 catch (ACSErr::ACSbaseExImpl &ex)
00534 {
00535 maciErrType::CannotGetComponentExImpl lex(ex, __FILE__, __LINE__,
00536 "maci::SimpleClient<T>::getComponent");
00537 lex.setCURL(name);
00538 throw lex;
00539 }
00540 catch( CORBA::SystemException &_ex )
00541 {
00542 ACSErrTypeCommon::CORBAProblemExImpl corbaProblemEx(__FILE__, __LINE__,
00543 "maci::SimpleClient::getComponent<>");
00544 corbaProblemEx.setMinor(_ex.minor());
00545 corbaProblemEx.setCompletionStatus(_ex.completed());
00546 corbaProblemEx.setInfo(_ex._info().c_str());
00547 maciErrType::CannotGetComponentExImpl ex(corbaProblemEx, __FILE__, __LINE__,
00548 "maci::SimpleClient::getComponent<>");
00549 ex.setCURL(name);
00550 throw ex;
00551 }
00552 catch(...)
00553 {
00554 ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__,
00555 "maci::SimpleClient::getComponent<>");
00556 maciErrType::CannotGetComponentExImpl ex(uex, __FILE__, __LINE__,
00557 "maci::SimpleClient::getComponent<>");
00558 ex.setCURL(name);
00559 throw ex;
00560 }
00561 }
00562
00563
00564
00565
00566 template<class T>
00567 ComponentSmartPtr<T> SimpleClient::getComponentSmartPtr(const char *name, const char *domain, bool activate)
00568 {
00569 return ComponentSmartPtr<T>(this, true, this->getComponent<T>(name, domain, activate));
00570 }
00571
00572
00573
00574 template<class T>
00575 ComponentSmartPtr<T> SimpleClient::getDynamicComponentSmartPtr(maci::ComponentSpec compSpec, bool markAsDefault)
00576 {
00577 return ComponentSmartPtr<T>(this, true, this->getDynamicComponent<T>(compSpec,markAsDefault));
00578 }
00579
00580
00581
00582
00583
00584
00585 template<class T>
00586 T* SimpleClient::getDynamicComponent(maci::ComponentSpec compSpec, bool markAsDefault)
00587 {
00588
00589
00590
00591
00592
00593
00594 ComponentInfo_var cInfo;
00595 ACS_TRACE("maci::SimpleClient<>::getDynamicComponent");
00596 try
00597 {
00598 cInfo = m_manager->get_dynamic_component(m_handle,
00599 compSpec,
00600 markAsDefault);
00601 CORBA::Object_var obj = cInfo->reference;
00602 if (CORBA::is_nil(obj.in()))
00603 {
00604 ACSErrTypeCORBA::CORBAReferenceNilExImpl ex(__FILE__, __LINE__,
00605 "maci::SimpleClient<>::getDynamicComponent");
00606 ex.setVariable("cInfo->reference");
00607 throw ex;
00608 }
00609
00610
00611 T* tmpRef = T::_narrow(obj);
00612 if(CORBA::is_nil(tmpRef))
00613 { ACSErrTypeCORBA::NarrowFailedExImpl ex(__FILE__, __LINE__, "maci::SimpleClient<>::getDynamicComponent");
00614
00615 throw ex;
00616 }
00617 return tmpRef;
00618
00619 }
00620 catch (maciErrType::NoPermissionEx &ex)
00621 {
00622 maciErrType::NoPermissionExImpl lex(ex, __FILE__, __LINE__,
00623 "maci::SimpleClient<>::getDynamicComponent");
00624 throw lex;
00625 }
00626 catch (maciErrType::IncompleteComponentSpecEx &ex)
00627 {
00628 maciErrType::IncompleteComponentSpecExImpl lex(ex, __FILE__, __LINE__,
00629 "maci::SimpleClient<>::getDynamicComponent");
00630 lex.setCURL(compSpec.component_name.in());
00631 throw lex;
00632 }
00633 catch (maciErrType::InvalidComponentSpecEx &ex)
00634 {
00635 maciErrType::InvalidComponentSpecExImpl lex(ex, __FILE__, __LINE__,
00636 "maci::SimpleClient<>::getDynamicComponent");
00637 throw lex;
00638 }
00639 catch (maciErrType::ComponentSpecIncompatibleWithActiveComponentEx &ex)
00640 {
00641 maciErrType::ComponentSpecIncompatibleWithActiveComponentExImpl lex(ex, __FILE__, __LINE__,
00642 "maci::SimpleClient<>::getDynamicComponent");
00643 lex.setCURL(compSpec.component_name.in());
00644 throw lex;
00645 }
00646 catch (maciErrType::CannotGetComponentEx &ex)
00647 {
00648 maciErrType::CannotGetComponentExImpl lex(ex, __FILE__, __LINE__,
00649 "maci::SimpleClient<>::getDynamicComponent");
00650 lex.setCURL(compSpec.component_name.in());
00651 throw lex;
00652 }
00653 catch(ACSErr::ACSbaseExImpl &ex)
00654 {
00655 maciErrType::CannotGetComponentExImpl lex(ex, __FILE__, __LINE__,
00656 "maci::SimpleClient<>::getDynamicComponent");
00657 lex.setCURL(compSpec.component_name.in());
00658 throw lex;
00659 }
00660 catch( CORBA::SystemException &ex )
00661 {
00662 ACSErrTypeCommon::CORBAProblemExImpl corbaProblemEx(__FILE__, __LINE__,
00663 "maci::SimpleClient<>::getDynamicComponent");
00664 corbaProblemEx.setMinor(ex.minor());
00665 corbaProblemEx.setCompletionStatus(ex.completed());
00666 corbaProblemEx.setInfo(ex._info().c_str());
00667
00668 maciErrType::CannotGetComponentExImpl lex(corbaProblemEx, __FILE__, __LINE__,
00669 "maci::SimpleClient<>::getDynamicComponent");
00670 lex.setCURL(compSpec.component_name.in());
00671 throw lex;
00672 }
00673 catch (...)
00674 {
00675 ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__,
00676 "maci::SimpleClient<>::getDynamicComponent");
00677 maciErrType::CannotGetComponentExImpl lex(uex, __FILE__, __LINE__,
00678 "maci::SimpleClient<>::getDynamicComponent");
00679 lex.setCURL(compSpec.component_name.in());
00680 throw lex;
00681 }
00682 }
00683
00684
00685
00686
00687
00688 template<class T>
00689 T* SimpleClient::getComponentNonSticky(const char *name)
00690 {
00691 if(!m_initialized)
00692 {
00693 ACSErrTypeCommon::NotInitializedExImpl notInitEx( __FILE__, __LINE__,
00694 "maci::SimpleClient::getComponentNonSticky<>");
00695 notInitEx.setName("SimpleClient");
00696 maciErrType::CannotGetComponentExImpl ex( notInitEx, __FILE__, __LINE__,
00697 "maci::SimpleClient::getComponentNonSticky<>");
00698 name ? ex.setCURL(name) : ex.setCURL("NULL");
00699 throw ex;
00700 }
00701
00702 if(!name)
00703 {
00704 ACSErrTypeCommon::NullPointerExImpl nullEx(__FILE__, __LINE__,
00705 "maci::SimpleClient::getComponentNonSticky<>");
00706 nullEx.setVariable("(parameter) name");
00707 maciErrType::CannotGetComponentExImpl ex(nullEx, __FILE__, __LINE__,
00708 "maci::SimpleClient::getComponentNonSticky<>");
00709 ex.setCURL("NULL");
00710 throw ex;
00711 }
00712
00713 ACS_SHORT_LOG((LM_DEBUG, "Getting component non sticky: '%s'. Creating it...", name));
00714
00715 try
00716 {
00717 CORBA::Object_var obj = manager()->get_component_non_sticky(m_handle, name);
00718 T* tmpRef = T::_narrow(obj);
00719 if (CORBA::is_nil(tmpRef))
00720 {
00721
00722 ACSErrTypeCORBA::NarrowFailedExImpl ex(__FILE__, __LINE__, "maci::SimpleClient<>::getComponentNonSticky");
00723 ex.setNarrowType(typeid(T).name());
00724 throw ex;
00725 }
00726 return tmpRef;
00727 }
00728 catch(maciErrType::NoPermissionEx &_ex)
00729 {
00730 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00731 "maci::SimpleClient::getComponentNonSticky<>");
00732 ex.setCURL(name);
00733 throw ex;
00734 }
00735 catch(maciErrType::CannotGetComponentEx &_ex)
00736 {
00737 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00738 "maci::SimpleClient::getComponentNonSticky<>");
00739 ex.setCURL(name);
00740 throw ex;
00741 }
00742 catch(maciErrType::ComponentNotAlreadyActivatedEx &_ex)
00743 {
00744 maciErrType::CannotGetComponentExImpl ex(_ex, __FILE__, __LINE__,
00745 "maci::SimpleClient::getComponentNonSticky<>");
00746 ex.setCURL(name);
00747 throw ex;
00748 }
00749 catch (ACSErr::ACSbaseExImpl &ex)
00750 {
00751 maciErrType::CannotGetComponentExImpl lex(ex, __FILE__, __LINE__,
00752 "maci::SimpleClient<>::getComponentNonSticky");
00753 lex.setCURL(name);
00754 throw lex;
00755 }
00756 catch( CORBA::SystemException &_ex )
00757 {
00758 ACSErrTypeCommon::CORBAProblemExImpl corbaProblemEx(__FILE__, __LINE__,
00759 "maci::SimpleClient::getComponentNonSticky<>");
00760 corbaProblemEx.setMinor(_ex.minor());
00761 corbaProblemEx.setCompletionStatus(_ex.completed());
00762 corbaProblemEx.setInfo(_ex._info().c_str());
00763 maciErrType::CannotGetComponentExImpl ex(corbaProblemEx, __FILE__, __LINE__,
00764 "maci::SimpleClient::getComponent<>");
00765 ex.setCURL(name);
00766 throw ex;
00767 }
00768 catch(...)
00769 {
00770 ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__,
00771 "maci::SimpleClient::getComponentNonSticky<>");
00772 maciErrType::CannotGetComponentExImpl ex(uex, __FILE__, __LINE__,
00773 "maci::SimpleClient::getComponentNonSticky<>");
00774 ex.setCURL(name);
00775 throw ex;
00776 }
00777 }
00778
00779
00780
00781
00782 template<class T>
00783 ComponentSmartPtr<T> SimpleClient::getComponentNonStickySmartPtr(const char *name)
00784 {
00785 return ComponentSmartPtr<T>(this, false, this->getComponentNonSticky<T>(name));
00786 }
00787
00788 };
00789
00790 #endif