ComponentObserverConsumer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #include <coil/stringutil.h>
00020 #include <rtm/Typename.h>
00021 #include "ComponentObserverSkel.h"
00022 #include "ComponentObserverConsumer.h"
00023 #include <iostream>
00024 
00025 namespace RTC
00026 {
00034   ComponentObserverConsumer::ComponentObserverConsumer()
00035     : m_rtobj(NULL),
00036       m_compstat(*this), m_portaction(*this),
00037       m_ecaction(*this), m_configMsg(*this),
00038       m_interval(0, 100000), m_heartbeat(false),
00039       m_hblistenerid(NULL),
00040       m_timer(m_interval)
00041   {
00042     for (size_t i(0); i < OpenRTM::STATUS_KIND_NUM; ++i)
00043       {
00044         m_observed[i] = false;
00045       }
00046   }
00047 
00055   ComponentObserverConsumer::~ComponentObserverConsumer()
00056   {
00057     unsetComponentProfileListeners();
00058     unsetComponentStatusListeners();
00059     unsetPortProfileListeners();
00060     unsetExecutionContextListeners();
00061     unsetConfigurationListeners();
00062     unsetHeartbeat();
00063   }
00064 
00072   bool
00073   ComponentObserverConsumer::init(RTObject_impl& rtobj,
00074                                   const SDOPackage::ServiceProfile& profile)
00075   {
00076     if (!m_observer.setObject(profile.service))
00077       {
00078         // narrowing failed
00079         return false;
00080       }
00081 
00082     m_rtobj = &rtobj;
00083     m_profile = profile;
00084     coil::Properties prop;
00085     NVUtil::copyToProperties(prop, profile.properties);
00086     setHeartbeat(prop);
00087     setListeners(prop);
00088     return true;
00089   }
00090 
00098   bool
00099   ComponentObserverConsumer::reinit(const SDOPackage::ServiceProfile& profile)
00100   {
00101     if (!m_observer._ptr()->_is_equivalent(profile.service))
00102       {
00103         CorbaConsumer<OpenRTM::ComponentObserver> tmp;
00104         if (!tmp.setObject(profile.service))
00105           {
00106             return false;
00107           }
00108         m_observer.releaseObject();
00109         m_observer.setObject(profile.service);
00110       }
00111     m_profile= profile;
00112     coil::Properties prop;
00113     NVUtil::copyToProperties(prop, profile.properties);
00114     setHeartbeat(prop);
00115     setListeners(prop);
00116     return true;
00117   }
00118 
00126   const SDOPackage::ServiceProfile&
00127   ComponentObserverConsumer::getProfile() const
00128   {
00129     return m_profile;
00130   }  
00131 
00139   void ComponentObserverConsumer::finalize()
00140   {
00141     unsetComponentProfileListeners();
00142     unsetComponentStatusListeners();
00143     unsetPortProfileListeners();
00144     unsetExecutionContextListeners();
00145     unsetConfigurationListeners();
00146     unsetHeartbeat();
00147   }
00148 
00149   //============================================================
00150   // protected functions
00151 
00159   void ComponentObserverConsumer::setListeners(coil::Properties& prop)
00160   {
00161     if (prop["observed_status"].empty())
00162       {
00163         prop["observed_status"] = "ALL";
00164       }
00165 
00166     coil::vstring observed(coil::split(prop["observed_status"], ","));
00167     bool flags[OpenRTM::STATUS_KIND_NUM];
00168     for (int i(0); i < OpenRTM::STATUS_KIND_NUM; ++i)
00169       {
00170         flags[i] = false;
00171       }
00172     for (size_t i(0); i < observed.size(); ++i)
00173       {
00174         coil::toUpper(observed[i]);
00175         if (observed[i] == "COMPONENT_PROFILE")
00176           {
00177             flags[OpenRTM::COMPONENT_PROFILE] = 1;
00178           }
00179         else if (observed[i] == "RTC_STATUS")
00180           {
00181             flags[OpenRTM::RTC_STATUS] = 1;
00182           }
00183         else if (observed[i] == "EC_STATUS")
00184           {
00185             flags[OpenRTM::EC_STATUS] = 1;
00186           }
00187         else if (observed[i] == "PORT_PROFILE")
00188           {
00189             flags[OpenRTM::PORT_PROFILE] = 1;
00190           }
00191         else if (observed[i] == "CONFIGURATION")
00192           {
00193             flags[OpenRTM::CONFIGURATION] = 1;
00194           }
00195         else if (observed[i] == "ALL")
00196           {
00197             for (int j(0); j < OpenRTM::STATUS_KIND_NUM; ++j)
00198               {
00199                 flags[j] = true;
00200               }
00201             break;
00202           }
00203       }
00204   
00205     switchListeners(flags[OpenRTM::COMPONENT_PROFILE],
00206                     m_observed[OpenRTM::COMPONENT_PROFILE],
00207                     &ComponentObserverConsumer::setComponentProfileListeners,
00208                     &ComponentObserverConsumer::unsetComponentProfileListeners);
00209     switchListeners(flags[OpenRTM::RTC_STATUS],
00210                     m_observed[OpenRTM::RTC_STATUS],
00211                     &ComponentObserverConsumer::setComponentStatusListeners,
00212                     &ComponentObserverConsumer::unsetComponentStatusListeners);
00213     switchListeners(flags[OpenRTM::EC_STATUS],
00214                     m_observed[OpenRTM::EC_STATUS],
00215                     &ComponentObserverConsumer::setExecutionContextListeners,
00216                     &ComponentObserverConsumer::unsetExecutionContextListeners);
00217     switchListeners(flags[OpenRTM::PORT_PROFILE],
00218                     m_observed[OpenRTM::PORT_PROFILE],
00219                     &ComponentObserverConsumer::setPortProfileListeners,
00220                     &ComponentObserverConsumer::unsetPortProfileListeners);
00221     switchListeners(flags[OpenRTM::CONFIGURATION],
00222                     m_observed[OpenRTM::CONFIGURATION],
00223                     &ComponentObserverConsumer::setConfigurationListeners,
00224                     &ComponentObserverConsumer::unsetConfigurationListeners);
00225 
00226   }
00227 
00235   void ComponentObserverConsumer::
00236   switchListeners(bool& next, bool& pre,
00237                   void (ComponentObserverConsumer::*setfunc)(), 
00238                   void (ComponentObserverConsumer::*unsetfunc)())
00239   {
00240     if (!pre && next)
00241       {
00242         (this->*setfunc)();
00243         pre = true;
00244       }
00245     else if (pre && !next)
00246       {
00247         (this->*unsetfunc)();
00248         pre = false;
00249       }
00250   }
00251 
00252   //============================================================
00253   // Heartbeat related functions
00254 
00262   void ComponentObserverConsumer::heartbeat()
00263   {
00264     updateStatus(OpenRTM::HEARTBEAT, "");
00265   }
00266 
00274   void ComponentObserverConsumer::setHeartbeat(coil::Properties& prop)
00275   {
00276     if (coil::toBool(prop["heartbeat.enable"], "YES", "NO", false))
00277       {
00278         std::string interval(prop["heartbeat.interval"]);
00279         if (interval.empty())
00280           {
00281             m_interval = 1.0;
00282           }
00283         else
00284           {
00285             double tmp;
00286             coil::stringTo(tmp, interval.c_str());
00287             m_interval = tmp;
00288           }
00289         coil::TimeValue tm;
00290         tm = m_interval;
00291         m_hblistenerid = m_timer.
00292           registerListenerObj(this, &ComponentObserverConsumer::heartbeat, tm);
00293         m_timer.start();
00294       }
00295     else
00296       {
00297         if (m_heartbeat == true && m_hblistenerid != 0)
00298           {
00299             unsetHeartbeat();
00300             m_timer.stop();
00301           }
00302       }
00303   }
00304 
00312   void ComponentObserverConsumer::unsetHeartbeat()
00313   {
00314     m_timer.unregisterListener(m_hblistenerid);
00315     m_heartbeat = false;
00316     m_hblistenerid = 0;
00317     m_timer.stop();
00318   }
00319 
00320 
00321   //============================================================
00322   // Component status
00323 
00331   void ComponentObserverConsumer::setComponentStatusListeners()
00332   {
00333     if (m_compstat.activatedListener == NULL)
00334       {
00335         m_compstat.activatedListener = 
00336           m_rtobj->addPostComponentActionListener(POST_ON_ACTIVATED,
00337                                                   m_compstat,
00338                                                   &CompStatMsg::onActivated);
00339       }
00340     if (m_compstat.deactivatedListener == NULL)
00341       {
00342         m_compstat.deactivatedListener = 
00343           m_rtobj->addPostComponentActionListener(POST_ON_DEACTIVATED,
00344                                                   m_compstat,
00345                                                   &CompStatMsg::onDeactivated);
00346       }
00347     if (m_compstat.resetListener == NULL)
00348       {
00349         m_compstat.resetListener = 
00350           m_rtobj->addPostComponentActionListener(POST_ON_RESET,
00351                                                   m_compstat,
00352                                                   &CompStatMsg::onReset);
00353       }
00354     if (m_compstat.abortingListener == NULL)
00355       {
00356         m_compstat.abortingListener = 
00357           m_rtobj->addPostComponentActionListener(POST_ON_ABORTING,
00358                                                   m_compstat,
00359                                                   &CompStatMsg::onAborting);
00360       }
00361     if (m_compstat.finalizeListener == NULL)
00362       {
00363         m_compstat.finalizeListener = 
00364           m_rtobj->addPostComponentActionListener(POST_ON_FINALIZE,
00365                                                   m_compstat,
00366                                                   &CompStatMsg::onFinalize);
00367       }
00368   }
00369   
00377   void ComponentObserverConsumer::unsetComponentStatusListeners()
00378   {
00379     if (m_compstat.activatedListener != NULL)
00380       {
00381         m_rtobj->removePostComponentActionListener(POST_ON_ACTIVATED,
00382                                                  m_compstat.activatedListener);
00383         m_compstat.activatedListener = NULL;
00384       }
00385     if (m_compstat.deactivatedListener != NULL)
00386       {
00387         m_rtobj->removePostComponentActionListener(POST_ON_DEACTIVATED,
00388                                                m_compstat.deactivatedListener);
00389         m_compstat.deactivatedListener = NULL;
00390       }
00391     if (m_compstat.resetListener != NULL)
00392       {
00393         m_rtobj->removePostComponentActionListener(POST_ON_RESET,
00394                                                    m_compstat.resetListener);
00395         m_compstat.resetListener = NULL;
00396       }
00397     if (m_compstat.abortingListener != NULL)
00398       {
00399         m_rtobj->removePostComponentActionListener(POST_ON_ABORTING,
00400                                                    m_compstat.abortingListener);
00401         m_compstat.abortingListener = NULL;
00402       }
00403     if (m_compstat.finalizeListener != NULL)
00404       {
00405         m_rtobj->removePostComponentActionListener(POST_ON_FINALIZE,
00406                                                    m_compstat.finalizeListener);
00407         m_compstat.finalizeListener = NULL;
00408       }
00409   }
00410 
00411   //============================================================
00412   // Port profile
00420   void ComponentObserverConsumer::
00421   setPortProfileListeners()
00422   {
00423     if (m_portaction.portAddListener == NULL)
00424       {
00425         m_portaction.portAddListener =
00426           m_rtobj->addPortActionListener(ADD_PORT,
00427                                          m_portaction,
00428                                          &PortAction::onAddPort);
00429       }
00430     if (m_portaction.portRemoveListener == NULL)
00431       {
00432         m_portaction.portRemoveListener =
00433           m_rtobj->addPortActionListener(REMOVE_PORT,
00434                                          m_portaction,
00435                                          &PortAction::onRemovePort);
00436       }
00437     if (m_portaction.portConnectListener == NULL)
00438       {
00439         m_portaction.portConnectListener =
00440           m_rtobj->addPortConnectRetListener(ON_CONNECTED,
00441                                              m_portaction,
00442                                              &PortAction::onConnect);
00443       }
00444     if (m_portaction.portDisconnectListener == NULL)
00445       {
00446         m_portaction.portDisconnectListener =
00447           m_rtobj->addPortConnectRetListener(ON_DISCONNECTED,
00448                                              m_portaction,
00449                                              &PortAction::onDisconnect);
00450       }
00451   }
00452 
00460   void ComponentObserverConsumer::unsetPortProfileListeners()
00461   {
00462     if (m_portaction.portAddListener != NULL)
00463       {
00464         m_rtobj->removePortActionListener(ADD_PORT,
00465                                           m_portaction.portAddListener);
00466         m_portaction.portAddListener = NULL;
00467       }
00468     if (m_portaction.portRemoveListener != NULL)
00469       {
00470         m_rtobj->removePortActionListener(REMOVE_PORT,
00471                                           m_portaction.portRemoveListener);
00472         m_portaction.portRemoveListener = NULL;
00473       }
00474     if (m_portaction.portConnectListener != NULL)
00475       {
00476         m_rtobj->removePortConnectRetListener(ON_CONNECTED,
00477                                               m_portaction.portConnectListener);
00478         m_portaction.portConnectListener = NULL;
00479       }
00480     if (m_portaction.portDisconnectListener != NULL)
00481       {
00482         m_rtobj->removePortConnectRetListener(ON_DISCONNECTED,
00483                                            m_portaction.portDisconnectListener);
00484         m_portaction.portDisconnectListener = NULL;
00485       }
00486   }
00487 
00488   //============================================================
00489   // ExecutionContext profile
00490 
00498   void ComponentObserverConsumer::setExecutionContextListeners()
00499   {
00500     if (m_ecaction.ecAttached == NULL)
00501       {
00502         m_ecaction.ecAttached =
00503           m_rtobj->addExecutionContextActionListener(EC_ATTACHED,
00504                                                      m_ecaction,
00505                                                      &ECAction::onAttached);
00506       }
00507     if (m_ecaction.ecDetached == NULL)
00508       {
00509         m_ecaction.ecDetached = 
00510           m_rtobj->addExecutionContextActionListener(EC_DETACHED,
00511                                                      m_ecaction,
00512                                                      &ECAction::onDetached);
00513       }
00514     if (m_ecaction.ecRatechanged == NULL)
00515       {
00516         m_ecaction.ecRatechanged = 
00517           m_rtobj->addPostComponentActionListener(POST_ON_RATE_CHANGED,
00518                                                   m_ecaction,
00519                                                   &ECAction::onRateChanged);
00520       }
00521     if (m_ecaction.ecStartup == NULL)
00522       {
00523         m_ecaction.ecStartup = 
00524           m_rtobj->addPostComponentActionListener(POST_ON_STARTUP,
00525                                                   m_ecaction,
00526                                                   &ECAction::onStartup);
00527       }
00528     if (m_ecaction.ecShutdown == NULL)
00529       {
00530         m_ecaction.ecShutdown = 
00531           m_rtobj->addPostComponentActionListener(POST_ON_SHUTDOWN,
00532                                                   m_ecaction,
00533                                                   &ECAction::onShutdown);
00534       }
00535   }
00536 
00544   void ComponentObserverConsumer::unsetExecutionContextListeners()
00545   {
00546     if (m_ecaction.ecAttached != NULL)
00547       {
00548         m_rtobj->removeExecutionContextActionListener(EC_ATTACHED,
00549                                                       m_ecaction.ecAttached);
00550       }
00551     if (m_ecaction.ecDetached != NULL)
00552       {
00553         m_rtobj->removeExecutionContextActionListener(EC_ATTACHED,
00554                                                       m_ecaction.ecDetached);
00555       }
00556     if (m_ecaction.ecRatechanged != NULL)
00557       {
00558         m_rtobj->removePostComponentActionListener(POST_ON_RATE_CHANGED,
00559                                                    m_ecaction.ecRatechanged);
00560       }
00561     if (m_ecaction.ecStartup != NULL)
00562       {
00563         m_rtobj->removePostComponentActionListener(POST_ON_STARTUP,
00564                                                    m_ecaction.ecStartup);
00565       }
00566     if (m_ecaction.ecShutdown != NULL)
00567       {
00568         m_rtobj->removePostComponentActionListener(POST_ON_SHUTDOWN,
00569                                                    m_ecaction.ecShutdown);
00570       }
00571   }
00572 
00573   //============================================================
00574   // ComponentProfile related functions
00582   void ComponentObserverConsumer::setComponentProfileListeners()
00583   {
00584   }
00585 
00593   void ComponentObserverConsumer::unsetComponentProfileListeners()
00594   {
00595   }
00596 
00597 
00598   //============================================================
00599   // Configuration
00600 
00601   void ComponentObserverConsumer::setConfigurationListeners()
00602   {
00603     m_configMsg.updateConfigParamListener = 
00604       m_rtobj->addConfigurationParamListener(ON_UPDATE_CONFIG_PARAM,
00605                                              m_configMsg,
00606                                              &ConfigAction::updateConfigParam);
00607     m_configMsg.setConfigSetListener = 
00608       m_rtobj->addConfigurationSetListener(ON_SET_CONFIG_SET,
00609                                              m_configMsg,
00610                                              &ConfigAction::setConfigSet);
00611     m_configMsg.addConfigSetListener = 
00612       m_rtobj->addConfigurationSetListener(ON_ADD_CONFIG_SET,
00613                                              m_configMsg,
00614                                              &ConfigAction::addConfigSet);
00615     m_configMsg.updateConfigSetListener = 
00616       m_rtobj->addConfigurationSetNameListener(ON_UPDATE_CONFIG_SET,
00617                                                m_configMsg,
00618                                                &ConfigAction::updateConfigSet);
00619     m_configMsg.removeConfigSetListener = 
00620       m_rtobj->addConfigurationSetNameListener(ON_REMOVE_CONFIG_SET,
00621                                                m_configMsg,
00622                                                &ConfigAction::removeConfigSet);
00623     m_configMsg.activateConfigSetListener = 
00624       m_rtobj->addConfigurationSetNameListener(ON_ACTIVATE_CONFIG_SET,
00625                                                m_configMsg,
00626                                               &ConfigAction::activateConfigSet);
00627   }
00628 
00636   void ComponentObserverConsumer::unsetConfigurationListeners()
00637   {
00638 
00639     if (m_configMsg.updateConfigParamListener != NULL)
00640       {
00641         m_rtobj->
00642           removeConfigurationParamListener(ON_UPDATE_CONFIG_PARAM,
00643                                       m_configMsg.updateConfigParamListener);
00644         m_configMsg.updateConfigParamListener = NULL;
00645       }
00646     if (m_configMsg.setConfigSetListener != NULL)
00647       {
00648         m_rtobj->removeConfigurationSetListener(ON_SET_CONFIG_SET,
00649                                            m_configMsg.setConfigSetListener);
00650         m_configMsg.setConfigSetListener = NULL;
00651       }
00652     if (m_configMsg.addConfigSetListener != NULL)
00653       {
00654         m_rtobj->removeConfigurationSetListener(ON_ADD_CONFIG_SET,
00655                                             m_configMsg.addConfigSetListener);
00656         m_configMsg.addConfigSetListener = NULL;
00657       }
00658     if (m_configMsg.updateConfigSetListener != NULL)
00659       {
00660         m_rtobj->removeConfigurationSetNameListener(ON_UPDATE_CONFIG_SET,
00661                                           m_configMsg.updateConfigSetListener);
00662         m_configMsg.updateConfigSetListener = NULL;
00663       }
00664     if (m_configMsg.removeConfigSetListener != NULL)
00665       {
00666         m_rtobj->removeConfigurationSetNameListener(ON_REMOVE_CONFIG_SET,
00667                                           m_configMsg.removeConfigSetListener);
00668         m_configMsg.removeConfigSetListener = NULL;
00669       }
00670     if (m_configMsg.activateConfigSetListener != NULL)
00671       {
00672         m_rtobj->removeConfigurationSetNameListener(ON_ACTIVATE_CONFIG_SET,
00673                                         m_configMsg.activateConfigSetListener);
00674         m_configMsg.activateConfigSetListener = NULL;
00675       }
00676   }
00677   
00678   
00679 }; // namespace RTC
00680 
00681 extern "C"
00682 {
00683   void ComponentObserverConsumerInit()
00684   {
00685     RTC::SdoServiceConsumerFactory& factory
00686       = RTC::SdoServiceConsumerFactory::instance();
00687     factory.addFactory(CORBA_Util::toRepositoryId<OpenRTM::ComponentObserver>(),
00688                        ::coil::Creator< ::RTC::SdoServiceConsumerBase,
00689                        ::RTC::ComponentObserverConsumer>,
00690                        ::coil::Destructor< ::RTC::SdoServiceConsumerBase,
00691                        ::RTC::ComponentObserverConsumer>);
00692   }
00693 };


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:03