IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
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
"
23
#include "
ibrcommon/link/LinkManager.h
"
24
#include "
ibrcommon/link/LinkEvent.h
"
25
#include "
ibrcommon/thread/MutexLock.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
33
#include "
ibrcommon/link/NetLinkManager.h
"
34
#elif __WIN32__
35
#include "
ibrcommon/link/Win32LinkManager.h
"
36
#else
37
#include "
ibrcommon/link/PosixLinkManager.h
"
38
#endif
39
40
namespace
ibrcommon
41
{
42
// default value for LinkMonitor checks
43
size_t
LinkManager::_link_request_interval
= 5000;
44
45
LinkManager
&
LinkManager::getInstance
()
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
58
void
LinkManager::initialize
()
59
{
60
static
bool
initialized =
false
;
61
if
(!initialized) {
62
getInstance
().
up
();
63
initialized =
true
;
64
}
65
}
66
67
void
LinkManager::addEventListener
(
const
vinterface
&iface,
LinkManager::EventCallback
*cb)
throw
()
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
76
void
LinkManager::removeEventListener
(
const
vinterface
&iface,
LinkManager::EventCallback
*cb)
throw
()
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
91
void
LinkManager::removeEventListener
(
LinkManager::EventCallback
*cb)
throw
()
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
109
void
LinkManager::raiseEvent
(
const
LinkEvent
&lme)
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
115
if
(lme.
getAction
() ==
LinkEvent::ACTION_ADDRESS_ADDED
)
ibrcommon::Thread::sleep
(1000);
116
117
// get the corresponding interface
118
const
vinterface
&iface = lme.
getInterface
();
119
120
// search for event subscriptions
121
ibrcommon::MutexLock
l(
_listener_mutex
);
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
132
void
LinkManager::setLinkRequestInterval
(
size_t
interval)
133
{
134
_link_request_interval
= interval;
135
}
136
137
size_t
LinkManager::getLinkRequestInterval
()
138
{
139
return
_link_request_interval
;
140
}
141
142
std::set<vinterface>
LinkManager::getMonitoredInterfaces
()
143
{
144
ibrcommon::MutexLock
l(
_listener_mutex
);
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
}
ibrcommon
ibrcommon
link
LinkManager.cpp
Generated on Thu Mar 27 2014 09:26:20 for IBR-DTNSuite by
1.8.4