Junxiao Shi | 2dc416d | 2017-07-03 04:46:16 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | * |
| 21 | * @author Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 22 | * @author Davide Pesavento <davide.pesavento@lip6.fr> |
| 23 | */ |
| 24 | |
| 25 | #include "network-monitor-stub.hpp" |
| 26 | #include <unordered_map> |
| 27 | #include <boost/range/adaptor/map.hpp> |
| 28 | #include <boost/range/algorithm/copy.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace net { |
| 32 | |
| 33 | class NetworkMonitorImplStub : public NetworkMonitorImpl |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | NetworkMonitorImplStub(uint32_t capabilities) |
| 38 | : m_capabilities(capabilities) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | uint32_t |
| 43 | getCapabilities() const final |
| 44 | { |
| 45 | return m_capabilities; |
| 46 | } |
| 47 | |
| 48 | shared_ptr<const NetworkInterface> |
| 49 | getNetworkInterface(const std::string& ifname) const final |
| 50 | { |
| 51 | auto i = m_interfaces.find(ifname); |
| 52 | return i == m_interfaces.end() ? nullptr : i->second; |
| 53 | } |
| 54 | |
| 55 | std::vector<shared_ptr<const NetworkInterface>> |
| 56 | listNetworkInterfaces() const final |
| 57 | { |
| 58 | std::vector<shared_ptr<const NetworkInterface>> v; |
| 59 | boost::copy(m_interfaces | boost::adaptors::map_values, std::back_inserter(v)); |
| 60 | return v; |
| 61 | } |
| 62 | |
| 63 | public: // internal |
| 64 | using NetworkMonitorImpl::makeNetworkInterface; |
| 65 | |
| 66 | void |
| 67 | addInterface(shared_ptr<NetworkInterface> netif) |
| 68 | { |
| 69 | BOOST_ASSERT(netif != nullptr); |
| 70 | bool isNew = m_interfaces.emplace(netif->getName(), netif).second; |
| 71 | if (!isNew) { |
| 72 | BOOST_THROW_EXCEPTION(std::invalid_argument("duplicate ifname")); |
| 73 | } |
| 74 | this->emitSignal(onInterfaceAdded, netif); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | removeInterface(const std::string& ifname) |
| 79 | { |
| 80 | auto i = m_interfaces.find(ifname); |
| 81 | if (i == m_interfaces.end()) { |
| 82 | return; |
| 83 | } |
| 84 | shared_ptr<NetworkInterface> netif = i->second; |
| 85 | m_interfaces.erase(i); |
| 86 | this->emitSignal(onInterfaceRemoved, netif); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | emitEnumerationCompleted() |
| 91 | { |
| 92 | this->emitSignal(onEnumerationCompleted); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | uint32_t m_capabilities; |
| 97 | std::unordered_map<std::string, shared_ptr<NetworkInterface>> m_interfaces; |
| 98 | }; |
| 99 | |
| 100 | NetworkMonitorStub::NetworkMonitorStub(uint32_t capabilities) |
| 101 | : NetworkMonitor(make_unique<NetworkMonitorImplStub>(capabilities)) |
| 102 | { |
| 103 | } |
| 104 | |
| 105 | NetworkMonitorImplStub& |
| 106 | NetworkMonitorStub::getImpl() |
| 107 | { |
| 108 | return static_cast<NetworkMonitorImplStub&>(this->NetworkMonitor::getImpl()); |
| 109 | } |
| 110 | |
| 111 | shared_ptr<NetworkInterface> |
| 112 | NetworkMonitorStub::makeNetworkInterface() |
| 113 | { |
| 114 | return NetworkMonitorImplStub::makeNetworkInterface(); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | NetworkMonitorStub::addInterface(shared_ptr<NetworkInterface> netif) |
| 119 | { |
| 120 | this->getImpl().addInterface(std::move(netif)); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | NetworkMonitorStub::removeInterface(const std::string& ifname) |
| 125 | { |
| 126 | this->getImpl().removeInterface(ifname); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | NetworkMonitorStub::emitEnumerationCompleted() |
| 131 | { |
| 132 | this->getImpl().emitEnumerationCompleted(); |
| 133 | } |
| 134 | |
| 135 | } // namespace net |
| 136 | } // namespace ndn |