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