RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpreadGroup.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 "SpreadGroup.h"
28 
29 #include <stdexcept>
30 
31 #include <sp.h>
32 
33 #include <rsc/logging/Logger.h>
34 
35 #include "../../CommException.h"
36 
37 using namespace std;
38 using namespace rsc::logging;
39 
40 namespace rsb {
41 namespace spread {
42 
43 SpreadGroup::SpreadGroup(const string& n) :
44  name(n), logger(Logger::getLogger("rsb.spread.SpreadGroup")) {
45  RSCDEBUG(logger, "new spread group object, group name: " << n);
46 }
47 
49  RSCDEBUG(logger, "destructor called for group object: " << name);
50 }
51 
52 string SpreadGroup::getName() const {
53  return name;
54 }
55 
57 
58  if (!con->isActive()) {
59  throw runtime_error("Spread connection is not active");
60  }
61 
62  int retCode = SP_join(*con->getMailbox(), name.c_str());
63  handleRetCode(retCode, "joining");
64 
65 }
66 
68 
69  if (!con->isActive()) {
70  throw CommException("Connection is not active, cannot leave " + name);
71  }
72 
73  int retCode = SP_leave(*con->getMailbox(), name.c_str());
74  handleRetCode(retCode, "leaving");
75 
76 }
77 
78 void SpreadGroup::handleRetCode(const int& retCode,
79  const std::string& actionName) {
80 
81  if (!retCode) {
82  RSCDEBUG(logger, actionName << " spread group with name " << name << " successful");
83  } else {
84  stringstream msg;
85  msg << "Got error while " << actionName << " spread group '" << name
86  << "': ";
87  switch (retCode) {
88  case ILLEGAL_GROUP:
89  msg << "ILLEGAL_GROUP";
90  break;
91  case ILLEGAL_SESSION:
92  msg << "ILLEGAL_SESSION";
93  break;
94  case CONNECTION_CLOSED:
95  msg << "CONNECTION_CLOSED";
96  break;
97  default:
98  msg << "Unknown spread error with code " << retCode;
99  break;
100  }
101  RSCERROR(logger, "Error " << actionName << " spread group: " << msg.str());
102  throw CommException(msg.str());
103  }
104 
105 }
106 
107 }
108 }