RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InPushConnector.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede at techfak dot uni-bielefeld dot de>
6  * Copyright (C) 2012 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
7  *
8  * This file may be licensed under the terms of the
9  * GNU Lesser General Public License Version 3 (the ``LGPL''),
10  * or (at your option) any later version.
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the LGPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the LGPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The development of this software was supported by:
23  * CoR-Lab, Research Institute for Cognition and Robotics
24  * Bielefeld University
25  *
26  * ============================================================ */
27 
28 #include "InPushConnector.h"
29 
30 #include <rsc/threading/ThreadedTaskExecutor.h>
31 
32 #include "../../Scope.h"
33 
34 using namespace std;
35 
36 using namespace rsc::logging;
37 using namespace rsc::runtime;
38 using namespace rsc::threading;
39 
40 using namespace rsb::eventprocessing;
41 using namespace rsb::converter;
42 
43 namespace rsb {
44 namespace spread {
45 
46 transport::InPushConnector* InPushConnector::create(const Properties& args) {
47  static LoggerPtr logger = Logger::getLogger("rsb.spread.InConnector");
48  RSCDEBUG(logger, "creating InConnector with properties " << args);
49 
50  return new InPushConnector(args.get<ConverterSelectionStrategyPtr>("converters"),
51  args.get<string> ("host", defaultHost()),
52  args.getAs<unsigned int> ("port", defaultPort()));
53 }
54 
55 InPushConnector::InPushConnector(const ConverterSelectionStrategyPtr converters,
56  const string& host,
57  unsigned int port) :
58  transport::ConverterSelectingConnector<string>(converters), logger(
59  Logger::getLogger("rsb.spread.InPushConnector")), active(false),
60  connector(new SpreadConnector(host, port)) {
61  this->exec = TaskExecutorPtr(new ThreadedTaskExecutor);
62  this->rec = boost::shared_ptr<ReceiverTask>(new ReceiverTask(
63  this->connector->getConnection(), HandlerPtr(), this));
64 }
65 
67  if (this->active) {
68  deactivate();
69  }
70 }
71 
72 string InPushConnector::getClassName() const {
73  return "InPushConnector";
74 }
75 
76 void InPushConnector::printContents(ostream& stream) const {
77  stream << "active = " << this->active
78  << "connector = " << this->connector;
79 }
80 
82  this->connector->activate();
83 
84  // (re-)start threads
85  this->exec->schedule(rec);
86  //this->exec->schedule(st);
87  this->active = true;
88 
89  // check that scope is applied
90  if (activationScope) {
91  setScope(*activationScope);
92  activationScope.reset();
93  }
94 
95 }
96 
98  this->rec->cancel();
99  if (this->connector->getConnection()->isActive()) {
100  this->connector->getConnection()->interruptReceive();
101  this->rec->waitDone();
102  }
103  this->connector->deactivate();
104  this->active = false;
105 }
106 
108  this->connector->setQualityOfServiceSpecs(specs);
110  this->rec->setPruning(false);
111  } else {
112  this->rec->setPruning(true);
113  }
114 }
115 
117  assert(this->handlers.empty());
119  this->rec->setHandler(this->handlers.front());
120 }
121 
124  this->rec->setHandler(HandlerPtr());
125 }
126 
127 void InPushConnector::setScope(const Scope& scope) {
128  if (!active) {
129  activationScope.reset(new Scope(scope));
130  } else {
131  connector->join(connector->makeGroupName(scope));
132  }
133 }
134 
135 }
136 }