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 
117 {
118  if (!the_instance) {
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)
boost::intrusive_ptr< SharedConnectionBase > shared_ptr
SharedConnectionBase::shared_ptr get(const key_t &key) const
void remove(SharedConnectionBase *connection)
static const int DATA
Definition: ConnPolicy.hpp:111
boost::intrusive_ptr< SharedConnectionBase > connection
virtual const std::string & getName() const
virtual ConnID * clone() const
SharedMutexLock is a scope based Monitor, protecting critical sections with a SharedMutex object thro...
Definition: MutexLock.hpp:204
virtual bool isSameID(ConnID const &id) const
boost::shared_ptr< SharedConnectionRepository > shared_ptr
static const int BUFFER
Definition: ConnPolicy.hpp:112
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
std::string name_id
Definition: ConnPolicy.hpp:256
static Logger & log()
Definition: Logger.hpp:350
virtual const ConnPolicy * getConnPolicy() const
static Logger::LogFunction endlog()
Definition: Logger.hpp:362
static SharedConnectionRepository::shared_ptr the_instance
SharedConnectionBase(const ConnPolicy &policy)
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51


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