00001
00020 #ifndef RTC_COMPONENTOBSERVERCONSUMER_H
00021 #define RTC_COMPONENTOBSERVERCONSUMER_H
00022
00023 #include <coil/Mutex.h>
00024 #include <coil/Factory.h>
00025 #include <coil/stringutil.h>
00026 #include <rtm/SdoServiceConsumerBase.h>
00027 #include <rtm/CorbaConsumer.h>
00028 #include <rtm/ComponentActionListener.h>
00029 #include <rtm/idl/SDOPackageStub.h>
00030 #include "ComponentObserverStub.h"
00031
00032 namespace RTC
00033 {
00034
00040 class ComponentObserverConsumer
00041 : public SdoServiceConsumerBase
00042 {
00043 public:
00051 ComponentObserverConsumer();
00052
00060 virtual ~ComponentObserverConsumer();
00061
00069 virtual bool init(RTObject_impl& rtobj,
00070 const SDOPackage::ServiceProfile& profile);
00071
00079 virtual bool reinit(const SDOPackage::ServiceProfile& profile);
00080
00088 virtual const SDOPackage::ServiceProfile& getProfile() const;
00089
00097 virtual void finalize();
00098
00099 protected:
00107 inline void updateStatus(OpenRTM::StatusKind statuskind, const char* msg)
00108 {
00109 try
00110 {
00111 m_observer->update_status(statuskind, msg);
00112 }
00113 catch (...)
00114 {
00115 m_rtobj->removeSdoServiceConsumer(m_profile.id);
00116 }
00117 }
00118
00126 inline const char* toString(OpenRTM::StatusKind kind)
00127 {
00128 static const char* kinds[] =
00129 {
00130 "COMPONENT_PROFILE",
00131 "RTC_STATUS",
00132 "EC_STATUS",
00133 "PORT_PROFILE",
00134 "CONFIGURATION",
00135 "HEARTBEAT"
00136 };
00137 return (size_t)kind < sizeof(kind)/sizeof(char*) ? kinds[kind] : "";
00138 }
00139
00147 void setListeners(coil::Properties& prop);
00148
00156 void switchListeners(bool& next, bool& pre,
00157 void (ComponentObserverConsumer::*setfunc)(),
00158 void (ComponentObserverConsumer::*unsetfunc)());
00159
00160
00161
00169 void heartbeat();
00170
00178 void setHeartbeat(coil::Properties& prop);
00179
00187 void unsetHeartbeat();
00188
00189
00190
00198 void setComponentStatusListeners();
00199
00207 void unsetComponentStatusListeners();
00208
00209
00210
00218 void setPortProfileListeners();
00219
00227 void unsetPortProfileListeners();
00228
00229
00230
00231
00239 void setExecutionContextListeners();
00240
00248 void unsetExecutionContextListeners();
00249
00250
00251
00252
00260 void setComponentProfileListeners();
00261
00269 void unsetComponentProfileListeners();
00270
00271
00272
00273
00281 void setConfigurationListeners();
00282
00290 void unsetConfigurationListeners();
00291
00292
00293 private:
00301 class CompStatMsg
00302 {
00303 public:
00304 CompStatMsg(ComponentObserverConsumer& coc)
00305 : activatedListener(NULL), deactivatedListener(NULL),
00306 resetListener(NULL), abortingListener(NULL),
00307 finalizeListener(NULL), m_coc(coc) {}
00308 void onGeneric(const char* msgprefix, UniqueId ec_id, ReturnCode_t ret)
00309 {
00310 if (ret == RTC::RTC_OK)
00311 {
00312 std::string msg(msgprefix);
00313 msg += coil::otos(ec_id);
00314 m_coc.updateStatus(OpenRTM::RTC_STATUS, msg.c_str());
00315 }
00316 }
00317 void onActivated(UniqueId ec_id, ReturnCode_t ret)
00318 {
00319 onGeneric("ACTIVE:", ec_id, ret);
00320 }
00321 void onDeactivated(UniqueId ec_id, ReturnCode_t ret)
00322 {
00323 onGeneric("INACTIVE:", ec_id, ret);
00324 }
00325 void onReset(UniqueId ec_id, ReturnCode_t ret)
00326 {
00327 onGeneric("INACTIVE:", ec_id, ret);
00328 }
00329 void onAborting(UniqueId ec_id, ReturnCode_t ret)
00330 {
00331 onGeneric("ERROR:", ec_id, ret);
00332 }
00333 void onFinalize(UniqueId ec_id, ReturnCode_t ret)
00334 {
00335 onGeneric("FINALIZE:", ec_id, ret);
00336 }
00337
00338 PostComponentActionListener* activatedListener;
00339 PostComponentActionListener* deactivatedListener;
00340 PostComponentActionListener* resetListener;
00341 PostComponentActionListener* abortingListener;
00342 PostComponentActionListener* finalizeListener;
00343 private:
00344 ComponentObserverConsumer& m_coc;
00345 };
00346
00354 class PortAction
00355 {
00356 public:
00357 PortAction(ComponentObserverConsumer& coc)
00358 : portAddListener(NULL), portRemoveListener(NULL),
00359 portConnectListener(NULL), portDisconnectListener(NULL),
00360 m_coc(coc) {}
00361 void onGeneric(const char* _msg, const char* portname)
00362 {
00363 std::string msg(_msg);
00364 msg += portname;
00365 m_coc.updateStatus(OpenRTM::PORT_PROFILE, msg.c_str());
00366 }
00367 void onAddPort(const ::RTC::PortProfile& pprof)
00368 {
00369 onGeneric("ADD:", static_cast<const char*>(pprof.name));
00370 }
00371 void onRemovePort(const ::RTC::PortProfile& pprof)
00372 {
00373 onGeneric("REMOVE:", static_cast<const char*>(pprof.name));
00374 }
00375 void onConnect(const char* portname,
00376 ::RTC::ConnectorProfile& pprof, ReturnCode_t ret)
00377 {
00378 if (ret == RTC::RTC_OK)
00379 {
00380 onGeneric("CONNECT:", portname);
00381 }
00382 }
00383 void onDisconnect(const char* portname,
00384 ::RTC::ConnectorProfile& pprof, ReturnCode_t ret)
00385 {
00386 if (ret == RTC::RTC_OK)
00387 {
00388 onGeneric("DISCONNECT:", portname);
00389 }
00390 }
00391
00392 PortActionListener* portAddListener;
00393 PortActionListener* portRemoveListener;
00394 PortConnectRetListener* portConnectListener;
00395 PortConnectRetListener* portDisconnectListener;
00396
00397 private:
00398 ComponentObserverConsumer& m_coc;
00399 };
00400
00408 class ECAction
00409 {
00410 public:
00411 ECAction(ComponentObserverConsumer& coc)
00412 : ecAttached(NULL), ecDetached(NULL), ecRatechanged(NULL),
00413 ecStartup(NULL), ecShutdown(NULL),
00414 m_coc(coc) {}
00415 void onGeneric(const char* _msg, UniqueId ec_id)
00416 {
00417 std::string msg(_msg + coil::otos(ec_id));
00418 m_coc.updateStatus(OpenRTM::EC_STATUS, msg.c_str());
00419 }
00420 void onAttached(UniqueId ec_id)
00421 {
00422 onGeneric("ATTACHED:", ec_id);
00423 }
00424 void onDetached(UniqueId ec_id)
00425 {
00426 onGeneric("DETACHED:", ec_id);
00427 }
00428 void onRateChanged(UniqueId ec_id, ReturnCode_t ret)
00429 {
00430 if (ret == RTC::RTC_OK)
00431 {
00432 onGeneric("RATE_CHANGED:", ec_id);
00433 }
00434 }
00435 void onStartup(UniqueId ec_id, ReturnCode_t ret)
00436 {
00437 if (ret == RTC::RTC_OK)
00438 {
00439 onGeneric("STARTUP:", ec_id);
00440 }
00441 }
00442 void onShutdown(UniqueId ec_id, ReturnCode_t ret)
00443 {
00444 if (ret == RTC::RTC_OK)
00445 {
00446 onGeneric("SHUTDOWN:", ec_id);
00447 }
00448 }
00449 ExecutionContextActionListener* ecAttached;
00450 ExecutionContextActionListener* ecDetached;
00451 PostComponentActionListener* ecRatechanged;
00452 PostComponentActionListener* ecStartup;
00453 PostComponentActionListener* ecShutdown;
00454 private:
00455 ComponentObserverConsumer& m_coc;
00456 };
00457
00465 class ConfigAction
00466 {
00467 public:
00468 ConfigAction(ComponentObserverConsumer& coc)
00469 : updateConfigParamListener(NULL), setConfigSetListener(NULL),
00470 addConfigSetListener(NULL), updateConfigSetListener(NULL),
00471 removeConfigSetListener(NULL), activateConfigSetListener(NULL),
00472 m_coc(coc) {}
00473 void updateConfigParam(const char* configsetname,
00474 const char* configparamname)
00475 {
00476 std::string msg("UPDATE_CONFIG_PARAM: ");
00477 msg += configsetname;
00478 msg += ".";
00479 msg += configparamname;
00480 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00481 }
00482 void setConfigSet(const coil::Properties& config_set)
00483 {
00484 std::string msg("SET_CONFIG_SET: ");
00485 msg += config_set.getName();
00486 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00487 }
00488 void addConfigSet(const coil::Properties& config_set)
00489 {
00490 std::string msg("ADD_CONFIG_SET: ");
00491 msg += config_set.getName();
00492 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00493 }
00494 void updateConfigSet(const char* config_set_name)
00495 {
00496 std::string msg("UPDATE_CONFIG_SET: ");
00497 msg += config_set_name;
00498 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00499 }
00500 void removeConfigSet(const char* config_set_name)
00501 {
00502 std::string msg("REMOVE_CONFIG_SET: ");
00503 msg += config_set_name;
00504 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00505 }
00506 void activateConfigSet(const char* config_set_name)
00507 {
00508 std::string msg("ACTIVATE_CONFIG_SET: ");
00509 msg += config_set_name;
00510 m_coc.updateStatus(OpenRTM::CONFIGURATION, msg.c_str());
00511 }
00512
00513 ConfigurationParamListener* updateConfigParamListener;
00514 ConfigurationSetListener* setConfigSetListener;
00515 ConfigurationSetListener* addConfigSetListener;
00516 ConfigurationSetNameListener* updateConfigSetListener;
00517 ConfigurationSetNameListener* removeConfigSetListener;
00518 ConfigurationSetNameListener* activateConfigSetListener;
00519
00520 private:
00521 ComponentObserverConsumer& m_coc;
00522 };
00523
00524
00525
00526 RTC::RTObject_impl* m_rtobj;
00527 SDOPackage::ServiceProfile m_profile;
00528 CorbaConsumer<OpenRTM::ComponentObserver> m_observer;
00529
00530 bool m_observed[OpenRTM::STATUS_KIND_NUM];
00531
00532 CompStatMsg m_compstat;
00533 PortAction m_portaction;
00534 ECAction m_ecaction;
00535 ConfigAction m_configMsg;
00536
00537 coil::TimeValue m_interval;
00538 bool m_heartbeat;
00539 ListenerId m_hblistenerid;
00540
00541
00542 coil::Timer m_timer;
00543
00544 };
00545
00546 };
00547
00548 extern "C"
00549 {
00550 DLL_EXPORT void ComponentObserverConsumerInit();
00551 };
00552
00553 #endif // RTC_COMPONENTOBSERVERCONSUMER_H
00554
00555