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