RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Assembly.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 "Assembly.h"
28 
29 #include <boost/format.hpp>
30 #include <boost/date_time/microsec_time_clock.hpp>
31 
32 #include "../../protocol/ProtocolException.h"
33 
34 using namespace std;
35 
36 using namespace boost;
37 using namespace boost::posix_time;
38 
39 using namespace rsc::logging;
40 using namespace rsc::threading;
41 
42 using namespace rsb::protocol;
43 
44 namespace rsb {
45 namespace spread {
46 
48  logger(
49  Logger::getLogger(
50  str(
51  format("rsb.spread.Assembly[%1%]")
52  % n->notification().event_id().sequence_number()))), receivedParts(
53  0), birthTime(microsec_clock::local_time()) {
54  store.resize(n->num_data_parts());
55  add(n);
56 }
57 
59 }
60 
62  RSCTRACE(logger, "Joining fragments");
63  assert(isComplete());
64 
65  NotificationPtr notification(
66  store[0]->mutable_notification(),
67  rsc::misc::ParentSharedPtrDeleter
68  < rsb::protocol::FragmentedNotification > (store[0]));
69 
70  // Concatenate data parts
71  string* resultData = notification->mutable_data();
72  for (unsigned int i = 1; i < this->store.size(); ++i) {
73  resultData->append(store[i]->notification().data());
74  }
75  return notification;
76 }
77 
79  RSCTRACE(
80  logger,
81  "Adding notification " << n->notification().event_id().sequence_number() << " (part " << n->data_part() << "/" << this->store.size() << ") to assembly");
82  assert(n->num_data_parts() == store.size());
83  //assert(!store[n->data_part()]);
84  if (store[n->data_part()]) {
85  throw ProtocolException(
86  boost::str(
87  boost::format(
88  "Received fragment (%d/%d) of notification for event with sender id %x and sequence number %d twice!.")
89  % n->data_part() % n->num_data_parts()
90  % n->notification().event_id().sender_id()
91  % n->notification().event_id().sequence_number()));
92  }
93  store[n->data_part()] = n;
94  ++receivedParts;
95  return isComplete();
96 }
97 
98 bool Assembly::isComplete() const {
99  return this->receivedParts == this->store.size();
100 }
101 
102 unsigned int Assembly::age() const {
103  return (microsec_clock::local_time() - this->birthTime).total_seconds();
104 }
105 
107  boost::recursive_mutex& poolMutex, const unsigned& ageS,
108  const unsigned int& pruningIntervalMs) :
109  PeriodicTask(pruningIntervalMs), logger(
110  Logger::getLogger("rsb.spread.AssemblyPool.PruningTask")), pool(
111  pool), poolMutex(poolMutex), maxAge(ageS) {
112 }
113 
115  boost::recursive_mutex::scoped_lock lock(this->poolMutex);
116 
117  RSCDEBUG(logger, "Scanning for old assemblies");
118  Pool::iterator it = this->pool.begin();
119  while (it != this->pool.end()) {
120  if (it->second->age() > maxAge) {
121  RSCDEBUG(logger, "Pruning old assembly " << it->second);
122  this->pool.erase(it++);
123  } else {
124  ++it;
125  }
126  }
127 
128 }
129 
130 AssemblyPool::AssemblyPool(const unsigned int& ageS,
131  const unsigned int& pruningIntervalMs) :
132  logger(Logger::getLogger("rsb.spread.AssemblyPool")), pruningAgeS(ageS), pruningIntervalMs(
133  pruningIntervalMs) {
134  if (ageS == 0) {
135  throw domain_error("Age must not be 0.");
136  }
137  if (pruningIntervalMs == 0) {
138  throw domain_error("Pruning interval must not be 0");
139  }
140 }
141 
143  setPruning(false);
144 }
145 
147  boost::recursive_mutex::scoped_lock lock(pruningMutex);
148  return pruningTask;
149 }
150 
151 void AssemblyPool::setPruning(const bool& prune) {
152  boost::recursive_mutex::scoped_lock lock(pruningMutex);
153 
154  if (!isPruning() && prune) {
155  RSCDEBUG(logger, "Starting Assembly pruning");
156  pruningTask.reset(
157  new PruningTask(this->pool, this->poolMutex, pruningAgeS,
159  this->executor.schedule(this->pruningTask);
160  } else if (isPruning() && !prune) {
161  RSCDEBUG(logger, "Stopping Assembly pruning");
162  assert(pruningTask);
163  pruningTask->cancel();
164  pruningTask->waitDone();
165  RSCDEBUG(logger, "Assembly pruning stopped");
166  }
167 
168 }
169 
171  FragmentedNotificationPtr notification) {
172  boost::recursive_mutex::scoped_lock lock(this->poolMutex);
173 
174  string key = notification->notification().event_id().sender_id();
175  key.push_back(
176  notification->notification().event_id().sequence_number()
177  & 0x000000ff);
178  key.push_back(
179  notification->notification().event_id().sequence_number()
180  & 0x0000ff00);
181  key.push_back(
182  notification->notification().event_id().sequence_number()
183  & 0x00ff0000);
184  key.push_back(
185  notification->notification().event_id().sequence_number()
186  & 0xff000000);
187  Pool::iterator it = this->pool.find(key);
188  NotificationPtr result;
189  AssemblyPtr assembly;
190  if (it != this->pool.end()) {
191  // Push message to existing Assembly
192  assembly = it->second;
193  RSCTRACE(
194  logger,
195  "Adding notification " << notification->notification().event_id().sequence_number() << " to existing assembly " << assembly);
196  assembly->add(notification);
197  } else {
198  // Create new Assembly
199  RSCTRACE(
200  logger,
201  "Creating new assembly for notification " << notification->notification().event_id().sequence_number());
202  assembly.reset(new Assembly(notification));
203  it = this->pool.insert(make_pair(key, assembly)).first;
204  }
205 
206  if (assembly->isComplete()) {
207  result = assembly->getCompleteNotification();
208  this->pool.erase(it);
209  }
210 
211  RSCTRACE(logger, "dataPool size: " << this->pool.size());
212 
213  return result;
214 
215 }
216 
217 }
218 }