Go to the documentation of this file.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
00040
00041
00042
00043
00044
00045
00046 #ifndef CONNECTIONMANAGER_HPP_
00047 #define CONNECTIONMANAGER_HPP_
00048
00049
00050 #include "ConnID.hpp"
00051 #include "List.hpp"
00052 #include "../ConnPolicy.hpp"
00053 #include "../os/Mutex.hpp"
00054 #include "../base/rtt-base-fwd.hpp"
00055 #include "../base/ChannelElementBase.hpp"
00056 #include <boost/tuple/tuple.hpp>
00057 #include <boost/bind.hpp>
00058 #include <boost/shared_ptr.hpp>
00059 #ifndef USE_CPP11
00060 #include <boost/lambda/lambda.hpp>
00061 #endif
00062
00063 #include <rtt/os/Mutex.hpp>
00064 #include <rtt/os/MutexLock.hpp>
00065 #include <list>
00066
00067
00068 namespace RTT
00069 {
00070
00071 namespace internal
00072 {
00079 class RTT_API ConnectionManager
00080 {
00081 public:
00088 typedef boost::tuple<boost::shared_ptr<ConnID>, base::ChannelElementBase::shared_ptr, ConnPolicy> ChannelDescriptor;
00089
00095 ConnectionManager(base::PortInterface* port);
00096 ~ConnectionManager();
00097
00103 void addConnection(ConnID* port_id, base::ChannelElementBase::shared_ptr channel_input, ConnPolicy policy);
00104
00105 bool removeConnection(ConnID* port_id);
00106
00110 void disconnect();
00111
00115 bool connected() const;
00116
00118 bool disconnect(base::PortInterface* port);
00119
00120 template<typename Pred>
00121 bool delete_if(Pred pred) {
00122 RTT::os::MutexLock lock(connection_lock);
00123 bool result = false;
00124 std::list<ChannelDescriptor>::iterator it = connections.begin();
00125 while (it != connections.end())
00126 {
00127 if (pred(*it))
00128 {
00129 result = true;
00130 it = connections.erase(it);
00131 }
00132 else ++it;
00133 }
00134 return result;
00135 }
00136
00145 template<typename Pred>
00146 void select_reader_channel(Pred pred, bool copy_old_data) {
00147 RTT::os::MutexLock lock(connection_lock);
00148 std::pair<bool, ChannelDescriptor> new_channel =
00149 find_if(pred, copy_old_data);
00150 if (new_channel.first)
00151 {
00152
00153
00154
00155 cur_channel = new_channel.second;
00156 }
00157 }
00158
00159 template<typename Pred>
00160 std::pair<bool, ChannelDescriptor> find_if(Pred pred, bool copy_old_data) {
00161
00162
00163
00164 ChannelDescriptor channel = cur_channel;
00165 if ( channel.get<1>() )
00166 if ( pred( copy_old_data, channel ) )
00167 return std::make_pair(true, channel);
00168
00169 std::list<ChannelDescriptor>::iterator result;
00170 for (result = connections.begin(); result != connections.end(); ++result)
00171 if ( pred(false, *result) == true)
00172 return std::make_pair(true, *result);
00173 return std::make_pair(false, ChannelDescriptor());
00174 }
00175
00180 bool isSingleConnection() const { return connections.size() == 1; }
00181
00187 base::ChannelElementBase* getCurrentChannel() const {
00188 return cur_channel.get<1>().get();
00189 }
00190
00194 std::list<ChannelDescriptor> getChannels() const {
00195 return connections;
00196 }
00197
00203 void clear();
00204
00205 protected:
00206
00207 void updateCurrentChannel(bool reset_current);
00208
00217 bool findMatchingPort(ConnID const* conn_id, ChannelDescriptor const& descriptor);
00218
00223 bool eraseConnection(ChannelDescriptor& descriptor);
00224
00226 os::Mutex connection_resize_mtx;
00227
00231 base::PortInterface* mport;
00232
00237 std::list< ChannelDescriptor > connections;
00238
00242 ChannelDescriptor cur_channel;
00243
00248 RTT::os::Mutex connection_lock;
00249 };
00250
00251 }
00252
00253 }
00254
00255 #endif