Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | **/ |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 25 | |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 26 | #include "face-monitor.hpp" |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 28 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 29 | #include <ndn-cpp-dev/face.hpp> |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 30 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 31 | namespace nfd { |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 32 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 33 | NFD_LOG_INIT("FaceMonitor"); |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 34 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 35 | FaceMonitor::FaceMonitor(ndn::Face& face) |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 36 | : m_face(face) |
| 37 | , m_isStopped(true) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | FaceMonitor::~FaceMonitor() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | FaceMonitor::removeAllSubscribers() |
| 47 | { |
| 48 | m_notificationCallbacks.clear(); |
| 49 | m_timeoutCallbacks.clear(); |
| 50 | stopNotifications(); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | FaceMonitor::stopNotifications() |
| 55 | { |
| 56 | if (static_cast<bool>(m_lastInterestId)) |
| 57 | m_face.removePendingInterest(m_lastInterestId); |
| 58 | m_isStopped = true; |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | FaceMonitor::startNotifications() |
| 63 | { |
| 64 | if (m_isStopped == false) |
| 65 | return; // Notifications cycle has been started. |
| 66 | m_isStopped = false; |
| 67 | |
| 68 | Interest interest("/localhost/nfd/faces/events"); |
| 69 | interest |
| 70 | .setMustBeFresh(true) |
| 71 | .setChildSelector(1) |
| 72 | .setInterestLifetime(time::seconds(60)) |
| 73 | ; |
| 74 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 75 | NFD_LOG_DEBUG("startNotification: Interest Sent: " << interest); |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 76 | |
| 77 | m_lastInterestId = m_face.expressInterest(interest, |
| 78 | bind(&FaceMonitor::onNotification, this, _2), |
| 79 | bind(&FaceMonitor::onTimeout, this)); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | FaceMonitor::addSubscriber(const NotificationCallback& notificationCallback) |
| 84 | { |
| 85 | addSubscriber(notificationCallback, TimeoutCallback()); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | FaceMonitor::addSubscriber(const NotificationCallback& notificationCallback, |
| 90 | const TimeoutCallback& timeoutCallback) |
| 91 | { |
| 92 | if (static_cast<bool>(notificationCallback)) |
| 93 | m_notificationCallbacks.push_back(notificationCallback); |
| 94 | |
| 95 | if (static_cast<bool>(timeoutCallback)) |
| 96 | m_timeoutCallbacks.push_back(timeoutCallback); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | FaceMonitor::onTimeout() |
| 101 | { |
| 102 | if (m_isStopped) |
| 103 | return; |
| 104 | |
| 105 | std::vector<TimeoutCallback>::iterator it; |
| 106 | for (it = m_timeoutCallbacks.begin(); |
| 107 | it != m_timeoutCallbacks.end(); ++it) { |
| 108 | (*it)(); |
| 109 | //One of the registered callbacks has cleared the vector, |
| 110 | //return now as the iterator has been invalidated and |
| 111 | //the vector is empty. |
| 112 | if (m_timeoutCallbacks.empty()) { |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Interest newInterest("/localhost/nfd/faces/events"); |
| 118 | newInterest |
| 119 | .setMustBeFresh(true) |
| 120 | .setChildSelector(1) |
| 121 | .setInterestLifetime(time::seconds(60)) |
| 122 | ; |
| 123 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 124 | NFD_LOG_DEBUG("In onTimeout, sending interest: " << newInterest); |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 125 | |
| 126 | m_lastInterestId = m_face.expressInterest(newInterest, |
| 127 | bind(&FaceMonitor::onNotification, this, _2), |
| 128 | bind(&FaceMonitor::onTimeout, this)); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | FaceMonitor::onNotification(const Data& data) |
| 133 | { |
| 134 | if (m_isStopped) |
| 135 | return; |
| 136 | |
| 137 | m_lastSequence = data.getName().get(-1).toSegment(); |
| 138 | ndn::nfd::FaceEventNotification notification(data.getContent().blockFromValue()); |
| 139 | |
| 140 | std::vector<NotificationCallback>::iterator it; |
| 141 | for (it = m_notificationCallbacks.begin(); |
| 142 | it != m_notificationCallbacks.end(); ++it) { |
| 143 | (*it)(notification); |
| 144 | if (m_notificationCallbacks.empty()) { |
| 145 | //One of the registered callbacks has cleared the vector. |
| 146 | //return back, as no one is interested in notifications anymore. |
| 147 | return; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | //Setting up next notification |
| 152 | Name nextNotification("/localhost/nfd/faces/events"); |
| 153 | nextNotification.appendSegment(m_lastSequence + 1); |
| 154 | |
| 155 | Interest interest(nextNotification); |
| 156 | interest.setInterestLifetime(time::seconds(60)); |
| 157 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 158 | NFD_LOG_DEBUG("onNotification: Interest sent: " << interest); |
Obaid | 81fb2d2 | 2014-04-07 18:10:35 -0500 | [diff] [blame] | 159 | |
| 160 | m_lastInterestId = m_face.expressInterest(interest, |
| 161 | bind(&FaceMonitor::onNotification, this, _2), |
| 162 | bind(&FaceMonitor::onTimeout, this)); |
| 163 | } |
| 164 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 165 | } // namespace nfd |