IBR-DTNSuite  0.10
NodeHandshake.h
Go to the documentation of this file.
1 /*
2  * NodeHandshake.h
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #ifndef NODEHANDSHAKE_H_
23 #define NODEHANDSHAKE_H_
24 
25 #include <ibrdtn/data/BundleSet.h>
26 #include <ibrdtn/data/SDNV.h>
27 #include <iostream>
28 #include <sstream>
29 #include <list>
30 #include <set>
31 #include <map>
32 
33 namespace dtn
34 {
35  namespace routing
36  {
38  {
39  public:
41  {
46  };
47 
48  virtual ~NodeHandshakeItem() { };
49  virtual const dtn::data::Number& getIdentifier() const = 0;
50  virtual dtn::data::Length getLength() const = 0;
51  virtual std::ostream& serialize(std::ostream&) const = 0;
52  virtual std::istream& deserialize(std::istream&) = 0;
53  };
54 
56  {
57  public:
60  virtual ~BloomFilterSummaryVector();
61  const dtn::data::Number& getIdentifier() const;
63  std::ostream& serialize(std::ostream&) const;
64  std::istream& deserialize(std::istream&);
66 
67  const dtn::data::BundleSet& getVector() const;
68 
69  private:
70  dtn::data::BundleSet _vector;
71  };
72 
74  {
75  public:
78  virtual ~BloomFilterPurgeVector();
79  const dtn::data::Number& getIdentifier() const;
81  std::ostream& serialize(std::ostream&) const;
82  std::istream& deserialize(std::istream&);
84 
85  const dtn::data::BundleSet& getVector() const;
86 
87  private:
88  dtn::data::BundleSet _vector;
89  };
90 
92  {
93  public:
95  {
99  };
100 
101  NodeHandshake();
102  NodeHandshake(MESSAGE_TYPE type, const dtn::data::Number &lifetime = 60);
103 
104  virtual ~NodeHandshake();
105 
106  void addRequest(const dtn::data::Number &identifier);
107  bool hasRequest(const dtn::data::Number &identifier) const;
108  void addItem(NodeHandshakeItem *item);
109  bool hasItem(const dtn::data::Number &identifier) const;
110 
111  MESSAGE_TYPE getType() const;
112  const dtn::data::Number& getLifetime() const;
113 
114  const std::string toString() const;
115 
116  friend std::ostream& operator<<(std::ostream&, const NodeHandshake&);
117  friend std::istream& operator>>(std::istream&, NodeHandshake&);
118 
119  template<class T>
120  T& get();
121 
122  private:
123  class StreamMap
124  {
125  public:
126  StreamMap();
127  ~StreamMap();
128  void clear();
129  std::stringstream& get(const dtn::data::Number &identifier);
130  void remove(const dtn::data::Number &identifier);
131  bool has(const dtn::data::Number &identifier);
132 
133  private:
134  typedef std::map<dtn::data::Number, std::stringstream* > stream_map;
135  stream_map _map;
136  };
137 
138  NodeHandshakeItem* getItem(const dtn::data::Number &identifier) const;
139  void clear();
140 
141  dtn::data::Number _type;
142  dtn::data::Number _lifetime;
143 
144  typedef std::set<dtn::data::Number> request_set;
145  request_set _requests;
146 
147  typedef std::list<NodeHandshakeItem*> item_set;
148  item_set _items;
149 
150  StreamMap _raw_items;
151 
152  // deny copying
153  NodeHandshake& operator=( const NodeHandshake& ) { return *this; };
154  };
155 
156  template<class T>
158  {
159  NodeHandshakeItem *item = getItem(T::identifier);
160 
161  if (item == NULL)
162  {
163  // check if the item exists
164  if (!_raw_items.has(T::identifier))
165  throw ibrcommon::Exception("item not available");
166 
167  T *item_template = new T();
168  item_template->deserialize( _raw_items.get(T::identifier) );
169 
170  // remove the raw item
171  _raw_items.remove(T::identifier);
172 
173  // put item into the itemMap
174  _items.push_back(item_template);
175 
176  return (*item_template);
177  }
178 
179  return dynamic_cast<T&>(*item);
180  }
181  } /* namespace routing */
182 } /* namespace dtn */
183 #endif /* NODEHANDSHAKE_H_ */