ConnectionManager.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  ConnectionManager.cpp
00003 
00004                         ConnectionManager.cpp -  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.cpp
00041  *
00042  *  Created on: Oct 9, 2009
00043  *      Author: kaltan
00044  */
00045 
00046 #include "ConnectionManager.hpp"
00047 #include <boost/bind.hpp>
00048 #include <boost/scoped_ptr.hpp>
00049 #include "../base/PortInterface.hpp"
00050 #include "../os/MutexLock.hpp"
00051 #include "../base/InputPortInterface.hpp"
00052 #include <cassert>
00053 
00054 namespace RTT
00055 {
00056     using namespace detail;
00057 
00058     namespace internal
00059     {
00060 
00061         ConnectionManager::ConnectionManager(PortInterface* port)
00062             : mport(port)
00063         {
00064         }
00065 
00066         ConnectionManager::~ConnectionManager()
00067         {
00068             this->disconnect();
00069         }
00070 
00075         void clearChannel(ConnectionManager::ChannelDescriptor& descriptor) {
00076             descriptor.get<1>()->clear();
00077         }
00078 
00079         void ConnectionManager::clear()
00080         { RTT::os::MutexLock lock(connection_lock);
00081             std::for_each(connections.begin(), connections.end(), &clearChannel);
00082         }
00083 
00084         bool ConnectionManager::findMatchingPort(ConnID const* conn_id, ChannelDescriptor const& descriptor)
00085         {
00086             return ( descriptor.get<0>() && conn_id->isSameID(*descriptor.get<0>()));
00087         }
00088 
00089         void ConnectionManager::updateCurrentChannel(bool reset_current)
00090         {
00091             if (connections.empty())
00092                 cur_channel = ChannelDescriptor();
00093             else if (reset_current)
00094                 cur_channel = connections.front();
00095         }
00096 
00097         bool ConnectionManager::disconnect(PortInterface* port)
00098         {
00099             boost::scoped_ptr<ConnID> conn_id( port->getPortID() );
00100             return this->removeConnection(conn_id.get());
00101         }
00102 
00103         bool ConnectionManager::eraseConnection(ConnectionManager::ChannelDescriptor& descriptor)
00104         {
00105             // disconnect needs to know if we're from Out->In (forward) or from In->Out
00106             bool is_forward = true;
00107             if ( dynamic_cast<InputPortInterface*>(mport) )
00108                 is_forward = false; // disconnect on input port = backward.
00109 
00110             descriptor.get<1>()->disconnect( is_forward );
00111             return true;
00112         }
00113 
00114         void ConnectionManager::disconnect()
00115         {
00116             std::list<ChannelDescriptor> all_connections;
00117             { RTT::os::MutexLock lock(connection_lock);
00118                 all_connections.splice(all_connections.end(), connections);
00119                 cur_channel = ChannelDescriptor();
00120             }
00121             std::for_each(all_connections.begin(), all_connections.end(),
00122                     boost::bind(&ConnectionManager::eraseConnection, this, _1));
00123         }
00124 
00125         bool ConnectionManager::connected() const
00126         { return !connections.empty(); }
00127 
00128 
00129         void ConnectionManager::addConnection(ConnID* conn_id, ChannelElementBase::shared_ptr channel, ConnPolicy policy)
00130         { RTT::os::MutexLock lock(connection_lock);
00131             assert(conn_id);
00132             ChannelDescriptor descriptor = boost::make_tuple(conn_id, channel, policy);
00133             if (connections.empty())
00134                 cur_channel = descriptor;
00135             connections.push_back(descriptor);
00136         }
00137 
00138         bool ConnectionManager::removeConnection(ConnID* conn_id)
00139         {
00140             ChannelDescriptor descriptor;
00141             { RTT::os::MutexLock lock(connection_lock);
00142                 std::list<ChannelDescriptor>::iterator conn_it =
00143                     std::find_if(connections.begin(), connections.end(), boost::bind(&ConnectionManager::findMatchingPort, this, conn_id, _1));
00144                 if (conn_it == connections.end())
00145                     return false;
00146                 descriptor = *conn_it;
00147                 connections.erase(conn_it);
00148                 updateCurrentChannel( cur_channel.get<1>() == descriptor.get<1>() );
00149             }
00150 
00151             // disconnect needs to know if we're from Out->In (forward) or from In->Out
00152             bool is_forward = true;
00153             if ( dynamic_cast<InputPortInterface*>(mport) )
00154                 is_forward = false; // disconnect on input port = backward.
00155 
00156             descriptor.get<1>()->disconnect(is_forward);
00157             return true;
00158         }
00159 
00160         bool is_same_id(ConnID* conn_id, ConnectionManager::ChannelDescriptor const& channel)
00161         {
00162             return conn_id->isSameID( *channel.get<0>() );
00163         }
00164 
00165     }
00166 
00167 }


rtt
Author(s): RTT Developers
autogenerated on Thu Jan 2 2014 11:35:19