Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2015, Arizona Board of Regents. |
| 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Eric Newberry <enewberry@email.arizona.edu> |
| 20 | * @author Jerald Paul Abraham <jeraldabraham@email.arizona.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "ping-server.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ping { |
| 27 | namespace server { |
| 28 | |
| 29 | PingServer::PingServer(Face& face, const Options& options) |
| 30 | : m_options(options) |
| 31 | , m_name(options.prefix) |
| 32 | , m_nPings(0) |
| 33 | , m_face(face) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | PingServer::run() |
| 39 | { |
| 40 | m_name.append("ping"); |
| 41 | m_face.setInterestFilter(m_name, |
| 42 | bind(&PingServer::onInterest, |
| 43 | this, _2), |
| 44 | bind(&PingServer::onRegisterFailed, |
| 45 | this, _2)); |
| 46 | |
| 47 | m_face.processEvents(); |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | PingServer::getNPings() const |
| 52 | { |
| 53 | return m_nPings; |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | PingServer::onInterest(const Interest& interest) |
| 58 | { |
| 59 | Name interestName = interest.getName(); |
| 60 | |
| 61 | afterReceive(interestName); |
| 62 | |
| 63 | char responseContent[] = "NDN TLV Ping Response"; |
| 64 | shared_ptr<Data> data = make_shared<Data>(interestName); |
| 65 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
| 66 | data->setContent(reinterpret_cast<const uint8_t*>(responseContent), |
| 67 | sizeof(responseContent)); |
| 68 | m_keyChain.sign(*data); |
| 69 | m_face.put(*data); |
| 70 | |
| 71 | ++m_nPings; |
| 72 | if (m_options.shouldLimitSatisfied && m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) { |
| 73 | m_face.shutdown(); |
| 74 | m_face.getIoService().stop(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | PingServer::onRegisterFailed(const std::string& reason) |
| 80 | { |
| 81 | throw "Failed to register prefix in local hub's daemon, REASON: " + reason; |
| 82 | } |
| 83 | |
| 84 | } // namespace server |
| 85 | } // namespace ping |
| 86 | } // namespace ndn |