00001
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #ifndef RtcBase_h
00038 #define RtcBase_h
00039
00040
00041 #include "rtm/RTC.h"
00042
00043
00044 #include <list>
00045 #include <string>
00046 #include <algorithm>
00047 #include <functional>
00048
00049
00050 #include <ace/Task.h>
00051
00052
00053 #include "rtm/idl/RTCBaseSkel.h"
00054 #include "rtm/idl/RTCBaseStub.h"
00055 #include "rtm/RtcOutPort.h"
00056 #include "rtm/RtcModuleProfile.h"
00057 #include "rtm/RtcSystemLogger.h"
00058 #include "rtm/RtcServiceBase.h"
00059
00060 #define USLEEP(x) \
00061 { \
00062 ACE_Time_Value tv(0, x);\
00063 ACE_OS::sleep(tv);\
00064 }
00065
00066 namespace RTM {
00067
00068 using namespace std;
00069
00070
00071 class InPortBase;
00072 class OutPortBase;
00073 class RtcManager;
00074
00075 static char* StateString[] =
00076 {
00077 "RTC_UNKNOWN",
00078 "RTC_BORN",
00079 "RTC_INITIALIZING",
00080 "RTC_READY",
00081 "RTC_STARTING",
00082 "RTC_ACTIVE",
00083 "RTC_STOPPING",
00084 "RTC_ABORTING",
00085 "RTC_ERROR",
00086 "RTC_FATAL_ERROR",
00087 "RTC_EXITING",
00088 "UNKNOWN"
00089 };
00090
00098 typedef RTM::RTComponent::ComponentState ComponentState;
00099
00100 const ComponentState RTC_UNKNOWN = 0;
00101 const ComponentState RTC_BORN = 1;
00102 const ComponentState RTC_INITIALIZING = 2;
00103 const ComponentState RTC_READY = 3;
00104 const ComponentState RTC_STARTING = 4;
00105 const ComponentState RTC_ACTIVE = 5;
00106 const ComponentState RTC_STOPPING = 6;
00107 const ComponentState RTC_ABORTING = 7;
00108 const ComponentState RTC_ERROR = 8;
00109 const ComponentState RTC_FATAL_ERROR = 9;
00110 const ComponentState RTC_EXITING = 10;
00111
00112 typedef enum NamingPolicy {
00113 UNKNOWN = 0,
00114 LONGNAME_ENABLE = 1,
00115 ALIAS_ENABLE = 2
00116 };
00117
00165 class EXPORTS RtcBase
00166 : public virtual POA_RTM::RTCBase,
00167 public virtual PortableServer::RefCountServantBase,
00168 public ACE_Task<ACE_MT_SYNCH>
00169 {
00170 public:
00171
00191 RtcBase();
00192
00217 RtcBase(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
00218
00236 RtcBase(RtcManager* manager);
00237
00249 virtual ~RtcBase();
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00307 virtual RtmRes rtc_start()
00308 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00309
00325 virtual RtmRes rtc_stop()
00326 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00327
00344 virtual RtmRes rtc_reset()
00345 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00346
00361 virtual RtmRes rtc_exit()
00362 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00363
00378 virtual RtmRes rtc_kill();
00379
00380
00381
00382
00402 virtual RtmRes rtc_worker();
00403
00404
00405
00406
00407
00408
00424 virtual RtmRes rtc_ready_entry()
00425 {USLEEP(1000); return RTM_OK;};
00426
00441 virtual RtmRes rtc_ready_do()
00442 {USLEEP(1000); return RTM_OK;};
00443
00458 virtual RtmRes rtc_ready_exit()
00459 {USLEEP(1000); return RTM_OK;};
00460
00461
00462
00463
00464
00479 virtual RtmRes rtc_active_entry()
00480 {USLEEP(1000); return RTM_OK;};
00481
00496 virtual RtmRes rtc_active_do()
00497 {USLEEP(1000); return RTM_OK;};
00498
00513 virtual RtmRes rtc_active_exit()
00514 {USLEEP(1000); return RTM_OK;};
00515
00516
00517
00518
00519
00534 virtual RtmRes rtc_error_entry()
00535 {USLEEP(1000); return RTM_OK;};
00536
00551 virtual RtmRes rtc_error_do()
00552 {USLEEP(1000); return RTM_OK;};
00553
00568 virtual RtmRes rtc_error_exit()
00569 {USLEEP(1000); return RTM_OK;};
00570
00571
00572
00573
00574
00590 virtual RtmRes rtc_fatal_entry()
00591 {USLEEP(1000); return RTM_OK;};
00592
00608 virtual RtmRes rtc_fatal_do()
00609 {USLEEP(1000); return RTM_OK;};
00610
00626 virtual RtmRes rtc_fatal_exit()
00627 {USLEEP(1000); return RTM_OK;};
00628
00629
00630
00648 virtual RtmRes rtc_init_entry()
00649 {USLEEP(1000); return RTM_OK;};
00650
00668 virtual RtmRes rtc_starting_entry()
00669 {USLEEP(1000); return RTM_OK;};
00670
00688 virtual RtmRes rtc_stopping_entry()
00689 {USLEEP(1000); return RTM_OK;};
00690
00708 virtual RtmRes rtc_aborting_entry()
00709 {USLEEP(1000); return RTM_OK;};
00710
00728 virtual RtmRes rtc_exiting_entry()
00729 {USLEEP(1000); return RTM_OK;};
00730
00731
00732
00733
00734
00747 virtual OutPort_ptr rtc_state();
00748
00749
00750
00751
00764 virtual InPortList* inports() ;
00765
00782 virtual InPort_ptr get_inport(const char* name)
00783 throw (CORBA::SystemException, RTM::RTComponent::NoSuchName);
00784
00797 virtual OutPortList* outports() ;
00798
00815 virtual OutPort_ptr get_outport(const char* name)
00816 throw (CORBA::SystemException, RTM::RTComponent::NoSuchName);
00817
00818
00831 virtual RTCServiceProfileList* get_service_profiles();
00832
00845 virtual RTCServiceProfile* get_service_profile(const char* name);
00846
00859 virtual RTCService_ptr get_service(const char* name);
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00882 virtual char* instance_id();
00883
00896 virtual char* implementation_id();
00897
00910 virtual char* description();
00911
00924 virtual char* version();
00925
00938 virtual char* maker();
00939
00952 virtual char* category();
00953
00966 virtual RTCProfile* profile() ;
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00992 virtual RtmRes rtc_start_thread();
00993
01006 virtual RtmRes rtc_suspend_thread();
01007
01020 virtual RtmRes rtc_stop_thread();
01021
01034 virtual RtmRes rtc_set_parent(RTCBase_ptr comp);
01035
01049 virtual RtmRes rtc_add_component(RTCBase_ptr comp)
01050 {return RTM_ERR;};
01051
01065 virtual RtmRes rtc_delete_component(RTCBase_ptr comp)
01066 {return RTM_ERR;} ;
01067
01082 virtual RtmRes rtc_replace_component(RTCBase_ptr comp1, RTCBase_ptr comp2)
01083 {return RTM_ERR;};
01084
01098 virtual RtmRes rtc_replace_component_by_name(const char* name1, const char* name2)
01099 {return RTM_ERR;};
01100
01114 virtual RTCBaseList* rtc_components()
01115 {
01116 RTCBaseList_var myself = new RTCBaseList();
01117 myself->length(1);
01118
01119 #ifdef ORB_IS_TAO
01120 (*myself)[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
01121 #else
01122 myself[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
01123 #endif
01124 return myself._retn();
01125 };
01126
01140 virtual RTCBase_ptr rtc_get_component(const char* name)
01141 {
01142 return dynamic_cast<RTCBase_ptr>(m_pPOA->servant_to_reference(this));
01143 };
01144
01158 virtual RtmRes rtc_attach_inport(InPort_ptr in_port)
01159 {return RTM_ERR;};
01160
01175 virtual RtmRes rtc_attach_inport_by_name(RTCBase_ptr comp, const char* name)
01176 {return RTM_ERR;};
01177
01178
01192 virtual RtmRes rtc_detatch_inport(InPort_ptr in_port)
01193 {return RTM_ERR;};
01194
01209 virtual RtmRes rtc_detatch_inport_by_name(const char* name)
01210 {return RTM_ERR;};
01211
01225 virtual RtmRes rtc_attach_outport(OutPort_ptr out_port)
01226 {return RTM_ERR;} ;
01227
01242 virtual RtmRes rtc_attach_outport_by_name(RTCBase_ptr comp,
01243 const char* name)
01244 {return RTM_ERR;};
01245
01259 virtual RtmRes rtc_detatch_outport(OutPort_ptr out_port)
01260 {return RTM_ERR;}
01261
01276 virtual RtmRes rtc_detatch_outport_by_name(const char* name)
01277 {return RTM_ERR;};
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01308 virtual void init_orb(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
01309
01329 virtual int open(void *args);
01330
01347 virtual int svc(void);
01348
01349
01368 virtual int close(unsigned long flags);
01369
01383 virtual RTM::RTComponent::ComponentState getState();
01384
01385
01386
01387
01388
01389
01408 virtual void initModuleProfile(RtcModuleProfile prof);
01409
01425 virtual RtcModuleProfile& getModuleProfile()
01426 {
01427 return m_Profile;
01428 }
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01464 virtual string setComponentName(int num);
01465
01466
01467
01468
01491 bool registerInPort(InPortBase& in_ch);
01492
01515 bool registerPort(InPortBase& in_ch)
01516 {
01517 return registerInPort(in_ch);
01518 };
01519
01540 bool deleteInPort(InPortBase& in_ch);
01541
01562 bool deletePort(InPortBase& in_ch)
01563 {
01564 return deleteInPort(in_ch);
01565 };
01566
01587 bool deleteInPortByName(const char* ch_name);
01588
01606 void readAllInPorts();
01607
01608
01624 void finalizeInPorts();
01625
01626
01627
01628
01651 bool registerOutPort(OutPortBase& out_ch);
01652 bool registerPort(OutPortBase& out_ch)
01653 {
01654 return registerOutPort(out_ch);
01655 };
01656
01678 bool deleteOutPort(OutPortBase& out_ch);
01679
01701 bool deletePort(OutPortBase& out_ch)
01702 {
01703 return deleteOutPort(out_ch);
01704 };
01705
01726 bool deleteOutPortByName(const char* ch_name);
01727
01745 void writeAllOutPorts();
01746
01747
01764 void finalizeOutPorts();
01765
01766
01767 bool registerService(RtcServiceBase& service,
01768 RtcServiceProfile& profile);
01769
01770
01771
01772
01803 void appendAlias(const char* alias);
01804
01835 void appendAlias(const std::string alias);
01836
01850 std::list<string> getAliases();
01851
01852 void setNamingPolicy(NamingPolicy policy);
01853 NamingPolicy getNamingPolicy();
01854 bool isLongNameEnable();
01855 bool isAliasEnable();
01856
01857 void forceExit();
01858
01874 void finalize();
01875
01876 bool isThreadRunning();
01877
01878
01879 protected:
01887 CORBA::ORB_ptr m_pORB;
01888
01896 PortableServer::POA_ptr m_pPOA;
01897
01905 RtcManager* m_pManager;
01906
01914 RTCBase_var m_Parent;
01915
01923 RTCBase_var m_MyObjRef;
01924
01932 typedef enum ThreadStates {
01933 UNKNOWN,
01934 RUNNING,
01935 SUSPEND,
01936 STOP
01937 };
01938 class ThreadState
01939 {
01940 public:
01941 ThreadState() : m_Flag(RUNNING){};
01942 ThreadStates m_Flag;
01943 ACE_Thread_Mutex m_Mutex;
01944 };
01952 ThreadState m_ThreadState;
01953
01961 class eq_name
01962 {
01963 public:
01964 const string m_name;
01965 eq_name(const char* name) : m_name(name){};
01966 bool operator()(InPortBase* ch)
01967 {
01968 string ch_name(ch->name());
01969 return m_name == ch_name;
01970 };
01971 bool operator()(OutPortBase* ch)
01972 {
01973 string ch_name(ch->name());
01974 return m_name == ch_name;
01975 };
01976 };
01977
01985 struct ComponentStateMtx {
01986 public:
01994 ComponentStateMtx():_state(RTM::RTComponent::RTC_BORN){};
01995
02003 ComponentState _state;
02004
02012 ACE_Thread_Mutex _mutex;
02013 };
02014
02022 ComponentStateMtx m_CurrentState;
02023
02031 ComponentStateMtx m_NextState;
02032
02040 typedef RtmRes (RtcBase::*StateFunc)();
02041
02042
02043
02044
02052 StateFunc _exit_func[11];
02053
02061 StateFunc _entry_func[11];
02062
02070 StateFunc _do_func[11];
02071
02072
02073
02074
02075
02083 RtmRes _check_error(RtmRes result);
02084
02092 RtmRes _nop(){return RTM_OK;}
02093
02101 RtmRes _rtc_initializing();
02102
02110 RtmRes _rtc_ready_entry();
02118 RtmRes _rtc_starting();
02126 RtmRes _rtc_active_entry();
02134 RtmRes _rtc_stopping();
02142 RtmRes _rtc_aborting();
02143
02151 RtmRes _rtc_error_entry();
02152
02160 RtmRes _rtc_fatal_entry();
02161
02169 RtmRes _rtc_exiting();
02170
02178 RtmRes _rtc_ready();
02179
02187 RtmRes _rtc_active();
02188
02196 RtmRes _rtc_error();
02197
02205 RtmRes _rtc_fatal();
02206
02214 RtmRes _rtc_ready_exit();
02215
02223 RtmRes _rtc_active_exit();
02224
02232 RtmRes _rtc_error_exit();
02233
02241 RtmRes _rtc_fatal_exit();
02242
02250 void init_state_func_table();
02251
02252
02260 typedef list<InPortBase*>::iterator InPorts_it;
02261
02269 typedef list<OutPortBase*>::iterator OutPorts_it;
02270
02278 struct InPorts
02279 {
02280 std::list<InPortBase*> m_List;
02281 ACE_Thread_Mutex m_Mutex;
02282 };
02290 InPorts m_InPorts;
02291
02299 struct OutPorts
02300 {
02301 std::list<OutPortBase*> m_List;
02302 ACE_Thread_Mutex m_Mutex;
02303 };
02311 OutPorts m_OutPorts;
02312
02313
02321 TimedState m_TimedState;
02329 OutPortAny<TimedState> m_StatePort;
02330
02338 RtcModuleProfile m_Profile;
02339
02340
02341
02342
02350 std::list<string> m_Alias;
02351
02359 RtcServiceAdmin m_ServiceAdmin;
02360
02361
02362 NamingPolicy m_NamingPolicy;
02363
02371 RtcMedLogbuf m_MedLogbuf;
02372
02380 RtcLogStream rtcout;
02381
02382 };
02383
02384
02385
02386
02387
02388 class EXPORTS RtcManager;
02396 typedef RtcBase* (*RtcNewFunc)(RtcManager* manager);
02397
02405 typedef void (*RtcDeleteFunc)(RtcBase* rtc);
02406
02407 };
02408
02409
02410 #endif // __RtcBase_h__