ConnectionManager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 ConnectionManager.cpp
3 
4  ConnectionManager.cpp - 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.cpp
41  *
42  * Created on: Oct 9, 2009
43  * Author: kaltan
44  */
45 
46 #include "ConnectionManager.hpp"
47 #include "PortConnectionLock.hpp"
48 #include <boost/bind.hpp>
49 #include <boost/scoped_ptr.hpp>
50 #include "../base/PortInterface.hpp"
51 #include "../os/MutexLock.hpp"
52 #include "../base/InputPortInterface.hpp"
53 #include "../Logger.hpp"
54 #include <cassert>
55 
56 namespace RTT
57 {
58  using namespace detail;
59 
60  namespace internal
61  {
62 
64  : mport(port)
65  {
66  }
67 
69  {
70  this->disconnect();
71  }
72 
74  {
75  boost::scoped_ptr<ConnID> conn_id( port->getPortID() );
76  return this->removeConnection(conn_id.get(), /* disconnect = */ true);
77  }
78 
79  ConnectionManager::Connections::iterator ConnectionManager::eraseConnection(const Connections::iterator& descriptor, bool disconnect)
80  {
81  base::ChannelElementBase::shared_ptr channel = descriptor->get<1>();
82 
83  // disconnect from a shared connection
84  if (channel == shared_connection) {
85  RTT::log(Debug) << "Port " << mport->getName() << " disconnected from shared connection " << shared_connection->getName() << RTT::endlog();
86  shared_connection.reset();
87  }
88  Connections::iterator next = connections.erase(descriptor); // invalidates descriptor
89 
90  if (disconnect) {
91  // disconnect needs to know if we're from Out->In (forward) or from In->Out
92  bool is_forward = true;
93  if ( dynamic_cast<InputPortInterface*>(mport) )
94  is_forward = false; // disconnect on input port = backward.
95 
96  mport->getEndpoint()->disconnect(channel, is_forward);
97  }
98 
99  return next;
100  }
101 
103  {
105  for(Connections::iterator conn_it = connections.begin(); conn_it != connections.end(); ) {
106  conn_it = eraseConnection(conn_it, true);
107  }
108  }
109 
111  {
113  return !connections.empty();
114  }
115 
117  {
119  boost::scoped_ptr<ConnID> conn_id( port->getPortID() );
120  for(Connections::const_iterator conn_it = connections.begin(); conn_it != connections.end(); ++conn_it ) {
121  if (conn_it->get<0>() && conn_id->isSameID(*conn_it->get<0>())) return true;
122  }
123  return false;
124  }
125 
127  {
129  assert(conn_id);
130 
131  // check if the new connection is a shared connection
132  {
133  SharedConnectionBase::shared_ptr is_shared_connection = boost::dynamic_pointer_cast<SharedConnectionBase>(channel);
134  if (is_shared_connection) shared_connection.swap(is_shared_connection);
135  }
136 
137  // add ChannelDescriptor to the connections list
138  ChannelDescriptor descriptor = boost::make_tuple(conn_id, channel, policy);
139  connections.push_back(descriptor);
140 
141  return true;
142  }
143 
144  bool ConnectionManager::removeConnection(ConnID* conn_id, bool disconnect /* = true */)
145  {
147  bool found = false;
148  for(Connections::iterator conn_it = connections.begin(); conn_it != connections.end(); ) {
149  if (conn_it->get<0>() && conn_id->isSameID(*conn_it->get<0>())) {
150  conn_it = eraseConnection(conn_it, disconnect);
151  found = true;
152  } else {
153  conn_it++;
154  }
155  }
156  return found;
157  }
158 
160  {
162  bool found = false;
163  for(Connections::iterator conn_it = connections.begin(); conn_it != connections.end(); ) {
164  if (conn_it->get<1>() && channel == conn_it->get<1>()) {
165  conn_it = eraseConnection(conn_it, disconnect);
166  found = true;
167  } else {
168  conn_it++;
169  }
170  }
171  return found;
172  }
173 
174  }
175 
176 }
virtual bool isSameID(ConnID const &id) const =0
Connections::iterator eraseConnection(const Connections::iterator &descriptor, bool disconnect)
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
bool removeConnection(ConnID *port_id, bool disconnect=true)
bool connectedTo(base::PortInterface *port)
const std::string & getName() const
internal::SharedConnectionBase::shared_ptr shared_connection
virtual ChannelElementBase * getEndpoint() const =0
ConnectionManager(base::PortInterface *port)
virtual void disconnect(bool forward)
boost::tuple< boost::shared_ptr< ConnID >, base::ChannelElementBase::shared_ptr, ConnPolicy > ChannelDescriptor
boost::intrusive_ptr< ChannelElementBase > shared_ptr
bool addConnection(ConnID *port_id, base::ChannelElementBase::shared_ptr channel, ConnPolicy policy)
virtual internal::ConnID * getPortID() const
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
std::list< ChannelDescriptor > connections
static Logger & log()
Definition: Logger.hpp:350
static Logger::LogFunction endlog()
Definition: Logger.hpp:362


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:22