network-monitor-impl-osx.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
53 #include "ndn-cxx-config.hpp"
54 
55 #ifdef NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H
56 
58 #include "../network-interface.hpp"
59 
60 namespace ndn {
61 namespace util {
62 
63 NetworkMonitor::Impl::Impl(NetworkMonitor& nm, boost::asio::io_service& io)
64  : m_nm(nm)
65  , m_scheduler(io)
66  , m_cfLoopEvent(m_scheduler)
67 {
68  scheduleCfLoop();
69 
70  // Potentially useful System Configuration regex patterns:
71  //
72  // State:/Network/Interface/.*/Link
73  // State:/Network/Interface/.*/IPv4
74  // State:/Network/Interface/.*/IPv6
75  //
76  // State:/Network/Global/DNS
77  // State:/Network/Global/IPv4
78  //
79  // Potentially useful notifications from Darwin Notify Center:
80  //
81  // com.apple.system.config.network_change
82  //
83  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
84  static_cast<void*>(this),
86  CFSTR("com.apple.system.config.network_change"),
87  nullptr, // object to observe
88  CFNotificationSuspensionBehaviorDeliverImmediately);
89 }
90 
92 {
93  CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(),
94  static_cast<void*>(this));
95 }
96 
97 shared_ptr<NetworkInterface>
98 NetworkMonitor::Impl::getNetworkInterface(const std::string& ifname) const
99 {
100  return nullptr;
101 }
102 
103 std::vector<shared_ptr<NetworkInterface>>
105 {
106  return {};
107 }
108 
109 void
110 NetworkMonitor::Impl::afterNotificationCenterEvent(CFNotificationCenterRef center,
111  void* observer,
112  CFStringRef name,
113  const void* object,
114  CFDictionaryRef userInfo)
115 {
116  static_cast<Impl*>(observer)->m_nm.onNetworkStateChanged();
117 }
118 
119 void
120 NetworkMonitor::Impl::scheduleCfLoop()
121 {
122  // poll each second for new events
123  m_cfLoopEvent = m_scheduler.scheduleEvent(time::seconds(1), bind(&Impl::pollCfLoop, this));
124 }
125 
126 void
127 NetworkMonitor::Impl::pollCfLoop()
128 {
129  // this should dispatch ready events and exit
130  CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
131 
132  scheduleCfLoop();
133 }
134 
135 } // namespace util
136 } // namespace ndn
137 
138 #endif // NDN_CXX_HAVE_COREFOUNDATION_COREFOUNDATION_H
Impl(NetworkMonitor &nm, boost::asio::io_service &io)
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:75
ndn NetworkMonitor
Copyright (c) 2013-2017 Regents of the University of California.
static void afterNotificationCenterEvent(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
std::vector< shared_ptr< NetworkInterface > > listNetworkInterfaces() const
shared_ptr< NetworkInterface > getNetworkInterface(const std::string &) const