YARP
Yet Another Robot Platform
ConnectManager.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
9 
11 
12 namespace {
13 YARP_SERVERSQL_LOG_COMPONENT(CONNECTMANAGER, "yarp.serversql.impl.ConnectManager")
14 } // namespace
15 
16 ConnectManager::ConnectManager() = default;
17 
18 ConnectManager::~ConnectManager()
19 {
20  clear();
21 }
22 
24  for (std::list<ConnectThread *>::iterator it = con.begin();
25  it != con.end(); it++) {
26  if ((*it) != nullptr) {
27  delete (*it);
28  }
29  }
30  con.clear();
31 }
32 
33 void ConnectManager::disconnect(const std::string& src,
34  const std::string& dest,
35  bool srcDrop)
36 {
37  connect(src,dest,false);
38 }
39 
40 void ConnectManager::connect(const std::string& src,
41  const std::string& dest,
42  bool positive)
43 {
44  yCTrace(CONNECTMANAGER, " ??? %s %s", src.c_str(), dest.c_str());
45  ConnectThread *t = nullptr;
46  yCTrace(CONNECTMANAGER, "***** %zd threads", con.size());
47  std::list<ConnectThread *>::iterator it = con.begin();
48  bool already = false;
49  while (it != con.end()) {
50  if ((*it) != nullptr) {
51  if (!(*it)->needed) {
52  if (t == nullptr) {
53  yCTrace(CONNECTMANAGER, "***** reusing a thread");
54  t = (*it);
55  t->stop();
56  } else {
57  yCTrace(CONNECTMANAGER, "***** deleting a thread");
58  (*it)->stop();
59  delete (*it);
60  it = con.erase(it);
61  continue;
62  }
63  } else {
64  if ((*it)->src == src && (*it)->dest == dest) {
65  mutex.lock();
66  yCTrace(CONNECTMANAGER,
67  "??? prethread %d %d",
68  (*it)->needed,
69  (*it)->ct);
70  if ((*it)->needed) {
71  (*it)->positive = positive;
72  (*it)->ct++;
73  already = true;
74  }
75  mutex.unlock();
76  }
77  }
78  }
79  it++;
80  }
81  if (!already) {
82  if (t == nullptr) {
83  t = new ConnectThread(mutex);
84  con.push_back(t);
85  }
86  t->src = src;
87  t->dest = dest;
88  t->ct = 1;
89  t->needed = true;
90  t->positive = positive;
91  t->start();
92  }
93 }
float t
void connect(const std::string &src, const std::string &dest, bool positive=true)
void disconnect(const std::string &src, const std::string &dest, bool srcDrop)
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:34