Orocos Real-Time Toolkit  2.9.0
SharedConnection.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Intermodalics Thu Jul 30 18:28:12 CEST 2015 SharedConnection.cpp
3 
4  SharedConnection.cpp - description
5  -------------------
6  begin : Thu July 30 2015
7  copyright : (C) 2015 Intermodalics
8  email : johannes@intermodalics.eu
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 #include "SharedConnection.hpp"
39 #include "../os/MutexLock.hpp"
40 #include "../Logger.hpp"
41 
42 #if defined(ORO_HAVE_BOOST_UUID)
43  #include <boost/uuid/random_generator.hpp>
44  #include <boost/uuid/uuid_io.hpp>
45 #elif defined(ORO_HAVE_LIBUUID)
46  #include <uuid/uuid.h>
47 #endif
48 
49 using namespace RTT;
50 using namespace RTT::base;
51 using namespace RTT::internal;
52 
53 bool SharedConnID::isSameID(ConnID const& id) const
54 {
55  SharedConnID const* real_id = dynamic_cast<SharedConnID const*>(&id);
56  if (!real_id)
57  return false;
58  else return real_id->connection == this->connection;
59 }
60 
62 {
63  return new SharedConnID(this->connection);
64 }
65 
66 #ifdef ORO_HAVE_BOOST_UUID
67 static boost::uuids::random_generator uuid_generator;
68 #endif
69 
71  : policy(policy)
72 {
73  // assign random name if none was given
74  if (this->policy.name_id.empty()) {
75 #if defined(ORO_HAVE_BOOST_UUID)
76  this->policy.name_id = boost::uuids::to_string(uuid_generator());
77 #elif defined(ORO_HAVE_LIBUUID)
78  uuid_t uuid;
79  uuid_generate_random(uuid);
80  char uuid_string[37];
81  uuid_unparse(uuid, uuid_string);
82  this->policy.name_id = uuid_string;
83 #endif
84  }
85 
86  // register at SharedConnectionRepository
88 
89  log(Debug) << "Constructed shared " << (policy.type == ConnPolicy::DATA ? "data" : (policy.type == ConnPolicy::BUFFER ? "buffer" : "")) << " connection '" << getName() << "'." << endlog();
90 }
91 
93 {
94  // unregister this connection at the SharedConnectionRepository
96 
97  log(Debug) << "Destroyed shared connection '" << getName() << "'." << endlog();
98 }
99 
101 {
102  return new SharedConnID(this);
103 }
104 
105 const std::string &SharedConnectionBase::getName() const
106 {
107  return policy.name_id;
108 }
109 
111 {
112  return &policy;
113 }
114 
115 static SharedConnectionRepository::shared_ptr the_instance;
117 {
118  if (!the_instance) {
119  the_instance.reset(new SharedConnectionRepository());
120  }
121  return the_instance;
122 }
123 
125 {
126  RTT::os::MutexLock lock(mutex);
127  if (map.count(key)) return false;
128  map[key] = connection;
129  return true;
130 }
131 
133 {
134  RTT::os::MutexLock lock(mutex);
135  for(Map::iterator it = map.begin(); it != map.end(); )
136  {
137  if (it->second == connection) {
138  Map::iterator copy = it++;
139  map.erase(copy);
140  } else {
141  it++;
142  }
143  }
144 }
145 
147 {
148  RTT::os::SharedMutexLock lock(mutex);
149  return map.count(key);
150 }
151 
153 {
154  RTT::os::SharedMutexLock lock(mutex);
155  if (!map.count(key)) return SharedConnectionBase::shared_ptr();
156  return map.at(key);
157 }
bool add(const key_t &key, SharedConnectionBase *connection)
A repository which stores pointers to all shared connections within the process.
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
SharedConnectionBase::shared_ptr get(const key_t &key) const
void remove(SharedConnectionBase *connection)
int type
DATA, BUFFER or CIRCULAR_BUFFER.
Definition: ConnPolicy.hpp:190
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
static const int DATA
Definition: ConnPolicy.hpp:111
boost::intrusive_ptr< SharedConnectionBase > connection
Classes which contain all implementation code for the RTT.
virtual const std::string & getName() const
Represents a shared connection created by the ConnFactory.
virtual ConnID * clone() const
SharedMutexLock is a scope based Monitor, protecting critical sections with a SharedMutex object thro...
Definition: MutexLock.hpp:204
Base classes of RTT classes.
virtual bool isSameID(ConnID const &id) const
boost::shared_ptr< SharedConnectionRepository > shared_ptr
This class is used in places where a permanent representation of a reference to a connection is neede...
Definition: ConnID.hpp:58
static const int BUFFER
Definition: ConnPolicy.hpp:112
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
std::string name_id
The name of this connection.
Definition: ConnPolicy.hpp:256
virtual const ConnPolicy * getConnPolicy() const
Get a pointer to the connection policy used to build this channel element, if available.
SharedConnectionBase(const ConnPolicy &policy)
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51
Base class for shared connection elements.