00001
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef RtcBase_h
00028 #define RtcBase_h
00029
00030
00031 #include "rtm/RTC.h"
00032
00033
00034 #include <list>
00035 #include <string>
00036 #include <algorithm>
00037 #include <functional>
00038
00039
00040 #include <ace/Task.h>
00041
00042
00043 #include "rtm/idl/RTCBaseSkel.h"
00044 #include "rtm/idl/RTCBaseStub.h"
00045 #include "rtm/RtcOutPort.h"
00046 #include "rtm/RtcModuleProfile.h"
00047 #include "rtm/RtcSystemLogger.h"
00048
00049
00050 #define USLEEP(x) usleep(x);
00051
00052 namespace RTM {
00053
00054 using namespace std;
00055
00056
00057 class InPortBase;
00058 class OutPortBase;
00059 class RtcManager;
00060
00061 static char* StateString[] =
00062 {
00063 "RTC_UNKNOWN",
00064 "RTC_BORN",
00065 "RTC_INITIALIZING",
00066 "RTC_READY",
00067 "RTC_STARTING",
00068 "RTC_ACTIVE",
00069 "RTC_STOPPING",
00070 "RTC_ABORTING",
00071 "RTC_ERROR",
00072 "RTC_FATAL_ERROR",
00073 "RTC_EXITING",
00074 "UNKNOWN"
00075 };
00076
00084 typedef RTM::RTComponent::ComponentState ComponentState;
00085
00086 const ComponentState RTC_UNKNOWN = RTM::RTComponent::RTC_UNKNOWN;
00087 const ComponentState RTC_BORN = RTM::RTComponent::RTC_BORN;
00088 const ComponentState RTC_INITIALIZING = RTM::RTComponent::RTC_INITIALIZING;
00089 const ComponentState RTC_READY = RTM::RTComponent::RTC_READY;
00090 const ComponentState RTC_STARTING = RTM::RTComponent::RTC_STARTING;
00091 const ComponentState RTC_ACTIVE = RTM::RTComponent::RTC_ACTIVE;
00092 const ComponentState RTC_STOPPING = RTM::RTComponent::RTC_STOPPING;
00093 const ComponentState RTC_ABORTING = RTM::RTComponent::RTC_ABORTING;
00094 const ComponentState RTC_ERROR = RTM::RTComponent::RTC_ERROR;
00095 const ComponentState RTC_FATAL_ERROR = RTM::RTComponent::RTC_FATAL_ERROR;
00096 const ComponentState RTC_EXITING = RTM::RTComponent::RTC_EXITING;
00097
00098
00099 typedef enum NamingPolicy {
00100 UNKNOWN = 0,
00101 LONGNAME_ENABLE = 1,
00102 ALIAS_ENABLE = 2
00103 };
00104
00152 class RtcBase
00153 : public virtual POA_RTM::RTCBase,
00154 public virtual PortableServer::RefCountServantBase,
00155 public ACE_Task<ACE_MT_SYNCH>
00156 {
00157 public:
00158
00178 RtcBase();
00179
00204 RtcBase(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
00205
00223 RtcBase(RtcManager* manager);
00224
00236 virtual ~RtcBase();
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
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
00294 virtual RtmRes rtc_start()
00295 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00296
00312 virtual RtmRes rtc_stop()
00313 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00314
00331 virtual RtmRes rtc_reset()
00332 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00333
00348 virtual RtmRes rtc_exit()
00349 throw (CORBA::SystemException, RTM::RTComponent::IllegalTransition);
00350
00365 virtual RtmRes rtc_kill();
00366
00367
00368
00369
00389 virtual RtmRes rtc_worker();
00390
00391
00392
00393
00394
00395
00411 virtual RtmRes rtc_ready_entry()
00412 {USLEEP(1000); return RTM_OK;};
00413
00428 virtual RtmRes rtc_ready_do()
00429 {USLEEP(1000); return RTM_OK;};
00430
00445 virtual RtmRes rtc_ready_exit()
00446 {USLEEP(1000); return RTM_OK;};
00447
00448
00449
00450
00451
00466 virtual RtmRes rtc_active_entry()
00467 {USLEEP(1000); return RTM_OK;};
00468
00483 virtual RtmRes rtc_active_do()
00484 {USLEEP(1000); return RTM_OK;};
00485
00500 virtual RtmRes rtc_active_exit()
00501 {USLEEP(1000); return RTM_OK;};
00502
00503
00504
00505
00506
00521 virtual RtmRes rtc_error_entry()
00522 {USLEEP(1000); return RTM_OK;};
00523
00538 virtual RtmRes rtc_error_do()
00539 {USLEEP(1000); return RTM_OK;};
00540
00555 virtual RtmRes rtc_error_exit()
00556 {USLEEP(1000); return RTM_OK;};
00557
00558
00559
00560
00561
00577 virtual RtmRes rtc_fatal_entry()
00578 {USLEEP(1000); return RTM_OK;};
00579
00595 virtual RtmRes rtc_fatal_do()
00596 {USLEEP(1000); return RTM_OK;};
00597
00613 virtual RtmRes rtc_fatal_exit()
00614 {USLEEP(1000); return RTM_OK;};
00615
00616
00617
00635 virtual RtmRes rtc_init_entry()
00636 {USLEEP(1000); return RTM_OK;};
00637
00655 virtual RtmRes rtc_starting_entry()
00656 {USLEEP(1000); return RTM_OK;};
00657
00675 virtual RtmRes rtc_stopping_entry()
00676 {USLEEP(1000); return RTM_OK;};
00677
00695 virtual RtmRes rtc_aborting_entry()
00696 {USLEEP(1000); return RTM_OK;};
00697
00715 virtual RtmRes rtc_exiting_entry()
00716 {USLEEP(1000); return RTM_OK;};
00717
00718
00719
00720
00721
00734 virtual OutPort_ptr rtc_state();
00735
00736
00737
00738
00751 virtual InPortList* inports() ;
00752
00769 virtual InPort_ptr get_inport(const char* name)
00770 throw (CORBA::SystemException, RTM::RTComponent::NoSuchName);
00771
00784 virtual OutPortList* outports() ;
00785
00802 virtual OutPort_ptr get_outport(const char* name)
00803 throw (CORBA::SystemException, RTM::RTComponent::NoSuchName);
00804
00805
00806
00807
00808
00809
00810
00811
00812
00825 virtual char* instance_id();
00826
00839 virtual char* implementation_id();
00840
00853 virtual char* description();
00854
00867 virtual char* version();
00868
00881 virtual char* maker();
00882
00895 virtual char* category();
00896
00909 virtual RTCProfile* profile() ;
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00933 virtual RtmRes rtc_start_thread();
00934
00947 virtual RtmRes rtc_suspend_thread();
00948
00961 virtual RtmRes rtc_stop_thread();
00962
00975 virtual RtmRes rtc_set_parent(RTCBase_ptr comp);
00976
00990 virtual RtmRes rtc_add_component(RTCBase_ptr comp)
00991 {return RTM_ERR;};
00992
01006 virtual RtmRes rtc_delete_component(RTCBase_ptr comp)
01007 {return RTM_ERR;} ;
01008
01023 virtual RtmRes rtc_replace_component(RTCBase_ptr comp1, RTCBase_ptr comp2)
01024 {return RTM_ERR;};
01025
01039 virtual RtmRes rtc_replace_component_by_name(const char* name1, const char* name2)
01040 {return RTM_ERR;};
01041
01055 virtual RTCBaseList* rtc_components()
01056 {
01057 RTCBaseList_var myself = new RTCBaseList();
01058 myself->length(1);
01059
01060 #ifdef ORB_IS_TAO
01061 (*myself)[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
01062 #else
01063 myself[0] = RTM::RTCBase::_narrow(m_pPOA->servant_to_reference(this));
01064 #endif
01065 return myself._retn();
01066 };
01067
01081 virtual RTCBase_ptr rtc_get_component(const char* name)
01082 {
01083 return dynamic_cast<RTCBase_ptr>(m_pPOA->servant_to_reference(this));
01084 };
01085
01099 virtual RtmRes rtc_attach_inport(InPort_ptr in_port)
01100 {return RTM_ERR;};
01101
01116 virtual RtmRes rtc_attach_inport_by_name(RTCBase_ptr comp, const char* name)
01117 {return RTM_ERR;};
01118
01119
01133 virtual RtmRes rtc_detatch_inport(InPort_ptr in_port)
01134 {return RTM_ERR;};
01135
01150 virtual RtmRes rtc_detatch_inport_by_name(const char* name)
01151 {return RTM_ERR;};
01152
01166 virtual RtmRes rtc_attach_outport(OutPort_ptr out_port)
01167 {return RTM_ERR;} ;
01168
01183 virtual RtmRes rtc_attach_outport_by_name(RTCBase_ptr comp,
01184 const char* name)
01185 {return RTM_ERR;};
01186
01200 virtual RtmRes rtc_detatch_outport(OutPort_ptr out_port)
01201 {return RTM_ERR;}
01202
01217 virtual RtmRes rtc_detatch_outport_by_name(const char* name)
01218 {return RTM_ERR;};
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01249 virtual void init_orb(CORBA::ORB_ptr orb, PortableServer::POA_ptr poa);
01250
01270 virtual int open(void *args);
01271
01288 virtual int svc(void);
01289
01290
01309 virtual int close(unsigned long flags);
01310
01324 virtual RTM::RTComponent::ComponentState getState();
01325
01326
01327
01328
01329
01330
01349 virtual void initModuleProfile(RtcModuleProfile prof);
01350
01366 virtual RtcModuleProfile& getModuleProfile()
01367 {
01368 return m_Profile;
01369 }
01370
01371
01372
01373
01374
01375
01376
01377
01378
01379
01380
01381
01405 virtual string setComponentName(int num);
01406
01407
01408
01409
01432 bool registerInPort(InPortBase& in_ch);
01433
01456 bool registerPort(InPortBase& in_ch)
01457 {
01458 return registerInPort(in_ch);
01459 };
01460
01481 bool deleteInPort(InPortBase& in_ch);
01482
01503 bool deletePort(InPortBase& in_ch)
01504 {
01505 return deleteInPort(in_ch);
01506 };
01507
01528 bool deleteInPortByName(const char* ch_name);
01529
01547 void readAllInPorts();
01548
01549
01565 void finalizeInPorts();
01566
01567
01568
01569
01592 bool registerOutPort(OutPortBase& out_ch);
01593 bool registerPort(OutPortBase& out_ch)
01594 {
01595 return registerOutPort(out_ch);
01596 };
01597
01619 bool deleteOutPort(OutPortBase& out_ch);
01620
01642 bool deletePort(OutPortBase& out_ch)
01643 {
01644 return deleteOutPort(out_ch);
01645 };
01646
01667 bool deleteOutPortByName(const char* ch_name);
01668
01686 void writeAllOutPorts();
01687
01688
01705 void finalizeOutPorts();
01706
01707
01738 void appendAlias(const char* alias);
01739
01770 void appendAlias(const std::string alias);
01771
01785 std::list<string> getAliases();
01786
01787 void setNamingPolicy(NamingPolicy policy);
01788 NamingPolicy getNamingPolicy();
01789 bool isLongNameEnable();
01790 bool isAliasEnable();
01791
01792 void forceExit();
01793
01809 void finalize();
01810
01811 bool isThreadRunning();
01812
01813
01814
01815
01816 protected:
01824 CORBA::ORB_ptr m_pORB;
01825
01833 PortableServer::POA_ptr m_pPOA;
01834
01842 RtcManager* m_pManager;
01843
01851 RTCBase_var m_Parent;
01852
01860 typedef enum ThreadStates {
01861 UNKNOWN,
01862 RUNNING,
01863 SUSPEND,
01864 STOP
01865 };
01866 class ThreadState
01867 {
01868 public:
01869 ThreadState() : m_Flag(RUNNING){};
01870 ThreadStates m_Flag;
01871 ACE_Thread_Mutex m_Mutex;
01872 };
01880 ThreadState m_ThreadState;
01881
01889 class eq_name
01890 {
01891 public:
01892 const string m_name;
01893 eq_name(const char* name) : m_name(name){};
01894 bool operator()(InPortBase* ch)
01895 {
01896 string ch_name(ch->name());
01897 return m_name == ch_name;
01898 };
01899 bool operator()(OutPortBase* ch)
01900 {
01901 string ch_name(ch->name());
01902 return m_name == ch_name;
01903 };
01904 };
01905
01913 struct ComponentStateMtx {
01914 public:
01922 ComponentStateMtx():_state(RTM::RTComponent::RTC_BORN){};
01923
01931 ComponentState _state;
01932
01940 ACE_Thread_Mutex _mutex;
01941 };
01942
01950 ComponentStateMtx m_CurrentState;
01951
01959 ComponentStateMtx m_NextState;
01960
01968 typedef RtmRes (RtcBase::*StateFunc)();
01969
01970
01971
01972
01980 StateFunc _exit_func[11];
01981
01989 StateFunc _entry_func[11];
01990
01998 StateFunc _do_func[11];
01999
02000
02001
02002
02003
02011 RtmRes _check_error(RtmRes result);
02012
02020 RtmRes _nop(){return RTM_OK;}
02021
02029 RtmRes _rtc_initializing();
02030
02038 RtmRes _rtc_ready_entry();
02046 RtmRes _rtc_starting();
02054 RtmRes _rtc_active_entry();
02062 RtmRes _rtc_stopping();
02070 RtmRes _rtc_aborting();
02071
02079 RtmRes _rtc_error_entry();
02080
02088 RtmRes _rtc_fatal_entry();
02089
02097 RtmRes _rtc_exiting();
02098
02106 RtmRes _rtc_ready();
02107
02115 RtmRes _rtc_active();
02116
02124 RtmRes _rtc_error();
02125
02133 RtmRes _rtc_fatal();
02134
02142 RtmRes _rtc_ready_exit();
02143
02151 RtmRes _rtc_active_exit();
02152
02160 RtmRes _rtc_error_exit();
02161
02169 RtmRes _rtc_fatal_exit();
02170
02178 void init_state_func_table();
02179
02180
02188 typedef list<InPortBase*>::iterator InPorts_it;
02189
02197 typedef list<OutPortBase*>::iterator OutPorts_it;
02198
02206 struct InPorts
02207 {
02208 list<InPortBase*> m_List;
02209 ACE_Thread_Mutex m_Mutex;
02210 };
02218 InPorts m_InPorts;
02219
02227 struct OutPorts
02228 {
02229 list<OutPortBase*> m_List;
02230 ACE_Thread_Mutex m_Mutex;
02231 };
02239 OutPorts m_OutPorts;
02240
02242
02243
02251 TimedState m_TimedState;
02259 OutPortAny<TimedState> m_StatePort;
02260
02268 RtcModuleProfile m_Profile;
02269
02270
02271
02272
02280 std::list<string> m_Alias;
02281
02282 NamingPolicy m_NamingPolicy;
02283
02291 RtcMedLogbuf m_MedLogbuf;
02292
02300 RtcLogStream rtcout;
02301
02302 };
02303
02304
02305
02306
02307
02308 class RtcManager;
02316 typedef RtcBase* (*RtcNewFunc)(RtcManager* manager);
02317
02325 typedef void (*RtcDeleteFunc)(RtcBase* rtc);
02326
02327 };
02328
02329
02330 #endif // __RtcBase_h__