Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RtcBase.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00022 #ifndef __RtcBase_h__
00023 #define __RtcBase_h__
00024 
00025 // CORBA header include
00026 #include "rtm/RTC.h"
00027 
00028 // STL includes
00029 #include <list>
00030 #include <string>
00031 #include <algorithm>
00032 #include <functional>
00033 
00034 // local includes 
00035 #include "rtm/idl/RTCBaseSkel.h"
00036 #include "rtm/idl/RTCBaseStub.h"
00037 #include <ace/Task.h>
00038 #include "rtm/RtcOutPort.h"
00039 //#include "rtm/RtcProfile.h"
00040 #include "rtm/RtcModuleProfile.h"
00041 //#include "rtm/RtcConfiguration.h"
00042 
00043 
00044 #define USLEEP(x) usleep(x);
00045 
00046 namespace RTM {
00047   // namespace
00048   using namespace std;
00049   
00050   
00051   class InPortBase;
00052   class OutPortBase;
00053   class RtcManager;
00054   
00055   static char* StateString[] =
00056         {
00057           "RTC_UNKNOWN",
00058           "RTC_BORN",
00059           "RTC_INITIALIZING",
00060           "RTC_READY",
00061           "RTC_STARTING",
00062           "RTC_ACTIVE",
00063           "RTC_STOPPING",
00064           "RTC_ABORTING",
00065           "RTC_ERROR",
00066           "RTC_FATAL_ERROR",
00067           "RTC_EXITING",
00068           "UNKNOWN"
00069         };
00070 
00071   typedef enum NamingPolicy {
00072         UNKNOWN = 0,
00073         LONGNAME_ENABLE = 1,
00074         ALIAS_ENABLE = 2
00075   };
00076 
00124   class RtcBase  
00125         : public virtual POA_RTM::RTCBase, 
00126       public virtual PortableServer::RefCountServantBase,
00127       public ACE_Task<ACE_MT_SYNCH>
00128   {
00129   public:
00130 
00150         RtcBase();
00151         
00176         RtcBase(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
00177 
00195         RtcBase(RtcManager* manager);
00196         
00208         virtual ~RtcBase();
00209         
00210         //============================================================
00211         //
00212         // <<< CORBA interfaces >>>
00213         //
00214         //============================================================
00215         
00216         //------------------------------------------------------------
00217         // Transition operation of component activity state
00218         //------------------------------------------------------------
00234         virtual RtmRes rtc_start()
00235           throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00236         
00252         virtual RtmRes rtc_stop()
00253           throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00254         
00271         virtual RtmRes rtc_reset()
00272           throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00273         
00288         virtual RtmRes rtc_exit()
00289           throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00290         
00305         virtual RtmRes rtc_kill();
00306         
00307         //------------------------------------------------------------
00308         // Main activity
00309         //------------------------------------------------------------
00329         virtual RtmRes rtc_worker();
00330         
00331         
00332         // ** Steady state **
00333         //------------------------------------------------------------
00334         // Ready state methods
00335         //------------------------------------------------------------
00351         virtual RtmRes rtc_ready_entry()
00352         {USLEEP(1000); return RTM_OK;};
00353         
00368         virtual RtmRes rtc_ready_do()
00369         {USLEEP(1000); return RTM_OK;};
00370         
00385         virtual RtmRes rtc_ready_exit()
00386         {USLEEP(1000); return RTM_OK;};
00387         
00388         
00389         //------------------------------------------------------------
00390         // Active state methods
00391         //------------------------------------------------------------
00406         virtual RtmRes rtc_active_entry()
00407         {USLEEP(1000); return RTM_OK;};
00408         
00423         virtual RtmRes rtc_active_do()
00424         {USLEEP(1000); return RTM_OK;};
00425         
00440         virtual RtmRes rtc_active_exit()
00441         {USLEEP(1000); return RTM_OK;};
00442         
00443         
00444         //------------------------------------------------------------
00445         // Error state methods
00446         //------------------------------------------------------------
00461         virtual RtmRes rtc_error_entry()
00462         {USLEEP(1000); return RTM_OK;};
00463         
00478         virtual RtmRes rtc_error_do()
00479         {USLEEP(1000); return RTM_OK;};
00480         
00495         virtual RtmRes rtc_error_exit()
00496         {USLEEP(1000); return RTM_OK;};
00497         
00498         
00499         //------------------------------------------------------------
00500         // FatalError state methods
00501         //------------------------------------------------------------
00517         virtual RtmRes rtc_fatal_entry()
00518         {USLEEP(1000); return RTM_OK;};
00519         
00535         virtual RtmRes rtc_fatal_do()
00536         {USLEEP(1000); return RTM_OK;};
00537         
00553         virtual RtmRes rtc_fatal_exit()
00554         {USLEEP(1000); return RTM_OK;};
00555         //------------------------------------------------------------
00556         
00557         // ** Transient state **
00575         virtual RtmRes rtc_init_entry()
00576         {USLEEP(1000); return RTM_OK;};
00577         
00595         virtual RtmRes rtc_starting_entry()
00596         {USLEEP(1000); return RTM_OK;};
00597         
00615         virtual RtmRes rtc_stopping_entry()
00616         {USLEEP(1000); return RTM_OK;};
00617         
00635         virtual RtmRes rtc_aborting_entry()
00636         {USLEEP(1000); return RTM_OK;};
00637         
00655         virtual RtmRes rtc_exiting_entry()
00656         {USLEEP(1000); return RTM_OK;};
00657         
00658         
00659         //------------------------------------------------------------
00660         // Status handling
00661         //------------------------------------------------------------
00674         virtual OutPort_ptr rtc_state();
00675         
00676         //------------------------------------------------------------
00677         // Get inports and outport object reference
00678         //------------------------------------------------------------
00691         virtual InPortList* inports() ;
00692 
00709         virtual InPort_ptr get_inport(const char* name)
00710           throw (CORBA::SystemException, RTM::RTComponent::NoSuchName); 
00711         
00724         virtual OutPortList* outports() ;
00725 
00742         virtual OutPort_ptr get_outport(const char* name)
00743           throw (CORBA::SystemException, RTM::RTComponent::NoSuchName);
00744         
00745         //------------------------------------------------------------
00746         // [CORBA interface] Getting component property
00747         //------------------------------------------------------------
00748         //  CORBA::Any* get_property(const char* name) {return NULL;};
00749         //  char* get_properties_xml() {return NULL;};
00750         //  virtual char* instance_name() {return "RtcBase0";}
00751         //  virtual char* class_name() {return "RtcBase";}
00752 
00765         virtual char* instance_id();
00766 
00779     virtual char* implementation_id();
00780 
00793     virtual char* description();
00794 
00807     virtual char* version();
00808 
00821     virtual char* maker();
00822 
00835     virtual char* category();
00836 
00849         virtual RTCProfile* profile() ;
00850         //      virtual RTCConfiguration* configuration() ;
00851 
00852         
00853         
00854         //------------------------------------------------------------
00855         // [CORBA interface] composit component interfaces
00856         //
00857         // These interfaces are methods for RTCompositComponet
00858         // Almost all methods do nothing. Only rtc_components and 
00859         // rtc_get_componet methods return this component itself.
00860         //------------------------------------------------------------
00873         virtual RtmRes rtc_start_thread();
00874 
00887         virtual RtmRes rtc_stop_thread();
00888 
00901         virtual RtmRes rtc_set_parent(RTCBase_ptr comp);
00902 
00916         virtual RtmRes rtc_add_component(RTCBase_ptr comp)
00917         {return RTM_ERR;};
00918 
00932         virtual RtmRes rtc_delete_component(RTCBase_ptr comp)
00933         {return RTM_ERR;} ;
00934 
00949         virtual RtmRes rtc_replace_component(RTCBase_ptr comp1, RTCBase_ptr comp2)
00950         {return RTM_ERR;};
00951 
00965         virtual RtmRes rtc_replace_component_by_name(const char* name1, const char* name2)
00966         {return RTM_ERR;};
00967 
00981         virtual RTCBaseList* rtc_components()
00982         {
00983           RTCBaseList_var myself = new RTCBaseList();
00984           myself->length(1);
00985 
00986 #ifdef ORB_IS_TAO
00987           (*myself)[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
00988 #else
00989           myself[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
00990 #endif
00991           return myself._retn();
00992         };
00993 
01007         virtual RTCBase_ptr rtc_get_component(const char* name)
01008         {
01009           return dynamic_cast<RTCBase_ptr>(m_pPOA->servant_to_reference(this));
01010         };
01011 
01025         virtual RtmRes rtc_attach_inport(InPort_ptr in_port)
01026         {return RTM_ERR;};
01027 
01042         virtual RtmRes rtc_attach_inport_by_name(RTCBase_ptr comp, const char* name)
01043         {return RTM_ERR;};
01044 
01045 
01059         virtual RtmRes rtc_detatch_inport(InPort_ptr in_port)
01060         {return RTM_ERR;};
01061 
01076         virtual RtmRes rtc_detatch_inport_by_name(const char* name)
01077         {return RTM_ERR;};
01078 
01092         virtual RtmRes rtc_attach_outport(OutPort_ptr out_port)
01093         {return RTM_ERR;} ;
01094 
01109         virtual RtmRes rtc_attach_outport_by_name(RTCBase_ptr comp,
01110                                                                                           const char* name)
01111         {return RTM_ERR;};
01112 
01126         virtual RtmRes rtc_detatch_outport(OutPort_ptr out_port)
01127         {return RTM_ERR;}
01128 
01143         virtual RtmRes rtc_detatch_outport_by_name(const char* name)
01144          {return RTM_ERR;};
01145         
01146         // end of CORBA interface definition
01147         //============================================================
01148         
01149         
01150         
01151         //============================================================
01152         //
01153         // <<< Local interfaces >>>
01154         //
01155         //============================================================
01156         
01175         virtual void init_orb(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
01176         
01196         virtual int open(void *args);
01197         
01214         virtual int svc(void);
01215         
01216         
01217         //============================================================
01218         // Component Property handling methods
01219         //============================================================
01220         // @brief Get component class name.
01221 
01240         virtual void initModuleProfile(RtcModuleProfile prof);
01241 
01257         virtual RtcModuleProfile& getModuleProfile()
01258         {
01259           return m_Profile;
01260         }
01261         /*
01262         virtual RtcComponentProfile& getProfile()
01263         {
01264           return m_Profile;
01265         }
01266         */
01267         /*
01268         virtual RtcConfiguration& getConfiguration()
01269         {
01270           return m_Configuration;
01271         }
01272         */
01296         virtual string setComponentName(int num);
01297 
01298         //============================================================
01299         // Input port handling
01300         //============================================================
01323         bool registerInPort(InPortBase& in_ch);
01324 
01347         bool registerPort(InPortBase& in_ch)
01348         {
01349           return registerInPort(in_ch);
01350         };
01351         
01372         bool deleteInPort(InPortBase& in_ch);
01373 
01394         bool deletePort(InPortBase& in_ch)
01395         {
01396           return deleteInPort(in_ch);
01397         };
01398         
01419         bool deleteInPortByName(const char* ch_name);
01420         
01421         //============================================================
01422         // Output channel handling
01423         //============================================================
01446         bool registerOutPort(OutPortBase& out_ch);
01447         bool registerPort(OutPortBase& out_ch)
01448         {
01449           return registerOutPort(out_ch);
01450         };
01451         
01473         bool deleteOutPort(OutPortBase& out_ch);
01474 
01496         bool deletePort(OutPortBase& out_ch)
01497         {
01498           return deleteOutPort(out_ch);
01499         };
01500         
01521         bool deleteOutPortByName(const char* ch_name);
01522         
01553         void appendAlias(const char* alias);
01554 
01585         void appendAlias(const std::string alias);
01599         std::list<string> getAliases();
01600 
01601         void setNamingPolicy(NamingPolicy policy);
01602         NamingPolicy getNamingPolicy();
01603         bool isLongNameEnable();
01604         bool isAliasEnable();
01605   
01606   protected:
01607         
01608         
01609         
01610         //============================================================
01611         // State implementation functions 
01612         //============================================================
01613         /*
01614          * 
01615          *                                   __                      __
01616          *                         _________V_ \          __________V_ \
01617          *                        |           |/  (any)->|            |/
01618          *                        |  Active   |          | FatalError |
01619          *                        |___________|--,       |____________|
01620          *                            A    |  ___|______      |
01621          *                      ______|___ | |          |     |
01622          *                     |          || | Aborting |     |
01623          *                     | Starting || |__________|     |
01624          *                     |__________||     |   __       |
01625          *             ____________   |    |   __V__V_ \   ___V_____
01626          *   _____    |            |<-+----+--|       |/  |         |    ___
01627          *  (Start)-->|Initializing|  |    |  | Error |-->| Exiting |-->(End)
01628          *   ~~~~~    |____________|--+----+->|_______|   |_________|    ~~~
01629          *                A  |        | ___|______  A         A
01630          *                |  |        ||          | |         |
01631          *                |  |        || Stopping | |         |
01632          *                |  |        ||__________| |         |
01633          *                |  |        |    |        |         |
01634          *                |  |     ___|____V__      |         |
01635          *                |  `--->|           |-----'         |
01636          *                `-------|   Ready   |               |
01637          *                       /|___________|---------------'
01638          *                       \__A
01639          *
01640          */
01648         typedef RTM::RTComponent::ComponentState ComponentState;
01649         /*
01650         static ComponentState RTC_UNKNOWN      = RTM::RTComponent::RTC_UNKNOWN;
01651         static ComponentState RTC_BORN         = RTM::RTComponent::RTC_BORN;
01652         static ComponentState RTC_INITIALIZING = RTM::RTComponent::RTC_INITIALIZING;
01653         static ComponentState RTC_READY        = RTM::RTComponent::RTC_READY;
01654         static ComponentState RTC_STARTING     = RTM::RTComponent::RTC_STARTING;
01655         static ComponentState RTC_ACTIVE       = RTM::RTComponent::RTC_ACTIVE;
01656         static ComponentState RTC_STOPPING     = RTM::RTComponent::RTC_STOPPING;
01657         static ComponentState RTC_ABORTING     = RTM::RTComponent::RTC_ABORTING;
01658         static ComponentState RTC_ERROR        = RTM::RTComponent::RTC_ERROR;
01659         static ComponentState RTC_FATAL_ERROR  = RTM::RTComponent::RTC_FATAL_ERROR;
01660         static ComponentState RTC_EXITING      = RTM::RTComponent::RTC_EXITING;
01661         */
01662 
01663         
01664   protected:
01672         CORBA::ORB_ptr m_pORB;
01673 
01681         PortableServer::POA_ptr m_pPOA;
01682 
01690         RTCBase_var m_Parent;
01691 
01699         class ThreadState
01700         {
01701         public:
01702           ThreadState() : m_Flag(true){};
01703           bool m_Flag;
01704           ACE_Thread_Mutex m_Mutex;
01705         };
01713         ThreadState m_ThreadState;
01714 
01722         class eq_name
01723         {
01724         public:
01725           const string m_name;
01726           eq_name(const char* name) : m_name(name){};
01727           bool operator()(InPortBase* ch)
01728           {
01729                 string ch_name(ch->name());
01730                 return m_name == ch_name;
01731           };
01732           bool operator()(OutPortBase* ch)
01733           {
01734                 string ch_name(ch->name());
01735                 return m_name == ch_name;
01736           };
01737         };
01738 
01746         struct ComponentStateMtx {
01747         public:
01755           ComponentStateMtx():_state(RTM::RTComponent::RTC_BORN){};
01756 
01764           ComponentState _state;
01765           
01773           ACE_Thread_Mutex _mutex;
01774         };
01775 
01783         ComponentStateMtx m_CurrentState;
01784 
01792         ComponentStateMtx m_NextState;
01793         
01801         typedef RtmRes (RtcBase::*StateFunc)();
01802         
01803         //------------------------------------------------------------
01804         // Function pointer tables for state action functions.
01805         //------------------------------------------------------------
01813         StateFunc _exit_func[11];
01814 
01822         StateFunc _entry_func[11];
01823 
01831         StateFunc _do_func[11];
01832         
01833         //------------------------------------------------------------
01834         // State action functions
01835         //------------------------------------------------------------
01836         // 
01844         RtmRes _check_error(RtmRes result);
01845 
01853         RtmRes _nop(){return RTM_OK;}
01854 
01862         RtmRes _rtc_initializing();
01863 
01871         RtmRes _rtc_ready_entry();
01879         RtmRes _rtc_starting();
01887         RtmRes _rtc_active_entry();
01895         RtmRes _rtc_stopping();
01903         RtmRes _rtc_aborting();
01904 
01912         RtmRes _rtc_error_entry();
01913 
01921         RtmRes _rtc_fatal_entry();
01922 
01930         RtmRes _rtc_exiting();
01931 
01939         RtmRes _rtc_ready();
01940 
01948         RtmRes _rtc_active();
01949 
01957         RtmRes _rtc_error();
01958 
01966         RtmRes _rtc_fatal();
01967 
01975         RtmRes _rtc_ready_exit();
01976 
01984         RtmRes _rtc_active_exit();
01985 
01993         RtmRes _rtc_error_exit();
01994 
02002         RtmRes _rtc_fatal_exit();
02003         
02011         void init_state_func_table();
02012         
02013 
02021         typedef list<InPortBase*>::iterator InPorts_it;
02022         
02030         typedef list<OutPortBase*>::iterator OutPorts_it;
02031         
02039         struct InPorts
02040         {
02041           list<InPortBase*> m_List;
02042           ACE_Thread_Mutex m_Mutex;
02043         };
02051         InPorts m_InPorts;
02052         
02060         struct OutPorts
02061         {
02062           list<OutPortBase*> m_List;
02063           ACE_Thread_Mutex m_Mutex;
02064         };
02072         OutPorts m_OutPorts;
02073         
02075         //  list<const bool*> m_InFlags;
02076         
02084         TimedState m_TimedState;
02092         OutPortAny<TimedState> m_StatePort;                                                                       
02093 
02101         RtcModuleProfile m_Profile;
02102         //      RtcManagerProfile m_ManagerProfile;
02103         //      RtcComponentProfile m_Profile;
02104         //      RtcConfiguration m_Configuration;
02105 
02113         std::list<string> m_Alias;
02114 
02115         NamingPolicy m_NamingPolicy;
02116 
02117   };
02118 
02119   //------------------------------------------------------------
02120   // RTComponent factory function type
02121   //------------------------------------------------------------
02122 
02123   class RtcManager;
02131   typedef RtcBase* (*RtcNewFunc)(RtcManager* manager);
02132 
02140   typedef void (*RtcDeleteFunc)(RtcBase* rtc);
02141 
02142 };  // end of namespace RTM
02143 
02144 
02145 #endif // __RtcBase_h__

Generated on Thu Dec 16 15:29:18 2004 for OpenRTM by doxygen 1.3.6