RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MessageHandler.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2012 Jan Moringen <jmoringe@techfak.uni-bielfeld.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 "MessageHandler.h"
28 
29 #include "../../CommException.h"
30 #include "../../MetaData.h"
31 #include "../../EventId.h"
32 
33 #include "../../converter/Converter.h"
34 
35 using namespace std;
36 
37 using namespace rsc::logging;
38 
39 using namespace rsb;
40 using namespace rsb::transport;
41 using namespace rsb::protocol;
42 
43 namespace rsb {
44 namespace spread {
45 
46 MessageHandler::MessageHandler(ConverterSelectionStrategyPtr converters) :
47  ConverterSelectingConnector<string>(converters),
48  logger(rsc::logging::Logger::getLogger("rsb.spread.MessageHandler")),
49  assemblyPool(new AssemblyPool()) {
50 }
51 
53 }
54 
56  RSCDEBUG(logger, "new Spread message " << message);
57 
58  assert(message->getType() == SpreadMessage::REGULAR);
59 
60  // Deserialize notification fragment from Spread message.
61  FragmentedNotificationPtr fragment(new FragmentedNotification());
62  if (!fragment->ParseFromString(message->getDataAsString())) {
63  throw CommException("Failed to parse notification in pbuf format");
64  }
65 
66  RSCTRACE(logger,
67  "Parsed event seqnum: " << fragment->notification().event_id().sequence_number());
68  RSCTRACE(logger,
69  "Binary length: " << fragment->notification().data().length());
70  RSCTRACE(logger,
71  "Number of split message parts: " << fragment->num_data_parts());
72  RSCTRACE(logger,
73  "... received message part : " << fragment->data_part());
74 
75  // Build data from parts.
77  if (!notification) {
78  return EventPtr();
79  }
80  RSCTRACE(logger,
81  "fragmented notification joined, last message " << message);
82 
83  // Convert notification payload.
84  ConverterPtr converter = getConverter(notification->wire_schema());
85  AnnotatedData deserialized = converter->deserialize(notification->wire_schema(),
86  notification->data());
87 
88  // Construct and return event.
89  EventPtr event(new Event());
90  fillEvent(event, *notification, deserialized.second, deserialized.first);
91  event->mutableMetaData().setReceiveTime();
92 
93  return event;
94 }
95 
97  NotificationPtr notification;
98 
99  if (fragment->num_data_parts() > 1) {
100  notification = this->assemblyPool->add(fragment);
101  } else {
102  notification.reset(fragment->mutable_notification(),
103  rsc::misc::ParentSharedPtrDeleter
104  <rsb::protocol::FragmentedNotification>(fragment));
105  }
106  return notification;
107 }
108 
109 void MessageHandler::setPruning(const bool& pruning) {
110  this->assemblyPool->setPruning(pruning);
111 }
112 
113 }
114 }