00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef ORO_CORBA_DISPATCHER_HPP
00040 #define ORO_CORBA_DISPATCHER_HPP
00041
00042 #include "../../os/MutexLock.hpp"
00043 #include "../../Activity.hpp"
00044 #include "../../base/ChannelElementBase.hpp"
00045 #include "../../Logger.hpp"
00046 #include "../../internal/List.hpp"
00047 #include "DataFlowI.h"
00048 #include "../../DataFlowInterface.hpp"
00049 #include "../../TaskContext.hpp"
00050
00051 namespace RTT {
00052 namespace corba {
00057 class CorbaDispatcher : public Activity
00058 {
00059 typedef std::map<DataFlowInterface*,CorbaDispatcher*> DispatchMap;
00060 RTT_CORBA_API static DispatchMap DispatchI;
00061
00062 typedef internal::List<base::ChannelElementBase::shared_ptr> RCList;
00063 RCList RClist;
00064
00065 bool do_exit;
00066
00067 RTT_CORBA_API static os::Mutex* mlock;
00068
00069 RTT_CORBA_API static int defaultScheduler;
00070 RTT_CORBA_API static int defaultPriority;
00071
00072 CorbaDispatcher( const std::string& name)
00073 : Activity(defaultScheduler, defaultPriority, 0.0, 0, name),
00074 RClist(20,2),
00075 do_exit(false)
00076 {}
00077
00078 CorbaDispatcher( const std::string& name, int scheduler, int priority)
00079 : Activity(scheduler, priority, 0.0, 0, name),
00080 RClist(20,2),
00081 do_exit(false)
00082 {}
00083
00084 ~CorbaDispatcher() {
00085 this->stop();
00086 }
00087
00088 public:
00097 static CorbaDispatcher* Instance(DataFlowInterface* iface, int scheduler = defaultScheduler, int priority = defaultPriority) {
00098 if (!mlock)
00099 mlock = new os::Mutex();
00100 DispatchMap::iterator result = DispatchI.find(iface);
00101 if ( result == DispatchI.end() ) {
00102 os::MutexLock lock(*mlock);
00103
00104 result = DispatchI.find(iface);
00105 if ( result != DispatchI.end() )
00106 return result->second;
00107
00108 std::string name;
00109 if ( iface == 0 || iface->getOwner() == 0)
00110 name = "Global";
00111 else
00112 name = iface->getOwner()->getName();
00113 name += ".CorbaDispatch";
00114 DispatchI[iface] = new CorbaDispatcher( name, scheduler, priority );
00115 DispatchI[iface]->start();
00116 return DispatchI[iface];
00117 }
00118 return result->second;
00119 }
00120
00125 static void Release(DataFlowInterface* iface) {
00126 DispatchMap::iterator result = DispatchI.find(iface);
00127 if ( result != DispatchI.end() ) {
00128 os::MutexLock lock(*mlock);
00129 delete result->second;
00130 DispatchI.erase(result);
00131 }
00132 if ( DispatchI.empty() )
00133 delete mlock;
00134 mlock = 0;
00135 }
00136
00140 static void ReleaseAll() {
00141 DispatchMap::iterator result = DispatchI.begin();
00142 while ( result != DispatchI.end() ) {
00143 delete result->second;
00144 DispatchI.erase(result);
00145 result = DispatchI.begin();
00146 }
00147 delete mlock;
00148 mlock = 0;
00149 }
00150
00151 void dispatchChannel( base::ChannelElementBase::shared_ptr chan ) {
00152 RClist.append( chan );
00153 this->trigger();
00154 }
00155
00156 void cancelChannel( base::ChannelElementBase::shared_ptr chan ) {
00157 RClist.erase( chan );
00158 }
00159
00160 bool initialize() {
00161 log(Info) <<"Started " << this->getName() << "." <<endlog();
00162 do_exit = false;
00163 return true;
00164 }
00165
00166 void loop() {
00167 while ( !RClist.empty() && !do_exit) {
00168 base::ChannelElementBase::shared_ptr chan = RClist.front();
00169 CRemoteChannelElement_i* rbase = dynamic_cast<CRemoteChannelElement_i*>(chan.get());
00170 if (rbase)
00171 rbase->transferSamples();
00172 RClist.erase( chan );
00173 }
00174 }
00175
00176 bool breakLoop() {
00177 do_exit = true;
00178 return true;
00179 }
00180 };
00181 }
00182 }
00183 #endif