IBR-DTNSuite  0.12
LinkManager.cpp
Go to the documentation of this file.
1 /*
2  * LinkManager.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 "ibrcommon/config.h"
26 #include "ibrcommon/Logger.h"
27 #include <list>
28 #include <string>
29 #include <typeinfo>
30 #include <unistd.h>
31 
32 #if defined HAVE_LIBNL || HAVE_LIBNL2 || HAVE_LIBNL3
34 #elif __WIN32__
36 #else
38 #endif
39 
40 namespace ibrcommon
41 {
42  // default value for LinkMonitor checks
44 
46  {
47 #if defined HAVE_LIBNL || HAVE_LIBNL2 || HAVE_LIBNL3
48  static NetLinkManager lm;
49 #elif __WIN32__
50  static Win32LinkManager lm;
51 #else
52  static PosixLinkManager lm;
53 #endif
54 
55  return lm;
56  }
57 
59  {
60  static bool initialized = false;
61  if (!initialized) {
62  getInstance().up();
63  initialized = true;
64  }
65  }
66 
68  {
69  if (cb == NULL) return;
70  ibrcommon::MutexLock l(_listener_mutex);
71 
72  callback_set &ss = _listener[iface];
73  ss.insert(cb);
74  }
75 
77  {
78  if (cb == NULL) return;
79  ibrcommon::MutexLock l(_listener_mutex);
80 
81  callback_set &ss = _listener[iface];
82 
83  ss.erase(cb);
84 
85  if (ss.empty())
86  {
87  _listener.erase(iface);
88  }
89  }
90 
92  {
93  if (cb == NULL) return;
94 
95  try {
96  ibrcommon::MutexLock l(_listener_mutex);
97 
98  for (listener_map::iterator iter = _listener.begin(); iter != _listener.end(); ++iter)
99  {
100  callback_set &ss = iter->second;
101  ss.erase(cb);
102  }
103  } catch (const ibrcommon::MutexException&) {
104  // this happens if this method is called after destroying the object
105  // and is normal at shutdown
106  }
107  }
108 
110  {
111  IBRCOMMON_LOGGER_DEBUG_TAG("LinkManager", 65) << "event raised " << lme.toString() << IBRCOMMON_LOGGER_ENDL;
112 
113  // wait some time until the event is reported to the subscribers
114  // this avoids bind issues if an address is not really ready
116 
117  // get the corresponding interface
118  const vinterface &iface = lme.getInterface();
119 
120  // search for event subscriptions
122  callback_set &ss = _listener[iface];
123 
124  for (callback_set::iterator iter = ss.begin(); iter != ss.end(); ++iter)
125  {
126  try {
127  (*iter)->eventNotify((LinkEvent&)lme);
128  } catch (const std::exception&) { };
129  }
130  }
131 
133  {
134  _link_request_interval = interval;
135  }
136 
138  {
139  return _link_request_interval;
140  }
141 
142  std::set<vinterface> LinkManager::getMonitoredInterfaces()
143  {
145 
146  std::set<vinterface> interfaces;
147 
148  for(listener_map::const_iterator it = _listener.begin(); it != _listener.end(); ++it)
149  {
150  interfaces.insert((*it).first);
151  }
152 
153  return interfaces;
154  }
155 }