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