IBR-DTNSuite  0.10
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 
31 #if defined HAVE_LIBNL || HAVE_LIBNL3
33 #else
35 #endif
36 
37 namespace ibrcommon
38 {
40  {
41 #if defined HAVE_LIBNL || HAVE_LIBNL3
42  static NetLinkManager lm;
43 #else
44  static PosixLinkManager lm;
45 #endif
46 
47  return lm;
48  }
49 
51  {
52  static bool initialized = false;
53  if(!initialized){
54  getInstance().up();
55  initialized=true;
56  }
57  }
58 
60  {
61  if (cb == NULL) return;
62  ibrcommon::MutexLock l(_listener_mutex);
63 
64  std::set<LinkManager::EventCallback* > &ss = _listener[iface];
65  ss.insert(cb);
66  }
67 
69  {
70  if (cb == NULL) return;
71  ibrcommon::MutexLock l(_listener_mutex);
72 
73  std::set<LinkManager::EventCallback* > &ss = _listener[iface];
74 
75  ss.erase(cb);
76 
77  if (ss.empty())
78  {
79  _listener.erase(iface);
80  }
81  }
82 
84  {
85  if (cb == NULL) return;
86 
87  try {
88  ibrcommon::MutexLock l(_listener_mutex);
89 
90  for (std::map<vinterface, std::set<LinkManager::EventCallback* > >::iterator iter = _listener.begin(); iter != _listener.end(); ++iter)
91  {
92  std::set<LinkManager::EventCallback* > &ss = iter->second;
93  ss.erase(cb);
94  }
95  } catch (const ibrcommon::MutexException&) {
96  // this happens if this method is called after destroying the object
97  // and is normal at shutdown
98  }
99  }
100 
102  {
103  IBRCOMMON_LOGGER_DEBUG_TAG("LinkManager", 65) << "event raised " << lme.toString() << IBRCOMMON_LOGGER_ENDL;
104 
105  // get the corresponding interface
106  const vinterface &iface = lme.getInterface();
107 
108  // search for event subscriptions
110  std::set<LinkManager::EventCallback* > &ss = _listener[iface];
111 
112  for (std::set<LinkManager::EventCallback* >::iterator iter = ss.begin(); iter != ss.end(); ++iter)
113  {
114  try {
115  (*iter)->eventNotify((LinkEvent&)lme);
116  } catch (const std::exception&) { };
117  }
118  }
119 }