IBR-DTNSuite  0.12
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  {
47  };
48 
49  virtual ~NodeHandshakeItem() { };
50  virtual const dtn::data::Number& getIdentifier() const = 0;
51  virtual dtn::data::Length getLength() const = 0;
52  virtual std::ostream& serialize(std::ostream&) const = 0;
53  virtual std::istream& deserialize(std::istream&) = 0;
54  };
55 
57  {
58  public:
61  virtual ~BloomFilterSummaryVector();
62  const dtn::data::Number& getIdentifier() const;
64  std::ostream& serialize(std::ostream&) const;
65  std::istream& deserialize(std::istream&);
67 
68  const dtn::data::BundleSet& getVector() const;
69 
70  private:
71  dtn::data::BundleSet _vector;
72  };
73 
75  {
76  public:
79  virtual ~BloomFilterPurgeVector();
80  const dtn::data::Number& getIdentifier() const;
82  std::ostream& serialize(std::ostream&) const;
83  std::istream& deserialize(std::istream&);
85 
86  const dtn::data::BundleSet& getVector() const;
87 
88  private:
89  dtn::data::BundleSet _vector;
90  };
91 
93  {
94  public:
96  {
100  };
101 
102  NodeHandshake();
103  NodeHandshake(MESSAGE_TYPE type, const dtn::data::Number &lifetime = 60);
104 
105  virtual ~NodeHandshake();
106 
107  void addRequest(const dtn::data::Number &identifier);
108  bool hasRequest(const dtn::data::Number &identifier) const;
109  void addItem(NodeHandshakeItem *item);
110  bool hasItem(const dtn::data::Number &identifier) const;
111 
112  MESSAGE_TYPE getType() const;
113  const dtn::data::Number& getLifetime() const;
114 
115  const std::string toString() const;
116 
117  friend std::ostream& operator<<(std::ostream&, const NodeHandshake&);
118  friend std::istream& operator>>(std::istream&, NodeHandshake&);
119 
120  template<class T>
121  T& get();
122 
123  private:
124  class StreamMap
125  {
126  public:
127  StreamMap();
128  ~StreamMap();
129  void clear();
130  std::stringstream& get(const dtn::data::Number &identifier);
131  void remove(const dtn::data::Number &identifier);
132  bool has(const dtn::data::Number &identifier);
133 
134  private:
135  typedef std::map<dtn::data::Number, std::stringstream* > stream_map;
136  stream_map _map;
137  };
138 
139  NodeHandshakeItem* getItem(const dtn::data::Number &identifier) const;
140  void clear();
141 
142  dtn::data::Number _type;
143  dtn::data::Number _lifetime;
144 
145  typedef std::set<dtn::data::Number> request_set;
146  request_set _requests;
147 
148  typedef std::list<NodeHandshakeItem*> item_set;
149  item_set _items;
150 
151  StreamMap _raw_items;
152 
153  // deny copying
154  NodeHandshake& operator=( const NodeHandshake& ) { return *this; };
155  };
156 
157  template<class T>
159  {
160  NodeHandshakeItem *item = getItem(T::identifier);
161 
162  if (item == NULL)
163  {
164  // check if the item exists
165  if (!_raw_items.has(T::identifier))
166  throw ibrcommon::Exception("item not available");
167 
168  T *item_template = new T();
169  item_template->deserialize( _raw_items.get(T::identifier) );
170 
171  // remove the raw item
172  _raw_items.remove(T::identifier);
173 
174  // put item into the itemMap
175  _items.push_back(item_template);
176 
177  return (*item_template);
178  }
179 
180  return dynamic_cast<T&>(*item);
181  }
182  } /* namespace routing */
183 } /* namespace dtn */
184 #endif /* NODEHANDSHAKE_H_ */