Orocos Real-Time Toolkit  2.8.3
ConnectionManager.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 ConnectionManager.hpp
3 
4  ConnectionManager.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 /*
40  * ConnectionManager.hpp
41  *
42  * Created on: Oct 9, 2009
43  * Author: kaltan
44  */
45 
46 #ifndef CONNECTIONMANAGER_HPP_
47 #define CONNECTIONMANAGER_HPP_
48 
49 
50 #include "ConnID.hpp"
51 #include "List.hpp"
52 #include "../ConnPolicy.hpp"
53 #include "../os/Mutex.hpp"
54 #include "../base/rtt-base-fwd.hpp"
55 #include "../base/ChannelElementBase.hpp"
56 #include <boost/tuple/tuple.hpp>
57 #include <boost/bind.hpp>
58 #include <boost/shared_ptr.hpp>
59 
60 #include <rtt/os/Mutex.hpp>
61 #include <rtt/os/MutexLock.hpp>
62 #include <list>
63 
64 
65 namespace RTT
66 {
67 
68  namespace internal
69  {
77  {
78  public:
85  typedef boost::tuple<boost::shared_ptr<ConnID>, base::ChannelElementBase::shared_ptr, ConnPolicy> ChannelDescriptor;
86 
94 
100  void addConnection(ConnID* port_id, base::ChannelElementBase::shared_ptr channel_input, ConnPolicy policy);
101 
102  bool removeConnection(ConnID* port_id);
103 
107  void disconnect();
108 
112  bool connected() const;
113 
115  bool disconnect(base::PortInterface* port);
116 
117  template<typename Pred>
118  bool delete_if(Pred pred) {
119  RTT::os::MutexLock lock(connection_lock);
120  bool result = false;
121  std::list<ChannelDescriptor>::iterator it = connections.begin();
122  while (it != connections.end())
123  {
124  if (pred(*it))
125  {
126  result = true;
127  it = connections.erase(it);
128  }
129  else ++it;
130  }
131  return result;
132  }
133 
142  template<typename Pred>
143  void select_reader_channel(Pred pred, bool copy_old_data) {
144  RTT::os::MutexLock lock(connection_lock);
145  ChannelDescriptor *new_channel =
146  find_if(pred, copy_old_data);
147  if (new_channel)
148  {
149  // We don't clear the current channel (to get it to NoData state), because there is a race
150  // between find_if and this line. We have to accept (in other parts of the code) that eventually,
151  // all channels return 'OldData'.
152  cur_channel = new_channel;
153  }
154  }
155 
156  template<typename Pred>
157  ChannelDescriptor *find_if(Pred pred, bool copy_old_data) {
158  // We only copy OldData in the initial read of the current channel.
159  // if it has no new data, the search over the other channels starts,
160  // but no old data is needed.
161  ChannelDescriptor *channel = cur_channel;
162  if ( channel )
163  if ( pred( copy_old_data, *channel ) )
164  return channel;
165 
166  std::list<ChannelDescriptor>::iterator result;
167  for (result = connections.begin(); result != connections.end(); ++result) {
168  if (cur_channel && (result->get<1>() == cur_channel->get<1>())) continue;
169  if ( pred(false, *result) == true)
170  return &(*result);
171  }
172  return NULL;
173  }
174 
179  bool isSingleConnection() const { return connections.size() == 1; }
180 
187  return cur_channel ? cur_channel->get<1>().get() : NULL;
188  }
189 
193  std::list<ChannelDescriptor> getChannels() const {
194  return connections;
195  }
196 
202  void clear();
203 
207  void lock() const {
208  connection_lock.lock();
209  };
210 
214  void unlock() const {
215  connection_lock.unlock();
216  }
217  protected:
218 
219  void updateCurrentChannel(bool reset_current);
220 
229  bool findMatchingPort(ConnID const* conn_id, ChannelDescriptor const& descriptor);
230 
235  bool eraseConnection(ChannelDescriptor& descriptor);
236 
239 
244 
249  std::list< ChannelDescriptor > connections;
250 
254  ChannelDescriptor *cur_channel;
255 
261  };
262 
263  }
264 
265 }
266 
267 #endif /* CONNECTIONMANAGER_HPP_ */
We can&#39;t use typedefs since C++ doesn&#39;t allow it for templated classes without specifying all the tem...
std::list< ChannelDescriptor > getChannels() const
Returns a list of all channels managed by this object.
base::ChannelElementBase * getCurrentChannel() const
Returns the first added channel or if select_if was called, the selected channel. ...
ChannelDescriptor * cur_channel
Optimisation in case only one channel is to be managed.
Manages connections between ports.
#define RTT_API
Definition: rtt-config.h:97
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:92
void unlock() const
Unlocks the mutex protecting the channel element list.
void lock() const
Locks the mutex protecting the channel element list.
base::PortInterface * mport
The port for which we manage connections.
os::Mutex connection_resize_mtx
os::Mutex for when it is needed to resize the connections list
boost::tuple< boost::shared_ptr< ConnID >, base::ChannelElementBase::shared_ptr, ConnPolicy > ChannelDescriptor
A Channel (= connection) is described by an opaque ConnID object, the first element of the channel an...
boost::intrusive_ptr< ChannelElementBase > shared_ptr
RTT::os::Mutex connection_lock
Lock that should be taken before the list of connections is accessed or modified. ...
bool isSingleConnection() const
Returns true if this manager manages only one connection.
void select_reader_channel(Pred pred, bool copy_old_data)
Selects a connection as the current channel if pred(connection) is true.
This class is used in places where a permanent representation of a reference to a connection is neede...
Definition: ConnID.hpp:58
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:82
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
std::list< ChannelDescriptor > connections
A list of all our connections.
In the data flow implementation, a channel is created by chaining ChannelElementBase objects...
ChannelDescriptor * find_if(Pred pred, bool copy_old_data)
The base class of every data flow port.
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51