OverSim
PubSubSubspace.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include "PubSubSubspace.h"
26 
27 using namespace std;
28 
30 {
32  lastTimestamp = 0;
33 }
34 
36 {
37 }
38 
39 std::ostream& operator<< (std::ostream& o, const PubSubSubspace& subspace)
40 {
41  o << "Id: " << subspace.spaceId << " responsible: " << subspace.responsibleNode;
42  return o;
43 }
44 
46 {
47  waitingForRespNode = false;
48 }
49 
50 std::ostream& operator<< (std::ostream& o, const PubSubSubspaceIntermediate& subspace)
51 {
52  o << dynamic_cast<const PubSubSubspace&>(subspace) << "\n";
53  o << " Children:\n";
54  set<NodeHandle>::iterator it;
55  for( it = subspace.children.begin(); it != subspace.children.end(); ++it ){
56  o << " " << *it << "\n";
57  }
58  return o;
59 }
60 
62 
65 {
67  heartbeatTimer = NULL;
70 }
71 
73 {
74  if( getNumChildren() + getNumIntermediates() < (int) maxChildren ) {
75  // we still have room in our children list, add to our own
78  }
79  return true;
80  } else {
81  // Child has to go to an intermediate
82  if( cachedChildren.insert( make_pair(child, false) ).second ){
84  }
85  return false;
86  }
87 }
88 
90 {
91  if( removeChild( child ) || cachedChildren.erase( child )){
93  return NULL;
94  } else {
95  std::deque<IntermediateNode>::iterator it;
96  for( it = intermediateNodes.begin(); it != intermediateNodes.end(); ++it ){
97  if( it->children.erase( child ) ) {
99  return &*it;
100  }
101  }
102  return NULL;
103  }
104 }
105 
107 {
108  std::deque<IntermediateNode>::iterator it;
109  for( it = intermediateNodes.begin(); it != intermediateNodes.end(); ++it ){
110  if( it->node.isUnspecified() ) continue;
111  int childIntermediates = intermediateNodes.size() - (it - intermediateNodes.begin() +1 )* maxChildren;
112  if( childIntermediates < 0 ) childIntermediates = 0;
113  if( it->children.size() + it->waitingChildren + childIntermediates < maxChildren ) return &*it;
114  }
115  return NULL;
116 }
117 
119 {
120  totalChildrenCount = children.size() + cachedChildren.size();
121  std::deque<IntermediateNode>::iterator it;
122  for( it = intermediateNodes.begin(); it != intermediateNodes.end(); ++it ){
123  totalChildrenCount += it->children.size();
124  }
125 }
126 
127 std::ostream& operator<< (std::ostream& o, const PubSubSubspaceResponsible& subspace)
128 {
129  o << dynamic_cast<const PubSubSubspaceIntermediate&>(subspace) << " BackupNode: " << subspace.backupNode;
130  o << "\n cachedChildren:\n";
131  map<NodeHandle, bool>::const_iterator iit;
132  for( iit = subspace.cachedChildren.begin(); iit != subspace.cachedChildren.end(); ++iit ){
133  o << " " << iit->first << " waiting: " << iit->second << "\n";
134  }
135  o << " totalChildrenCount: " << subspace.totalChildrenCount;
136  o << "\n IntermediateNodes:\n";
137  std::deque<PubSubSubspaceResponsible::IntermediateNode>::const_iterator it;
138  for( it = subspace.intermediateNodes.begin(); it != subspace.intermediateNodes.end(); ++it ){
139  o << " " << it->node;
140  o << "\n Children:\n";
141  for( set<NodeHandle>::iterator iit = it->children.begin(); iit != it->children.end(); ++iit ){
142  o << " " << *iit << "\n";
143  }
144  }
145  return o;
146 }
147