Orocos Real-Time Toolkit  2.9.0
CorbaDispatcher.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 CorbaDispatcher.hpp
3 
4  CorbaDispatcher.hpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_CORBA_DISPATCHER_HPP
40 #define ORO_CORBA_DISPATCHER_HPP
41 
42 #include "../../os/MutexLock.hpp"
43 #include "../../Activity.hpp"
44 #include "../../base/ChannelElementBase.hpp"
45 #include "../../Logger.hpp"
46 #include "../../internal/List.hpp"
47 #include "DataFlowI.h"
48 #include "../../DataFlowInterface.hpp"
49 #include "../../TaskContext.hpp"
50 
51 namespace RTT {
52  namespace corba {
57  class CorbaDispatcher : public Activity
58  {
59  typedef std::map<DataFlowInterface*,CorbaDispatcher*> DispatchMap;
60  RTT_CORBA_API static DispatchMap DispatchI;
61 
63  RCList RClist;
64 
65  bool do_exit;
66 
67  RTT_CORBA_API static os::Mutex* mlock;
68 
69  RTT_CORBA_API static int defaultScheduler;
70  RTT_CORBA_API static int defaultPriority;
71 
72  CorbaDispatcher( const std::string& name)
73  : Activity(defaultScheduler, defaultPriority, 0.0, 0, name),
74  RClist(20,2),
75  do_exit(false)
76  {}
77 
78  CorbaDispatcher( const std::string& name, int scheduler, int priority)
79  : Activity(scheduler, priority, 0.0, 0, name),
80  RClist(20,2),
81  do_exit(false)
82  {}
83 
84  ~CorbaDispatcher() {
85  this->stop();
86  }
87 
88  public:
97  static CorbaDispatcher* Instance(DataFlowInterface* iface, int scheduler = defaultScheduler, int priority = defaultPriority) {
98  if (!mlock)
99  mlock = new os::Mutex();
100  DispatchMap::iterator result = DispatchI.find(iface);
101  if ( result == DispatchI.end() ) {
102  os::MutexLock lock(*mlock);
103  // re-try to find (avoid race):
104  result = DispatchI.find(iface);
105  if ( result != DispatchI.end() )
106  return result->second;
107  // *really* not found, let's create it.
108  std::string name;
109  if ( iface == 0 || iface->getOwner() == 0)
110  name = "Global";
111  else
112  name = iface->getOwner()->getName();
113  name += "Corba";
114  DispatchI[iface] = new CorbaDispatcher( name, scheduler, priority );
115  DispatchI[iface]->start();
116  return DispatchI[iface];
117  }
118  return result->second;
119  }
120 
125  static void Release(DataFlowInterface* iface) {
126  DispatchMap::iterator result = DispatchI.find(iface);
127  if ( result != DispatchI.end() ) {
128  os::MutexLock lock(*mlock);
129  delete result->second;
130  DispatchI.erase(result);
131  }
132  if ( DispatchI.empty() )
133  delete mlock;
134  mlock = 0;
135  }
136 
140  static void ReleaseAll() {
141  DispatchMap::iterator result = DispatchI.begin();
142  while ( result != DispatchI.end() ) {
143  delete result->second;
144  DispatchI.erase(result);
145  result = DispatchI.begin();
146  }
147  delete mlock;
148  mlock = 0;
149  }
150 
152  {
153  result = result || (c0 == c1);
154  }
155 
157  bool has_element = false;
158  RClist.apply(boost::bind(&CorbaDispatcher::hasElement, _1, chan, boost::ref(has_element)));
159  if (!has_element)
160  RClist.append( chan );
161  this->trigger();
162  }
163 
165  RClist.erase( chan );
166  }
167 
168  bool initialize() {
169  log(Info) <<"Started " << this->getName() << "." <<endlog();
170  do_exit = false;
171  return true;
172  }
173 
174  void loop() {
175  while ( !RClist.empty() && !do_exit) {
177  CRemoteChannelElement_i* rbase = dynamic_cast<CRemoteChannelElement_i*>(chan.get());
178  if (rbase)
179  rbase->transferSamples();
180  RClist.erase( chan );
181  }
182  }
183 
184  bool breakLoop() {
185  do_exit = true;
186  return true;
187  }
188  };
189  }
190 }
191 #endif
value_t front() const
Returns the first element of the list.
virtual bool stop()
Stop the activity This will stop the activity by removing it from the &#39;run-queue&#39; of a thread or call...
Definition: Activity.cpp:278
The Interface of a TaskContext which exposes its data-flow ports.
bool empty() const
Returns true if this list is empty.
Activity(base::RunnableInterface *r=0, const std::string &name="Activity")
Create a not real-time Activity.
Definition: Activity.cpp:56
void cancelChannel(base::ChannelElementBase::shared_ptr chan)
Base class for CORBA channel servers.
Definition: DataFlowI.h:69
static void hasElement(base::ChannelElementBase::shared_ptr c0, base::ChannelElementBase::shared_ptr c1, bool &result)
void apply(Function func)
Apply a function to the elements of the whole list.
void dispatchChannel(base::ChannelElementBase::shared_ptr chan)
static void ReleaseAll()
May be called during program termination to clean up all resources.
static CorbaDispatcher * Instance(DataFlowInterface *iface, int scheduler=defaultScheduler, int priority=defaultPriority)
Create a new dispatcher for a given data flow interface.
boost::intrusive_ptr< ChannelElementBase > shared_ptr
An Activity executes a RunnableInterface object in a (periodic) thread.
Definition: Activity.hpp:70
This object sends over data flow messages from local buffers to a remote channel element.
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
static void Release(DataFlowInterface *iface)
Releases and cleans up a specific interface from dispatching.
virtual bool trigger()
Trigger that work has to be done.
Definition: Activity.cpp:146
bool append(value_t item)
Append a single value to the list.
bool erase(value_t item)
Erase a value from the list.
virtual const char * getName() const
Read the name of this task.
Definition: Thread.cpp:633
virtual const std::string & getName() const
Returns the name of this TaskContext.
TaskContext * getOwner() const
Returns the component this interface belongs to.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51