IBR-DTNSuite  0.10
WifiP2PManager.cpp
Go to the documentation of this file.
1 /*
2  * WifiP2PManager.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  * Written-by: Niels Wuerzbach <wuerzb@ibr.cs.tu-bs.de>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22 
23 #include "net/WifiP2PManager.h"
24 #include <ibrcommon/Logger.h>
25 #include "core/BundleCore.h"
26 #include <unistd.h>
27 
28 namespace dtn
29 {
30  namespace net
31  {
32  const std::string WifiP2PManager::TAG = "WifiP2PManager";
33 
34  //TODO How to do exception handling (try-catch) in initialization list
35  WifiP2PManager::WifiP2PManager(const std::string &ctrlpath)
36  : _running(false), _timeout(120), _priority(10),
37  ce(ctrlpath, dtn::core::BundleCore::local.getString(), *this, *this)
38  {
39  ce.setTime_ST_SCAN(20);
40  }
41 
43  {
44  // wait until the componentRun() method is finished
45  join();
46  }
47 
49  {
50  }
51 
53  {
55 
56  // set the running variable to true
57  _running = true;
58 
60  }
61 
63  {
64  while (_running) {
65  try {
66  ce.addService("IBRDTN");
67  ce.run();
68  } catch (wifip2p::SupplicantHandleException &ex) {
70  << "CoreEngine is not able to successfully communicate with SupplicantHandle. "
71  << "Exception raised: " << ex.what()
73  ::sleep(2);
74  }
75  }
76 
77  }
78 
80  {
81 
82  // iterates over all connections being established
83  list<wifip2p::Connection>::iterator conn_it = connections.begin();
84 
85  for (; conn_it != connections.end(); ++conn_it) {
86  // remove the respective connection from the local list structure.
87  try {
88  ce.disconnect(*conn_it);
89  } catch (wifip2p::SupplicantHandleException &ex) {
91  << "could not remove " << conn_it->getNetworkIntf().getName()
92  << " due to some exception: " << ex.what()
94  }
95  }
96 
98 
100 
101  // abort the main loop
102  _running = false;
103 
104  ce.stop();
105 
106  }
107 
108  const std::string WifiP2PManager::getName() const
109  {
110  return WifiP2PManager::TAG;
111  }
112 
114  {
116  }
117 
119  {
120  IBRCOMMON_LOGGER_DEBUG_TAG(WifiP2PManager::TAG, 10) << "connect request to " << uri.value << IBRCOMMON_LOGGER_ENDL;
121 
122  wifip2p::Peer p(uri.value);
123 
124  list<wifip2p::Peer>::iterator peer_it = peers.begin();
125  bool contained = false;
126 
127  for (; peer_it != peers.end(); ++peer_it) {
128  if (*peer_it == p) {
129  contained = true;
130  break;
131  } else {
132  continue;
133  }
134  }
135 
136  if (contained) {
137  try {
138  ce.connect(p);
139  } catch (wifip2p::SupplicantHandleException &ex) {
141  << "could not connect to peer " << p.getName()
142  << "@" << p.getMacAddr()
143  << " due to some exception: " << ex.what()
145  }
146  } else {
148  << "could not connect to peer " << p.getName()
149  << "@" << p.getMacAddr()
150  << "; peer not avaialable" << IBRCOMMON_LOGGER_ENDL;
151  }
152 
153  }
154 
156  {
157  IBRCOMMON_LOGGER_DEBUG_TAG(WifiP2PManager::TAG, 10) << "disconnect request from " << uri.value << IBRCOMMON_LOGGER_ENDL;
158 
159  wifip2p::Peer p(uri.value);
160 
161  try {
162  ce.disconnect(p);
163  } catch (wifip2p::SupplicantHandleException &ex) {
165  << "could not disconnect from peer " << p.getName()
166  << "@" << p.getMacAddr()
167  << " due to some exception: " << ex.what()
169  }
170  }
171 
172  void WifiP2PManager::peerFound(wifip2p::Peer peer) {
173  // create an EID for the discovered node
174  const dtn::data::EID remote_peer_eid(peer.getName());
175 
176  std::cout << "Found peer " << peer.getName() << " reported as discovered." << std::endl;
177 
178  list<wifip2p::Peer>::iterator peer_it = peers.begin();
179  bool contained = false;
180 
181  for (; peer_it != peers.end(); ++peer_it) {
182  if (*peer_it == peer) {
183  contained = true;
184  break;
185  } else {
186  continue;
187  }
188  }
189 
190  if (!contained) {
191 
192  std::cout << "Peer discovered as new peer." << std::endl;
193 
194  peers.push_back(peer);
195 
196  // create a p2p uri for the discovered node
197  const dtn::core::Node::URI uri(dtn::core::Node::NODE_P2P_DIALUP, this->getProtocol(), peer.getMacAddr(), _timeout, _priority);
198 
199  // announce the discovered peer
200  fireDiscovered(remote_peer_eid, uri);
201 
202  } else {
203 
204  std::cout << "Peer is still available." << std::endl;
205 
206  // create a p2p uri for the discovered node
207  const dtn::core::Node::URI uri(dtn::core::Node::NODE_P2P_DIALUP, this->getProtocol(), peer.getMacAddr(), _timeout, _priority);
208 
209  // announce the discovered peer
210  fireDiscovered(remote_peer_eid, uri);
211 
212  }
213 
214 
215  }
216 
217  void WifiP2PManager::connectionRequest(wifip2p::Peer peer) {
218 
219  list<wifip2p::Connection>::iterator conn_it = connections.begin();
220  bool contained = false;
221 
222  for (; conn_it != connections.end(); ++conn_it) {
223  if (conn_it->getPeer() == peer) {
224  contained = true;
225  break;
226  } else {
227  continue;
228  }
229  }
230 
231  if (!contained) {
232  try {
233  ce.connect(peer);
234  } catch (wifip2p::SupplicantHandleException &ex) {
236  << "could not connect to peer " << peer.getName()
237  << "@" << peer.getMacAddr()
238  << " due to some exception: " << ex.what()
240  }
241  } else {
243  << "peer " << peer.getName() << "@" << peer.getMacAddr()
244  << " already connected through p2p group interface "
245  << conn_it->getNetworkIntf().getName() << IBRCOMMON_LOGGER_ENDL;
246  }
247 
248  }
249 
250  void WifiP2PManager::connectionEstablished(wifip2p::Connection conn) {
251 
252  // create a new interface object
253  const ibrcommon::vinterface iface(conn.getNetworkIntf().getName());
254 
255  std::cout << "Will fire interface " << conn.getNetworkIntf().getName()<< " up." << std::endl;
256 
257  // push the actually announced connection to the local private list<Connection>
258  connections.push_back(conn);
259 
260  ::sleep(2);
261 
262  // announce the new p2p interface
263  fireInterfaceUp(iface);
264 
265  }
266 
267  void WifiP2PManager::connectionLost(wifip2p::Connection conn) {
268 
269  // create a new interface object
270  const ibrcommon::vinterface iface(conn.getNetworkIntf().getName());
271 
272  std::cout << "Connection at interface " << conn.getNetworkIntf().getName()
273  << " lost. Will fire interface down." << std::endl;
274 
275  list<wifip2p::Connection>::iterator conn_it = connections.begin();
276 
277  // remove the respective connection from the local list structure.
278  for (; conn_it != connections.end(); ++conn_it) {
279  if (conn_it->getNetworkIntf() == conn.getNetworkIntf()) {
280  conn_it = connections.erase(conn_it);
281  std::cout << "Connection removed from being locally registered." << std::endl;
282  }
283  }
284 
285  try {
286  ce.disconnect(conn);
287  } catch (wifip2p::SupplicantHandleException &ex) {
289  << "could not remove " << conn.getNetworkIntf().getName()
290  << " due to some exception: " << ex.what()
292  }
293 
294  // de-announce the p2p interface
295  fireInterfaceDown(iface);
296 
297  }
298 
299  void WifiP2PManager::log(std::string tag, std::string msg)
300  {
302  }
303 
304  void WifiP2PManager::log_err(std::string tag, std::string msg)
305  {
307  }
308 
309  void WifiP2PManager::log_debug(int debug, std::string tag, std::string msg)
310  {
311  IBRCOMMON_LOGGER_DEBUG_TAG(WifiP2PManager::TAG + "[" + tag + "]", debug) << msg << IBRCOMMON_LOGGER_ENDL;
312  }
313  } /* namespace net */
314 } /* namespace dtn */