IBR-DTN  1.0.0
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  {
101  };
102 
103  typedef std::set<dtn::data::Number> request_set;
104  typedef std::list<NodeHandshakeItem*> item_set;
105 
106  NodeHandshake();
107  NodeHandshake(MESSAGE_TYPE type, const dtn::data::Number &lifetime = 60);
108 
109  virtual ~NodeHandshake();
110 
111  void addRequest(const dtn::data::Number &identifier);
112  bool hasRequest(const dtn::data::Number &identifier) const;
113  const request_set& getRequests() const;
114 
115  void addItem(NodeHandshakeItem *item);
116  bool hasItem(const dtn::data::Number &identifier) const;
117  const item_set& getItems() const;
118 
119  MESSAGE_TYPE getType() const;
120  const dtn::data::Number& getLifetime() const;
121 
122  const std::string toString() const;
123 
124  friend std::ostream& operator<<(std::ostream&, const NodeHandshake&);
125  friend std::istream& operator>>(std::istream&, NodeHandshake&);
126 
127  template<class T>
128  T& get();
129 
130  private:
131  class StreamMap
132  {
133  public:
134  StreamMap();
135  ~StreamMap();
136  void clear();
137  std::stringstream& get(const dtn::data::Number &identifier);
138  void remove(const dtn::data::Number &identifier);
139  bool has(const dtn::data::Number &identifier);
140 
141  private:
142  typedef std::map<dtn::data::Number, std::stringstream* > stream_map;
143  stream_map _map;
144  };
145 
146  NodeHandshakeItem* getItem(const dtn::data::Number &identifier) const;
147  void clear();
148 
149  dtn::data::Number _type;
150  dtn::data::Number _lifetime;
151 
152  request_set _requests;
153  item_set _items;
154 
155  StreamMap _raw_items;
156 
157  // deny copying
158  NodeHandshake& operator=( const NodeHandshake& ) { return *this; };
159  };
160 
161  template<class T>
163  {
164  NodeHandshakeItem *item = getItem(T::identifier);
165 
166  if (item == NULL)
167  {
168  // check if the item exists
169  if (!_raw_items.has(T::identifier))
170  throw ibrcommon::Exception("item not available");
171 
172  T *item_template = new T();
173  item_template->deserialize( _raw_items.get(T::identifier) );
174 
175  // remove the raw item
176  _raw_items.remove(T::identifier);
177 
178  // put item into the itemMap
179  _items.push_back(item_template);
180 
181  return (*item_template);
182  }
183 
184  return dynamic_cast<T&>(*item);
185  }
186  } /* namespace routing */
187 } /* namespace dtn */
188 #endif /* NODEHANDSHAKE_H_ */
const std::string toString() const
friend std::istream & operator>>(std::istream &, NodeHandshake &)
virtual const dtn::data::Number & getIdentifier() const =0
virtual std::ostream & serialize(std::ostream &) const =0
size_t Length
Definition: Number.h:33
virtual std::istream & deserialize(std::istream &)=0
void addItem(NodeHandshakeItem *item)
std::ostream & serialize(std::ostream &) const
bool hasItem(const dtn::data::Number &identifier) const
const dtn::data::Number & getLifetime() const
bool hasRequest(const dtn::data::Number &identifier) const
std::istream & deserialize(std::istream &)
std::list< NodeHandshakeItem * > item_set
MESSAGE_TYPE getType() const
static const dtn::data::Number identifier
Definition: NodeHandshake.h:66
dtn::data::Length getLength() const
const dtn::data::Number & getIdentifier() const
const dtn::data::Number & getIdentifier() const
const request_set & getRequests() const
virtual dtn::data::Length getLength() const =0
const dtn::data::BundleSet & getVector() const
const item_set & getItems() const
const dtn::data::BundleSet & getVector() const
std::ostream & serialize(std::ostream &) const
dtn::data::Length getLength() const
static const dtn::data::Number identifier
Definition: NodeHandshake.h:84
std::set< dtn::data::Number > request_set
void addRequest(const dtn::data::Number &identifier)
friend std::ostream & operator<<(std::ostream &, const NodeHandshake &)
std::istream & deserialize(std::istream &)