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 | |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 29 | PingServer::PingServer(Face& face, KeyChain& keyChain, const Options& options) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 30 | : m_options(options) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 31 | , m_keyChain(keyChain) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 32 | , m_name(options.prefix) |
| 33 | , m_nPings(0) |
| 34 | , m_face(face) |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 35 | , m_registeredPrefixId(nullptr) |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 36 | { |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 37 | shared_ptr<Buffer> b = make_shared<Buffer>(); |
| 38 | b->assign(m_options.payloadSize, 'a'); |
| 39 | m_payload = Block(tlv::Content, b); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 43 | PingServer::start() |
| 44 | { |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 45 | m_name.append("ping"); |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 46 | m_registeredPrefixId = m_face.setInterestFilter(m_name, |
| 47 | bind(&PingServer::onInterest, |
| 48 | this, _2), |
| 49 | bind(&PingServer::onRegisterFailed, |
| 50 | this, _2)); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | PingServer::stop() |
| 55 | { |
| 56 | if (m_registeredPrefixId != nullptr) { |
| 57 | m_face.unsetInterestFilter(m_registeredPrefixId); |
| 58 | } |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int |
| 62 | PingServer::getNPings() const |
| 63 | { |
| 64 | return m_nPings; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | PingServer::onInterest(const Interest& interest) |
| 69 | { |
| 70 | Name interestName = interest.getName(); |
| 71 | |
| 72 | afterReceive(interestName); |
| 73 | |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 74 | shared_ptr<Data> data = make_shared<Data>(interestName); |
| 75 | data->setFreshnessPeriod(m_options.freshnessPeriod); |
Eric Newberry | 62f7f71 | 2015-05-17 12:15:52 -0700 | [diff] [blame] | 76 | data->setContent(m_payload); |
Eric Newberry | 38146c5 | 2015-06-22 15:11:30 -0700 | [diff] [blame] | 77 | m_keyChain.sign(*data, signingWithSha256()); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 78 | m_face.put(*data); |
| 79 | |
| 80 | ++m_nPings; |
| 81 | if (m_options.shouldLimitSatisfied && m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) { |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 82 | afterFinish(); |
Eric Newberry | 94996d6 | 2015-05-07 13:48:46 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | PingServer::onRegisterFailed(const std::string& reason) |
| 88 | { |
| 89 | throw "Failed to register prefix in local hub's daemon, REASON: " + reason; |
| 90 | } |
| 91 | |
| 92 | } // namespace server |
| 93 | } // namespace ping |
Alexander Afanasyev | 1e7a7b2 | 2015-08-26 15:35:26 -0700 | [diff] [blame] | 94 | } // namespace ndn |