RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Serialization.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project.
4  *
5  * Copyright (C) 2011 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 "Serialization.h"
28 
29 #include "../../MetaData.h"
30 #include "../../EventId.h"
31 #include "../../Scope.h"
32 
33 using namespace std;
34 
35 namespace rsb {
36 namespace transport {
37 namespace socket {
38 
39 EventPtr notificationToEvent(protocol::Notification& notification,
40  bool exposeWireSchema) {
43  EventPtr event(new Event());
44 
45  MetaData& metaData = event->mutableMetaData();
46  metaData.setCreateTime((boost::uint64_t) notification.meta_data().create_time());
47  metaData.setSendTime((boost::uint64_t) notification.meta_data().send_time());
48  metaData.setReceiveTime();
49 
50  event->setEventId(
51  rsc::misc::UUID(
52  (boost::uint8_t*) notification.event_id().sender_id().c_str()),
53  notification.event_id().sequence_number());
54  event->setScopePtr(ScopePtr(new Scope(notification.scope())));
55  if (notification.has_method()) {
56  event->setMethod(notification.method());
57  }
58 
59  for (int i = 0; i < notification.meta_data().user_infos_size(); ++i) {
60  metaData.setUserInfo(notification.meta_data().user_infos(i).key(),
61  notification.meta_data().user_infos(i).value());
62  }
63  for (int i = 0; i < notification.meta_data().user_times_size(); ++i) {
64  metaData.setUserTime(notification.meta_data().user_times(i).key(),
65  notification.meta_data().user_times(i).timestamp());
66  }
67  for (int i = 0; i < notification.causes_size(); ++i) {
68  event->addCause(EventId(rsc::misc::UUID((boost::uint8_t*) notification.causes(i).sender_id().c_str()),
69  notification.causes(i).sequence_number()));
70  }
71 
72  event->setData(boost::shared_ptr<string>(new string(notification.data())));
73 
74  if (exposeWireSchema) {
75  metaData.setUserInfo("rsb.wire-schema", notification.wire_schema());
76  }
77 
78  return event;
79 }
80 
81 void eventToNotification(protocol::Notification& notification,
82  const EventPtr& event,
83  const string& wireSchema,
84  const string& data) {
85  notification.mutable_event_id()->set_sender_id(
86  event->getMetaData().getSenderId().getId().data,
87  event->getMetaData().getSenderId().getId().size());
88  notification.mutable_event_id()->set_sequence_number(
89  event->getEventId().getSequenceNumber());
90  notification.set_scope(event->getScopePtr()->toString());
91  if (!event->getMethod().empty()) {
92  notification.set_method(event->getMethod());
93  }
94  notification.set_wire_schema(wireSchema);
95  notification.mutable_meta_data()->set_create_time(
96  event->getMetaData().getCreateTime());
97  notification.mutable_meta_data()->set_send_time(
98  event->getMetaData().getSendTime());
99  for (map<string, string>::const_iterator it =
100  event->mutableMetaData().userInfosBegin(); it
101  != event->mutableMetaData().userInfosEnd(); ++it) {
102  protocol::UserInfo* info =
103  notification.mutable_meta_data()->mutable_user_infos()->Add();
104  info->set_key(it->first);
105  info->set_value(it->second);
106  }
107  for (map<string, boost::uint64_t>::const_iterator it =
108  event->mutableMetaData().userTimesBegin(); it
109  != event->mutableMetaData().userTimesEnd(); ++it) {
110  protocol::UserTime* info =
111  notification.mutable_meta_data()->mutable_user_times()->Add();
112  info->set_key(it->first);
113  info->set_timestamp(it->second);
114  }
115 
116  set<EventId> causes = event->getCauses();
117  for (set<EventId>::const_iterator it = causes.begin();
118  it != causes.end(); ++it) {
119  protocol::EventId* cause = notification.mutable_causes()->Add();
120  cause->set_sender_id(it->getParticipantId().getId().data,
121  it->getParticipantId().getId().size());
122  cause->set_sequence_number(it->getSequenceNumber());
123  }
124 
125  notification.set_data(data);
126 }
127 
128 }
129 }
130 }