IBR-DTNSuite  0.10
StaticRegexRoute.cpp
Go to the documentation of this file.
1 /*
2  * StaticRegexRoute.cpp
3  *
4  * Created on: 28.11.2012
5  * Author: morgenro
6  */
7 
9 #include <ibrcommon/Logger.h>
10 
11 namespace dtn
12 {
13  namespace routing
14  {
15  StaticRegexRoute::StaticRegexRoute(const std::string &regex, const dtn::data::EID &dest)
16  : _dest(dest), _regex_str(regex), _invalid(false), _expire(0)
17  {
18  if ( regcomp(&_regex, regex.c_str(), 0) )
19  {
20  IBRCOMMON_LOGGER_TAG("StaticRegexRoute", error) << "Could not compile regex: " << regex << IBRCOMMON_LOGGER_ENDL;
21  _invalid = true;
22  }
23  }
24 
26  {
27  if (!_invalid) {
28  regfree(&_regex);
29  }
30  }
31 
33  : _dest(obj._dest), _regex_str(obj._regex_str), _invalid(obj._invalid)
34  {
35  if ( regcomp(&_regex, _regex_str.c_str(), 0) )
36  {
37  _invalid = true;
38  }
39  }
40 
42  {
43  if (!_invalid)
44  {
45  regfree(&_regex);
46  }
47 
48  _dest = obj._dest;
49  _regex_str = obj._regex_str;
50  _invalid = obj._invalid;
51 
52  if (!_invalid)
53  {
54  if ( regcomp(&_regex, obj._regex_str.c_str(), 0) )
55  {
56  IBRCOMMON_LOGGER_TAG("StaticRegexRoute", error) << "Could not compile regex: " << _regex_str << IBRCOMMON_LOGGER_ENDL;
57  _invalid = true;
58  }
59  }
60 
61  return *this;
62  }
63 
64  bool StaticRegexRoute::match(const dtn::data::EID &eid) const
65  {
66  if (_invalid) return false;
67 
68  const std::string dest = eid.getString();
69 
70  // test against the regular expression
71  int reti = regexec(&_regex, dest.c_str(), 0, NULL, 0);
72 
73  if( !reti )
74  {
75  // the expression match
76  return true;
77  }
78  else if( reti == REG_NOMATCH )
79  {
80  // the expression not match
81  return false;
82  }
83  else
84  {
85  char msgbuf[100];
86  regerror(reti, &_regex, msgbuf, sizeof(msgbuf));
87  IBRCOMMON_LOGGER_TAG("StaticRegexRoute", error) << "Regex match failed: " << std::string(msgbuf) << IBRCOMMON_LOGGER_ENDL;
88  return false;
89  }
90  }
91 
93  {
94  return _dest;
95  }
96 
97  const std::string StaticRegexRoute::toString() const
98  {
99  std::stringstream ss;
100  ss << _regex_str << " => " << _dest.getString();
101  return ss.str();
102  }
103 
105  {
106  return _expire;
107  }
108  }
109 }