IBR-DTNSuite  0.10
EID.cpp
Go to the documentation of this file.
1 /*
2  * EID.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/EID.h"
23 #include "ibrdtn/utils/Utils.h"
24 #include <sstream>
25 #include <iostream>
26 
27 namespace dtn
28 {
29  namespace data
30  {
31  const std::string EID::DEFAULT_SCHEME = "dtn";
32  const std::string EID::CBHE_SCHEME = "ipn";
33 
35  : _scheme(DEFAULT_SCHEME), _ssp("none")
36  {
37  }
38 
39  EID::EID(const std::string &scheme, const std::string &ssp)
40  : _scheme(scheme), _ssp(ssp)
41  {
42  dtn::utils::Utils::trim(_scheme);
44 
45  // TODO: checks for illegal characters
46  }
47 
48  EID::EID(const std::string &orig_value)
49  : _scheme(DEFAULT_SCHEME), _ssp("none")
50  {
51  if (orig_value.length() == 0) {
52  throw dtn::InvalidDataException("given EID is empty!");
53  }
54 
55  std::string value = orig_value;
57 
58  try {
59  // search for the delimiter
60  size_t delimiter = value.find_first_of(":");
61 
62  // jump to default eid if the format is wrong
63  if (delimiter == std::string::npos)
64  throw dtn::InvalidDataException("wrong EID format: " + value);
65 
66  // the scheme is everything before the delimiter
67  _scheme = value.substr(0, delimiter);
68 
69  // the ssp is everything else
70  size_t startofssp = delimiter + 1;
71  _ssp = value.substr(startofssp, value.length() - startofssp);
72 
73  // do syntax check
74  if (_scheme.length() == 0) {
75  throw dtn::InvalidDataException("scheme is empty!");
76  }
77 
78  if (_ssp.length() == 0) {
79  throw dtn::InvalidDataException("SSP is empty!");
80  }
81  } catch (const std::exception&) {
82  _scheme = DEFAULT_SCHEME;
83  _ssp = "none";
84  }
85  }
86 
87  EID::EID(const dtn::data::Number &node, const dtn::data::Number &application)
88  : _scheme(EID::DEFAULT_SCHEME), _ssp("none")
89  {
90  if (node == 0) return;
91 
92  std::stringstream ss_ssp;
93 
94  // add node ID
95  ss_ssp << node.get<size_t>();
96 
97  // add node ID if larger than zero
98  if (application > 0) {
99  ss_ssp << "." << application.get<size_t>();
100  }
101 
102  _ssp = ss_ssp.str();
103  _scheme = CBHE_SCHEME;
104  }
105 
107  {
108  }
109 
110  EID& EID::operator=(const EID &other)
111  {
112  _ssp = other._ssp;
113  _scheme = other._scheme;
114  return *this;
115  }
116 
117  bool EID::operator==(const EID &other) const
118  {
119  return (_ssp == other._ssp) && (_scheme == other._scheme);
120  }
121 
122  bool EID::operator==(const std::string &other) const
123  {
124  return ((*this) == EID(other));
125  }
126 
127  bool EID::operator!=(const EID &other) const
128  {
129  return !((*this) == other);
130  }
131 
132  EID EID::operator+(const std::string& suffix) const
133  {
134  return EID(getString() + suffix);
135  }
136 
137  bool EID::sameHost(const std::string& other) const
138  {
139  return ( EID(other) == getNode() );
140  }
141 
142  bool EID::sameHost(const EID &other) const
143  {
144  return ( other.getNode() == getNode() );
145  }
146 
147  bool EID::operator<(const EID &other) const
148  {
149  if (_scheme < other._scheme) return true;
150  if (_scheme != other._scheme) return false;
151 
152  return _ssp < other._ssp;
153  }
154 
155  bool EID::operator>(const EID &other) const
156  {
157  return other < (*this);
158  }
159 
160  std::string EID::getString() const
161  {
162  return _scheme + ":" + _ssp;
163  }
164 
165  std::string EID::getApplication() const throw ()
166  {
167  size_t first_char = 0;
168  char delimiter = '.';
169 
170  if (_scheme != EID::CBHE_SCHEME)
171  {
172  // with a uncompressed bundle header we have another delimiter
173  delimiter = '/';
174 
175  // first char not "/", e.g. "//node1" -> 2
176  first_char = _ssp.find_first_not_of(delimiter);
177 
178  // only "/" ? thats bad!
179  if (first_char == std::string::npos) {
180  return "";
181  //throw dtn::InvalidDataException("wrong eid format, ssp: " + _ssp);
182  }
183  }
184 
185  // start of application part
186  size_t application_start = _ssp.find_first_of(delimiter, first_char);
187 
188  // no application part available
189  if (application_start == std::string::npos)
190  return "";
191 
192  // return the application part
193  return _ssp.substr(application_start + 1, _ssp.length() - application_start - 1);
194  }
195 
196  std::string EID::getHost() const throw ()
197  {
198  size_t first_char = 0;
199  char delimiter = '.';
200 
201  if (_scheme != EID::CBHE_SCHEME)
202  {
203  // with a uncompressed bundle header we have another delimiter
204  delimiter = '/';
205 
206  // first char not "/", e.g. "//node1" -> 2
207  first_char = _ssp.find_first_not_of(delimiter);
208 
209  // only "/" ? thats bad!
210  if (first_char == std::string::npos) {
211  return "none";
212  // throw dtn::InvalidDataException("wrong eid format, ssp: " + _ssp);
213  }
214  }
215 
216  // start of application part
217  size_t application_start = _ssp.find_first_of(delimiter, first_char);
218 
219  // no application part available
220  if (application_start == std::string::npos)
221  return _ssp;
222 
223  // return the node part
224  return _ssp.substr(0, application_start);
225  }
226 
227  const std::string& EID::getScheme() const
228  {
229  return _scheme;
230  }
231 
232  const std::string& EID::getSSP() const
233  {
234  return _ssp;
235  }
236 
237  EID EID::getNode() const throw ()
238  {
239  return _scheme + ":" + getHost();
240  }
241 
242  bool EID::hasApplication() const
243  {
244  // with a compressed bundle header we have another delimiter
245  if (_scheme == EID::CBHE_SCHEME)
246  {
247  return (_ssp.find_first_of(".") != std::string::npos);
248  }
249 
250  // first char not "/", e.g. "//node1" -> 2
251  size_t first_char = _ssp.find_first_not_of("/");
252 
253  // only "/" ? thats bad!
254  if (first_char == std::string::npos)
255  throw dtn::InvalidDataException("wrong eid format, ssp: " + _ssp);
256 
257  // start of application part
258  size_t application_start = _ssp.find_first_of("/", first_char);
259 
260  // no application part available
261  if (application_start == std::string::npos)
262  return false;
263 
264  // return the application part
265  return true;
266  }
267 
268  bool EID::isCompressable() const
269  {
270  return ((_scheme == DEFAULT_SCHEME) && (_ssp == "none")) || (_scheme == EID::CBHE_SCHEME);
271  }
272 
273  bool EID::isNone() const
274  {
275  return (_scheme == DEFAULT_SCHEME) && (_ssp == "none");
276  }
277 
278  std::string EID::getDelimiter() const
279  {
280  if (_scheme == EID::CBHE_SCHEME) {
281  return ".";
282  } else {
283  return "/";
284  }
285  }
286 
288  {
289  dtn::data::Number node = 0;
290  dtn::data::Number app = 0;
291 
292  if (isCompressable())
293  {
294  std::stringstream ss_node(getHost());
295  node.read(ss_node);
296 
297  if (hasApplication())
298  {
299  std::stringstream ss_app(getApplication());
300  app.read(ss_app);
301  }
302  }
303 
304  return make_pair(node, app);
305  }
306  }
307 }