ConnectionManager.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  ConnectionManager.hpp
00003 
00004                         ConnectionManager.hpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Peter Soetens
00008     email                : peter@thesourcworks.com
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 /*
00040  * ConnectionManager.hpp
00041  *
00042  *  Created on: Oct 9, 2009
00043  *      Author: kaltan
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                     // We don't clear the current channel (to get it to NoData state), because there is a race
00153                     // between find_if and this line. We have to accept (in other parts of the code) that eventually,
00154                     // all channels return 'OldData'.
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                 // We only copy OldData in the initial read of the current channel.
00162                 // if it has no new data, the search over the other channels starts,
00163                 // but no old data is needed.
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 /* CONNECTIONMANAGER_HPP_ */


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:06