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