RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventsByScopeMapConverter.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB TimeSync project.
4  *
5  * Copyright (C) 2011 by Johannes Wienke <jwienke 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 
28 
29 #include <map>
30 #include <vector>
31 
32 #include "../Event.h"
33 #include "../EventCollections.h"
34 #include "../Scope.h"
35 #include "../protocol/Notification.h"
36 #include "../protocol/collections/EventsByScopeMap.pb.h"
37 #include "Converter.h"
39 #include "SerializationException.h"
40 
41 using namespace std;
42 
43 namespace rsb {
44 namespace converter {
45 
46 EventsByScopeMapConverter::EventsByScopeMapConverter(
47  ConverterSelectionStrategy<std::string>::Ptr serializationConverters,
48  ConverterSelectionStrategy<std::string>::Ptr deserializationConverters) :
49  Converter<string>("dummy", RSB_TYPE_TAG(EventsByScopeMap)), serializationConverters(
50  serializationConverters), deserializationConverters(
51  deserializationConverters), converter(
53  protocol::collections::EventsByScopeMap>) {
54 
55 }
56 
58 }
59 
61  return converter->getWireSchema();
62 }
63 
65  return "EventsByScopeMapConverter";
66 }
67 
69  string& wire) {
70 
71  if (data.first != getDataType()) {
73  "Called with unsupported data type " + data.first);
74  }
75 
76  boost::shared_ptr<EventsByScopeMap> dataMap = boost::static_pointer_cast<
77  EventsByScopeMap>(data.second);
78 
79  boost::shared_ptr<protocol::collections::EventsByScopeMap> syncMap(
81 
82  // iterate over all scopes
83  for (EventsByScopeMap::const_iterator mapIt = dataMap->begin();
84  mapIt != dataMap->end(); ++mapIt) {
85 
86  protocol::collections::EventsByScopeMap::ScopeSet* scopeSet =
87  syncMap->add_sets();
88  scopeSet->set_scope(mapIt->first.toString());
89 
90  // iterate over all events in one scope
91  for (vector<EventPtr>::const_iterator eventIt = mapIt->second.begin();
92  eventIt != mapIt->second.end(); ++eventIt) {
93 
94  // convert event to notification
95  EventPtr event = *eventIt;
96 
98  event->getType());
99  string wire;
100  string wireSchema = c->serialize(
101  make_pair(event->getType(), event->getData()), wire);
102 
103  protocol::Notification* notification =
104  scopeSet->add_notifications();
105  protocol::fillNotificationId(*notification, event);
106  protocol::fillNotificationHeader(*notification, event, wireSchema);
107  notification->set_data(wire);
108 
109  }
110 
111  }
112 
113  return converter->serialize(
114  make_pair(
115  rsc::runtime::typeName<
117  syncMap), wire);
118 
119 }
120 
122  const string& wire) {
123 
124  if (wireSchema != getWireSchema()) {
125  throw SerializationException("Unexpected wire schema " + wireSchema);
126  }
127 
129  syncMap.ParseFromString(wire);
130 
131  boost::shared_ptr<EventsByScopeMap> dataMap(new EventsByScopeMap);
132 
133  // iterate over all scope sets
134  for (int setCount = 0; setCount < syncMap.sets_size(); ++setCount) {
135 
136  const protocol::collections::EventsByScopeMap::ScopeSet& scopeSet =
137  syncMap.sets(setCount);
138  ScopePtr scope(new Scope(scopeSet.scope()));
139 
140  // iterate over all notifications in each scope set
141  for (int notificationIndex = 0;
142  notificationIndex < scopeSet.notifications_size();
143  ++notificationIndex) {
144 
145  // convert the notification back to an event
146  const protocol::Notification& notification = scopeSet.notifications(
147  notificationIndex);
148 
149  EventPtr event(new Event);
150  event->setScopePtr(scope);
151  AnnotatedData annotatedData =
153  notification.wire_schema())->deserialize(
154  notification.wire_schema(), notification.data());
155  event->setType(annotatedData.first);
156  event->setData(annotatedData.second);
157 
158  protocol::fillEvent(event, notification, annotatedData.second,
159  annotatedData.first);
160 
161  (*dataMap)[*scope].push_back(event);
162 
163  }
164 
165  }
166 
167  return make_pair(getDataType(), dataMap);
168 
169 }
170 
171 }
172 }