Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP |
| 23 | #define NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP |
| 24 | |
| 25 | #include "../interest.hpp" |
| 26 | #include "../security/key-chain.hpp" |
| 27 | #include "../util/time.hpp" |
| 28 | #include "../util/random.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | |
| 32 | /** |
| 33 | * @brief Helper class to generate CommandInterests |
| 34 | * |
| 35 | * @see http://redmine.named-data.net/projects/nfd/wiki/Command_Interests |
| 36 | */ |
| 37 | class CommandInterestGenerator |
| 38 | { |
| 39 | public: |
| 40 | static const Name DEFAULT_CERTIFICATE_NAME; |
| 41 | |
| 42 | CommandInterestGenerator() |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 43 | : m_lastTimestamp(time::toUnixTimestamp(time::system_clock::now())) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | virtual |
| 48 | ~CommandInterestGenerator() |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | generate(Interest& interest, const Name& certificateName = Name()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 55 | void |
| 56 | generateWithIdentity(Interest& interest, const Name& identity); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 57 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 58 | private: |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | time::milliseconds m_lastTimestamp; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 60 | KeyChain m_keyChain; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | inline void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 65 | CommandInterestGenerator::generate(Interest& interest, |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 66 | const Name& certificateName /*= Name()*/) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 68 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 69 | while (timestamp <= m_lastTimestamp) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 71 | timestamp += time::milliseconds(1); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Name commandInterestName = interest.getName(); |
| 75 | commandInterestName |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 76 | .append(name::Component::fromNumber(timestamp.count())) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 77 | .append(name::Component::fromNumber(random::generateWord64())); |
| 78 | interest.setName(commandInterestName); |
| 79 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | if (certificateName.empty()) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 81 | m_keyChain.sign(interest); |
| 82 | else |
| 83 | m_keyChain.sign(interest, certificateName); |
| 84 | |
| 85 | m_lastTimestamp = timestamp; |
| 86 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 88 | inline void |
| 89 | CommandInterestGenerator::generateWithIdentity(Interest& interest, const Name& identity) |
| 90 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
Yingdi Yu | e66bf2a | 2014-04-28 17:07:36 -0700 | [diff] [blame] | 92 | while (timestamp <= m_lastTimestamp) |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 93 | { |
| 94 | timestamp += time::milliseconds(1); |
| 95 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 96 | |
| 97 | Name commandInterestName = interest.getName(); |
| 98 | commandInterestName |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 99 | .append(name::Component::fromNumber(timestamp.count())) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 100 | .append(name::Component::fromNumber(random::generateWord64())); |
| 101 | interest.setName(commandInterestName); |
| 102 | |
| 103 | m_keyChain.signByIdentity(interest, identity); |
| 104 | |
| 105 | m_lastTimestamp = timestamp; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | } // namespace ndn |
| 110 | |
| 111 | #endif // NDN_HELPERS_COMMAND_INTEREST_GENERATOR_HPP |