RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 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(args.get<ConverterSelectionStrategyPtr>("converters"),
50  args.get<string> ("host", DEFAULT_HOST),
51  args.getAs<unsigned int> ("port", DEFAULT_PORT),
52  args.getAs<Server> ("server", SERVER_AUTO),
53  args.getAs<bool> ("tcpnodelay", true));
54 }
55 
56 InPullConnector::InPullConnector(ConverterSelectionStrategyPtr converters,
57  const string& host,
58  unsigned int port,
59  Server server,
60  bool tcpnodelay) :
61  ConnectorBase(converters, host, port, server, tcpnodelay),
62  InConnector(converters, host, port, server, tcpnodelay),
63  logger(Logger::getLogger("rsb.transport.socket.InPullConnector")) {
64 }
65 
67 }
68 
69 void InPullConnector::setScope(const Scope& scope) {
71 }
72 
74  if (!this->active) {
75  throw std::runtime_error("Cannot handle events when not active");
76  }
77 
78  // busEvent is an intermediate object. The deserialization of the
79  // payload still has to be performed.
80  EventPtr event(new Event(*busEvent));
81 
82  event->mutableMetaData().setReceiveTime();
83 
84  // Extract the serialized data and wire-schema from the
85  // intermediate event.
86  boost::shared_ptr<string> wireData = static_pointer_cast<string>(event->getData());
87  string wireSchema = event->getMetaData().getUserInfo("rsb.wire-schema");
88 
89  // Apply the configured converter.
91  = getConverter(wireSchema)->deserialize(wireSchema, *wireData);
92  event->setData(d.second);
93  event->setType(d.first);
94 
95  this->queue.push(event);
96 }
97 
99  if (block) {
100  return this->queue.pop();
101  } else {
102  try {
103  return this->queue.tryPop();
104  } catch (const QueueEmptyException&) {
105  return EventPtr();
106  }
107  }
108 }
109 
110 
111 }
112 }
113 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:256
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
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:60
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:58
const uint16_t DEFAULT_PORT
Definition: Types.cpp:39
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:251
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