IBR-DTNSuite  0.10
Dictionary.cpp
Go to the documentation of this file.
1 /*
2  * Dictionary.cpp
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 #include "ibrdtn/data/Dictionary.h"
23 #include "ibrdtn/data/Number.h"
24 #include "ibrdtn/data/Exceptions.h"
25 #include "ibrdtn/data/Bundle.h"
26 #include <map>
27 #include <stdexcept>
28 #include <iostream>
29 #include <vector>
30 
31 namespace dtn
32 {
33  namespace data
34  {
36  {
37  }
38 
43  {
44  add(bundle);
45  }
46 
48  {
49  this->operator=(d);
50  }
51 
56  {
57  _bytestream.str(d._bytestream.str());
58  return (*this);
59  }
60 
62  {
63  }
64 
65  Number Dictionary::get(const std::string &value) const throw (EntryNotFoundException)
66  {
67  std::string bytes = _bytestream.str();
68  const char *bytebegin = bytes.c_str();
69  const char *bytepos = bytebegin;
70  const char *byteend = bytebegin + bytes.length() + 1;
71 
72  if (bytes.length() <= 0) throw EntryNotFoundException();
73 
74  while (bytepos < byteend)
75  {
76  std::string dictstr(bytepos);
77 
78  if (dictstr == value)
79  {
80  return bytepos - bytebegin;
81  }
82 
83  bytepos += dictstr.length() + 1;
84  }
85 
86  throw EntryNotFoundException();
87  }
88 
89  bool Dictionary::exists(const std::string &value) const
90  {
91  std::string bytes = _bytestream.str();
92  const char *bytepos = bytes.c_str();
93  const char *byteend = bytepos + bytes.length();
94 
95  if (bytes.length() <= 0) return false;
96 
97  while (bytepos < byteend)
98  {
99  std::string dictstr(bytepos);
100 
101  if (dictstr == value)
102  {
103  return true;
104  }
105 
106  bytepos += dictstr.length() + 1;
107  }
108 
109  return false;
110  }
111 
112  void Dictionary::add(const std::string &value)
113  {
114  if (!exists(value))
115  {
116  _bytestream << value << '\0';
117  }
118  }
119 
120  void Dictionary::add(const EID &eid)
121  {
122  add(eid.getScheme());
123  add(eid.getSSP());
124  }
125 
126  void Dictionary::add(const list<EID> &eids)
127  {
128  list<EID>::const_iterator iter = eids.begin();
129 
130  while (iter != eids.end())
131  {
132  add(*iter);
133  ++iter;
134  }
135  }
136 
137  void Dictionary::add(const Bundle &bundle)
138  {
139  // rebuild the dictionary
140  add(bundle.destination);
141  add(bundle.source);
142  add(bundle.reportto);
143  add(bundle.custodian);
144 
145  // add EID of all secondary blocks
146  for (Bundle::const_iterator iter = bundle.begin(); iter != bundle.end(); ++iter)
147  {
148  const Block &b = (**iter);
149  add( b.getEIDList() );
150  }
151  }
152 
153  EID Dictionary::get(const Number &scheme, const Number &ssp)
154  {
155  char buffer[1024];
156 
157  _bytestream.seekg(scheme.get<std::streampos>());
158  _bytestream.get(buffer, 1024, '\0');
159  std::string scheme_str(buffer);
160 
161  _bytestream.seekg(ssp.get<std::streampos>());
162  _bytestream.get(buffer, 1024, '\0');
163  std::string ssp_str(buffer);
164 
165  return EID(scheme_str, ssp_str);
166  }
167 
169  {
170  _bytestream.str("");
171  }
172 
174  {
175  return _bytestream.str().length();
176  }
177 
179  {
180  const std::string scheme = eid.getScheme();
181  const std::string ssp = eid.getSSP();
182  return make_pair(get(scheme), get(ssp));
183  }
184 
185  std::ostream &operator<<(std::ostream &stream, const dtn::data::Dictionary &obj)
186  {
187  dtn::data::Number length(obj.getSize());
188  stream << length;
189  stream << obj._bytestream.str();
190 
191  return stream;
192  }
193 
194  std::istream &operator>>(std::istream &stream, dtn::data::Dictionary &obj)
195  {
196  dtn::data::Number length;
197  stream >> length;
198 
199  // if the dictionary size if zero throw a exception
200  if (length == 0)
201  throw dtn::InvalidDataException("Dictionary size is zero!");
202 
203  obj._bytestream.str("");
204  std::vector<char> data(length.get<size_t>());
205  stream.read(&data[0], data.size());
206  obj._bytestream.write(&data[0], data.size());
207 
208  return stream;
209  }
210  }
211 }