RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MembershipManager.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "MembershipManager.h"
28 
29 #include <iostream>
30 
31 using namespace std;
32 using namespace rsc::logging;
33 
34 namespace rsb {
35 namespace spread {
36 
37 MembershipManager::MembershipManager() :
38  logger(Logger::getLogger("rsb.spread.MembershipManager")) {
39  RSCTRACE(logger, "MembershipManager() entered");
40  groups = boost::shared_ptr<GroupMap>(new GroupMap());
41 }
42 
44 }
45 
47  RSCDEBUG(logger, "Trying to join group with id " << group << " on SpreadConnection " << c);
48  boost::recursive_mutex::scoped_lock lock(groupsMutex);
49  GroupMap::iterator i = groups->find(group);
50  if (groups->end() != i) {
51  int refs = i->second.second;
52  RSCDEBUG(logger, "Group object already existing, ref count: " << refs);
53  if (0 == refs) {
54  // if count == 0 we re-join the group
55  i->second.first->join(c);
56  RSCINFO(logger, "Re-joined SpreadGroup with id " << group);
57  }
58  i->second.second = ++refs;
59  RSCTRACE(logger, "New group ref count: " << i->second.second);
60  } else {
61  RSCDEBUG(logger, "Group object not found, creating new one");
62  SpreadGroupPtr sg(new SpreadGroup(group));
63  sg->join(c);
64  RSCINFO(logger, "Joined SpreadGroup with id " << group);
65  (*groups)[group] = make_pair(sg, 1);
66  }
67 }
68 
70  RSCDEBUG(logger, "Checking if we want to leave group with id " << group << " on SpreadConnection " << c);
71  boost::recursive_mutex::scoped_lock lock(groupsMutex);
72  GroupMap::iterator i = groups->find(group);
73  if (groups->end() != i) {
74  int refs = i->second.second;
75  RSCDEBUG(logger, "Group object found, ref count: " << refs);
76  // if count-1 == 0 sg->leave
77  i->second.second = --refs;
78  RSCTRACE(logger, "New group ref count: " << i->second.second);
79  if (0 == refs) {
80  RSCTRACE(logger, "Count is 0, leaving group");
81  i->second.first->leave(c);
82  RSCINFO(logger, "Left SpreadGroup with id " << group);
83  }
84  } else {
85  RSCWARN(logger, "SpreadGroup with id " << group << " not found in GroupMap");
86  }
87 }
88 
89 }
90 }