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 #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
00106 bool is_forward = true;
00107 if ( dynamic_cast<InputPortInterface*>(mport) )
00108 is_forward = false;
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
00152 bool is_forward = true;
00153 if ( dynamic_cast<InputPortInterface*>(mport) )
00154 is_forward = false;
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 }