RSB  0.17.0
InPullConnector.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2012, 2013, 2015 Jan Moringen <jmoringe@techfak.uni-bielefeld.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 "InPullConnector.h"
28 
29 #include "../../MetaData.h"
30 
31 #include "Factory.h"
32 
33 using namespace std;
34 
35 using namespace boost;
36 
37 using namespace rsc::logging;
38 using namespace rsc::runtime;
39 using namespace rsc::threading;
40 
41 namespace rsb {
42 namespace transport {
43 namespace socket {
44 
45 transport::InPullConnector* InPullConnector::create(const Properties& args) {
46  LoggerPtr logger = Logger::getLogger("rsb.transport.socket.InPullConnector");
47  RSCDEBUG(logger, "Creating InPullConnector with properties " << args);
48 
49  return new InPullConnector(getDefaultFactory(),
50  args.get<ConverterSelectionStrategyPtr>("converters"),
51  args.get<string> ("host", DEFAULT_HOST),
52  args.getAs<unsigned int> ("port", DEFAULT_PORT),
53  args.getAs<Server> ("server", SERVER_AUTO),
54  args.getAs<bool> ("tcpnodelay", true));
55 }
56 
57 InPullConnector::InPullConnector(FactoryPtr factory,
59  const string& host,
60  unsigned int port,
61  Server server,
62  bool tcpnodelay) :
63  ConnectorBase(factory, converters, host, port, server, tcpnodelay),
64  InConnector(factory, converters, host, port, server, tcpnodelay),
65  logger(Logger::getLogger("rsb.transport.socket.InPullConnector")) {
66 }
67 
69 }
70 
73 }
74 
76  if (!this->active) {
77  throw std::runtime_error("Cannot handle events when not active");
78  }
79 
80  // busEvent is an intermediate object. The deserialization of the
81  // payload still has to be performed.
82  EventPtr event(new Event(*busEvent));
83 
84  event->mutableMetaData().setReceiveTime();
85 
86  // Extract the serialized data and wire-schema from the
87  // intermediate event.
88  boost::shared_ptr<string> wireData = static_pointer_cast<string>(event->getData());
89  string wireSchema = event->getMetaData().getUserInfo("rsb.wire-schema");
90 
91  // Apply the configured converter.
93  = getConverter(wireSchema)->deserialize(wireSchema, *wireData);
94  event->setData(d.second);
95  event->setType(d.first);
96 
97  this->queue.push(event);
98 }
99 
101  if (block) {
102  return this->queue.pop();
103  } else {
104  try {
105  return this->queue.tryPop();
106  } catch (const QueueEmptyException&) {
107  return EventPtr();
108  }
109  }
110 }
111 
112 const std::string InPullConnector::getTransportURL() const {
114 }
115 
116 }
117 }
118 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:269
virtual const std::string getTransportURL() const
Basic message that is exchanged between informers and listeners.
Definition: Event.h:60
STL namespace.
boost::shared_ptr< Factory > FactoryPtr
Definition: Factory.h:113
void handle(rsb::EventPtr event)
Handle event.
virtual void setScope(const Scope &scope)
Instances of this class receive events from a bus that is accessed via a socket connection.
Definition: InConnector.h:61
FactoryPtr getDefaultFactory()
Definition: Factory.cpp:224
rsc::threading::SynchronizedQueue< EventPtr > queue
This class is intended to be used as a base class for connector classes of the socket-based transport...
Definition: ConnectorBase.h:59
const uint16_t DEFAULT_PORT
Definition: Types.cpp:39
virtual const std::string getTransportURL() const
ConverterPtr getConverter(const std::string &key) const
Try to find a suitable converter for key .
converter::ConverterSelectionStrategy< std::string >::Ptr ConverterSelectionStrategyPtr
boost::shared_ptr< Event > EventPtr
Definition: Event.h:264
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
virtual void setScope(const Scope &scope)
const string DEFAULT_HOST
Definition: Types.cpp:37