IBR-DTNSuite  0.8
daemon/src/routing/NodeHandshake.cpp
Go to the documentation of this file.
00001 /*
00002  * NodeHandshake.cpp
00003  *
00004  *  Created on: 09.12.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "routing/NodeHandshake.h"
00009 
00010 namespace dtn
00011 {
00012         namespace routing
00013         {
00014                 NodeHandshake::NodeHandshake()
00015                  : _type(HANDSHAKE_INVALID)
00016                 {
00017                 }
00018 
00019                 NodeHandshake::NodeHandshake(MESSAGE_TYPE type, size_t lifetime)
00020                  : _type(type), _lifetime(lifetime)
00021                 {
00022                 }
00023 
00024                 NodeHandshake::~NodeHandshake()
00025                 {
00026                         clear();
00027                 }
00028 
00029                 void NodeHandshake::clear()
00030                 {
00031                         for (std::list<NodeHandshakeItem*>::const_iterator iter = _items.begin(); iter != _items.end(); iter++)
00032                         {
00033                                 delete (*iter);
00034                         }
00035 
00036                         // clear raw items too
00037                         _raw_items.clear();
00038                 }
00039 
00040                 void NodeHandshake::addRequest(const size_t identifier)
00041                 {
00042                         _requests.insert(identifier);
00043                 }
00044 
00045                 bool NodeHandshake::hasRequest(const size_t identifier) const
00046                 {
00047                         return (_requests.find(identifier) != _requests.end());
00048                 }
00049 
00050                 void NodeHandshake::addItem(NodeHandshakeItem *item)
00051                 {
00052                         _items.push_back(item);
00053                 }
00054 
00055                 bool NodeHandshake::hasItem(const size_t identifier) const
00056                 {
00057                         for (std::list<NodeHandshakeItem*>::const_iterator iter = _items.begin(); iter != _items.end(); iter++)
00058                         {
00059                                 const NodeHandshakeItem &item = (**iter);
00060                                 if (item.getIdentifier() == identifier) return true;
00061                         }
00062 
00063                         return false;
00064                 }
00065 
00066                 NodeHandshakeItem* NodeHandshake::getItem(const size_t identifier) const
00067                 {
00068                         for (std::list<NodeHandshakeItem*>::const_iterator iter = _items.begin(); iter != _items.end(); iter++)
00069                         {
00070                                 NodeHandshakeItem *item = (*iter);
00071                                 if (item->getIdentifier() == identifier) return item;
00072                         }
00073 
00074                         return NULL;
00075                 }
00076 
00077                 size_t NodeHandshake::getType() const
00078                 {
00079                         return _type;
00080                 }
00081 
00082                 size_t NodeHandshake::getLifetime() const
00083                 {
00084                         return _lifetime;
00085                 }
00086 
00087                 std::ostream& operator<<(std::ostream &stream, const NodeHandshake &hs)
00088                 {
00089                         // first the type as SDNV
00090                         dtn::data::SDNV type(hs._type);
00091                         stream << type;
00092 
00093                         if (hs._type == NodeHandshake::HANDSHAKE_REQUEST)
00094                         {
00095                                 // then the number of request items
00096                                 dtn::data::SDNV number_of_items(hs._requests.size());
00097                                 stream << number_of_items;
00098 
00099                                 for (std::set<size_t>::const_iterator iter = hs._requests.begin(); iter != hs._requests.end(); iter++)
00100                                 {
00101                                         dtn::data::SDNV req(*iter);
00102                                         stream << req;
00103                                 }
00104                         }
00105                         else if (hs._type == NodeHandshake::HANDSHAKE_RESPONSE)
00106                         {
00107                                 // then the lifetime of this data
00108                                 dtn::data::SDNV lifetime(hs._lifetime);
00109                                 stream << lifetime;
00110 
00111                                 // then the number of request items
00112                                 dtn::data::SDNV number_of_items(hs._items.size());
00113                                 stream << number_of_items;
00114 
00115                                 for (std::list<NodeHandshakeItem*>::const_iterator iter = hs._items.begin(); iter != hs._items.end(); iter++)
00116                                 {
00117                                         const NodeHandshakeItem &item (**iter);
00118 
00119                                         // first the identifier of the item
00120                                         dtn::data::SDNV id(item.getIdentifier());
00121                                         stream << id;
00122 
00123                                         // then the length of the payload
00124                                         dtn::data::SDNV len(item.getLength());
00125                                         stream << len;
00126 
00127                                         item.serialize(stream);
00128                                 }
00129                         }
00130 
00131                         return stream;
00132                 }
00133 
00134                 std::istream& operator>>(std::istream &stream, NodeHandshake &hs)
00135                 {
00136                         // clear the node handshake object
00137                         hs.clear();
00138 
00139                         // first the type as SDNV
00140                         dtn::data::SDNV type;
00141                         stream >> type;
00142                         hs._type = type.getValue();
00143 
00144                         if (hs._type == NodeHandshake::HANDSHAKE_REQUEST)
00145                         {
00146                                 // then the number of request items
00147                                 dtn::data::SDNV number_of_items;
00148                                 stream >> number_of_items;
00149 
00150                                 for (size_t i = 0; i < number_of_items.getValue(); i++)
00151                                 {
00152                                         dtn::data::SDNV req;
00153                                         stream >> req;
00154                                         hs._requests.insert(req.getValue());
00155                                 }
00156                         }
00157                         else if (hs._type == NodeHandshake::HANDSHAKE_RESPONSE)
00158                         {
00159                                 // then the lifetime of this data
00160                                 dtn::data::SDNV lifetime;
00161                                 stream >> lifetime;
00162                                 hs._lifetime = lifetime.getValue();
00163 
00164                                 // then the number of request items
00165                                 dtn::data::SDNV number_of_items;
00166                                 stream >> number_of_items;
00167 
00168                                 for (size_t i = 0; i < number_of_items.getValue(); i++)
00169                                 {
00170                                         // first the identifier of the item
00171                                         dtn::data::SDNV id;
00172                                         stream >> id;
00173 
00174                                         // then the length of the payload
00175                                         dtn::data::SDNV len;
00176                                         stream >> len;
00177 
00178                                         // add to raw map and create data container
00179                                         //std::stringstream *data = new std::stringstream();
00180                                         std::stringstream &data = hs._raw_items.get(id.getValue());
00181 
00182                                         // copy data to stringstream
00183                                         ibrcommon::BLOB::copy(data, stream, len.getValue());
00184                                 }
00185                         }
00186 
00187                         return stream;
00188                 }
00189 
00190                 NodeHandshake::StreamMap::StreamMap()
00191                 {
00192                 }
00193 
00194                 NodeHandshake::StreamMap::~StreamMap()
00195                 {
00196                         clear();
00197                 }
00198 
00199                 bool NodeHandshake::StreamMap::has(size_t identifier)
00200                 {
00201                         std::map<size_t, std::stringstream* >::iterator i = _map.find(identifier);
00202                         return (i != _map.end());
00203                 }
00204 
00205                 std::stringstream& NodeHandshake::StreamMap::get(size_t identifier)
00206                 {
00207                         std::map<size_t, std::stringstream* >::iterator i = _map.find(identifier);
00208 
00209                         if (i == _map.end())
00210                         {
00211                                 std::pair<std::map<size_t, std::stringstream* >::iterator, bool> p =
00212                                                 _map.insert(pair<size_t, std::stringstream* >(identifier, new stringstream()));
00213                                 i = p.first;
00214                         }
00215 
00216                         return (*(*i).second);
00217                 }
00218 
00219                 void NodeHandshake::StreamMap::remove(size_t identifier)
00220                 {
00221                         std::map<size_t, std::stringstream* >::iterator i = _map.find(identifier);
00222 
00223                         if (i != _map.end())
00224                         {
00225                                 delete (*i).second;
00226                                 _map.erase(identifier);
00227                         }
00228                 }
00229 
00230                 void NodeHandshake::StreamMap::clear()
00231                 {
00232                         for (std::map<size_t, std::stringstream* >::iterator iter = _map.begin(); iter != _map.end(); iter++)
00233                         {
00234                                 delete (*iter).second;
00235                         }
00236                         _map.clear();
00237                 }
00238 
00239                 BloomFilterSummaryVector::BloomFilterSummaryVector(const SummaryVector &vector)
00240                  : _vector(vector)
00241                 {
00242                 }
00243 
00244                 BloomFilterSummaryVector::BloomFilterSummaryVector()
00245                 {
00246                 }
00247 
00248                 BloomFilterSummaryVector::~BloomFilterSummaryVector()
00249                 {
00250                 }
00251 
00252                 size_t BloomFilterSummaryVector::getIdentifier() const
00253                 {
00254                         return identifier;
00255                 }
00256 
00257                 size_t BloomFilterSummaryVector::getLength() const
00258                 {
00259                         return _vector.getLength();
00260                 }
00261 
00262                 const SummaryVector& BloomFilterSummaryVector::getVector() const
00263                 {
00264                         return _vector;
00265                 }
00266 
00267                 std::ostream& BloomFilterSummaryVector::serialize(std::ostream &stream) const
00268                 {
00269                         stream << _vector;
00270                         return stream;
00271                 }
00272 
00273                 std::istream& BloomFilterSummaryVector::deserialize(std::istream &stream)
00274                 {
00275                         stream >> _vector;
00276                         return stream;
00277                 }
00278 
00279                 size_t BloomFilterSummaryVector::identifier = NodeHandshakeItem::BLOOM_FILTER_SUMMARY_VECTOR;
00280 
00281                 BloomFilterPurgeVector::BloomFilterPurgeVector(const SummaryVector &vector)
00282                  : _vector(vector)
00283                 {
00284                 }
00285 
00286                 BloomFilterPurgeVector::BloomFilterPurgeVector()
00287                 {
00288                 }
00289 
00290                 BloomFilterPurgeVector::~BloomFilterPurgeVector()
00291                 {
00292                 }
00293 
00294                 size_t BloomFilterPurgeVector::getIdentifier() const
00295                 {
00296                         return identifier;
00297                 }
00298 
00299                 size_t BloomFilterPurgeVector::getLength() const
00300                 {
00301                         return _vector.getLength();
00302                 }
00303 
00304                 const SummaryVector& BloomFilterPurgeVector::getVector() const
00305                 {
00306                         return _vector;
00307                 }
00308 
00309                 std::ostream& BloomFilterPurgeVector::serialize(std::ostream &stream) const
00310                 {
00311                         stream << _vector;
00312                         return stream;
00313                 }
00314 
00315                 std::istream& BloomFilterPurgeVector::deserialize(std::istream &stream)
00316                 {
00317                         stream >> _vector;
00318                         return stream;
00319                 }
00320 
00321                 size_t BloomFilterPurgeVector::identifier = NodeHandshakeItem::BLOOM_FILTER_PURGE_VECTOR;
00322 
00323         } /* namespace routing */
00324 } /* namespace dtn */