Davide Pesavento | 9a8bae5 | 2016-02-24 20:33:08 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016 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 | * |
| 22 | * Parts of this implementation is based on daemondo command of MacPorts |
| 23 | * (https://www.macports.org/): |
| 24 | * |
| 25 | * Copyright (c) 2005-2007 James Berry <jberry@macports.org> |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * Redistribution and use in source and binary forms, with or without |
| 29 | * modification, are permitted provided that the following conditions |
| 30 | * are met: |
| 31 | * 1. Redistributions of source code must retain the above copyright |
| 32 | * notice, this list of conditions and the following disclaimer. |
| 33 | * 2. Redistributions in binary form must reproduce the above copyright |
| 34 | * notice, this list of conditions and the following disclaimer in the |
| 35 | * documentation and/or other materials provided with the distribution. |
| 36 | * 3. Neither the name of The MacPorts Project nor the names of its contributors |
| 37 | * may be used to endorse or promote products derived from this software |
| 38 | * without specific prior written permission. |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 41 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 44 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 45 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 46 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 47 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 48 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 50 | * POSSIBILITY OF SUCH DAMAGE. |
| 51 | */ |
| 52 | |
| 53 | #include "ndn-cxx-config.hpp" |
| 54 | |
| 55 | #ifdef NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H |
| 56 | |
| 57 | #include "network-monitor-impl-osx.hpp" |
| 58 | |
| 59 | namespace ndn { |
| 60 | namespace util { |
| 61 | |
| 62 | NetworkMonitor::Impl::Impl(NetworkMonitor& nm, boost::asio::io_service& io) |
| 63 | : m_nm(nm) |
| 64 | , m_scheduler(io) |
| 65 | , m_cfLoopEvent(m_scheduler) |
| 66 | { |
| 67 | scheduleCfLoop(); |
| 68 | |
| 69 | // Potentially useful System Configuration regex patterns: |
| 70 | // |
| 71 | // State:/Network/Interface/.*/Link |
| 72 | // State:/Network/Interface/.*/IPv4 |
| 73 | // State:/Network/Interface/.*/IPv6 |
| 74 | // |
| 75 | // State:/Network/Global/DNS |
| 76 | // State:/Network/Global/IPv4 |
| 77 | // |
| 78 | // Potentially useful notifications from Darwin Notify Center: |
| 79 | // |
| 80 | // com.apple.system.config.network_change |
| 81 | // |
| 82 | CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 83 | static_cast<void*>(this), |
| 84 | &Impl::afterNotificationCenterEvent, |
| 85 | CFSTR("com.apple.system.config.network_change"), |
| 86 | nullptr, // object to observe |
| 87 | CFNotificationSuspensionBehaviorDeliverImmediately); |
| 88 | } |
| 89 | |
| 90 | NetworkMonitor::Impl::~Impl() |
| 91 | { |
| 92 | CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), |
| 93 | static_cast<void*>(this)); |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | NetworkMonitor::Impl::afterNotificationCenterEvent(CFNotificationCenterRef center, |
| 98 | void *observer, |
| 99 | CFStringRef name, |
| 100 | const void *object, |
| 101 | CFDictionaryRef userInfo) |
| 102 | { |
| 103 | static_cast<Impl*>(observer)->m_nm.onNetworkStateChanged(); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | NetworkMonitor::Impl::scheduleCfLoop() |
| 108 | { |
| 109 | // poll each second for new events |
| 110 | m_cfLoopEvent = m_scheduler.scheduleEvent(time::seconds(1), bind(&Impl::pollCfLoop, this)); |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | NetworkMonitor::Impl::pollCfLoop() |
| 115 | { |
| 116 | // this should dispatch ready events and exit |
| 117 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true); |
| 118 | |
| 119 | scheduleCfLoop(); |
| 120 | } |
| 121 | |
| 122 | } // namespace util |
| 123 | } // namespace ndn |
| 124 | |
| 125 | #endif // NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H |